Advertisement
pszemraj

creating gradio Blocks demo from colab - chatGPT prompt

Jan 9th, 2023 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.38 KB | Software | 0 0
  1. Rewrite the python code to gradio API for a demo. Users should be able to enter the source text into a `gr.Textbox` Truncate entered user text to 4000 characters for the demo, and ensure this is displayed to the users via a gr.Markdown() object. Additionally, briefly summarize what the demo does and how it works in a gr.Markdown() block.
  2.  
  3. ---
  4. Gradio Blocks API example
  5.  
  6. import gradio as gr
  7. def update(name):
  8.     return f"Welcome to Gradio, {name}!"
  9.  
  10. with gr.Blocks() as demo:
  11.     gr.Markdown("Start typing below and then click **Run** to see the output.")
  12.     with gr.Row():
  13.         inp = gr.Textbox(placeholder="What is your name?")
  14.         out = gr.Textbox()
  15.     btn = gr.Button("Run")
  16.     btn.click(fn=update, inputs=inp, outputs=out)
  17.  
  18. demo.launch()
  19.  
  20. ---
  21. python code to be rewritten to gradio demo
  22.  
  23.  
  24. import re
  25. from tqdm.auto import tqdm
  26. #@title define functions
  27. #@markdown - this defines the `split_text` and `correct_text` functions
  28. #@markdown - **NOTE:** you may need to adjust the regex in `split_text` depending on your source text
  29.  
  30. def split_text(text: str) -> list:
  31.     # Split the text into sentences using regex
  32.     sentences = re.split(r"(?<=[^A-Z].[.?]) +(?=[A-Z])", text)
  33.  
  34.     # Initialize a list to store the sentence batches
  35.     sentence_batches = []
  36.  
  37.     # Initialize a temporary list to store the current batch of sentences
  38.     temp_batch = []
  39.  
  40.     # Iterate through the sentences
  41.     for sentence in sentences:
  42.         # Add the sentence to the temporary batch
  43.         temp_batch.append(sentence)
  44.  
  45.         # If the length of the temporary batch is between 2 and 3 sentences, or if it is the last batch, add it to the list of sentence batches
  46.         if len(temp_batch) >= 2 and len(temp_batch) <= 3 or sentence == sentences[-1]:
  47.             sentence_batches.append(temp_batch)
  48.             temp_batch = []
  49.  
  50.     return sentence_batches
  51.  
  52.  
  53. def correct_text(text: str, checker, corrector, separator: str = " ") -> str:
  54.     # Split the text into sentence batches
  55.     sentence_batches = split_text(text)
  56.  
  57.     # Initialize a list to store the corrected text
  58.     corrected_text = []
  59.  
  60.     # Iterate through the sentence batches
  61.     for batch in tqdm(
  62.         sentence_batches, total=len(sentence_batches), desc="correcting text.."
  63.     ):
  64.         # Join the sentences in the batch into a single string
  65.         raw_text = " ".join(batch)
  66.  
  67.         # Check the grammar quality of the text using the text-classification pipeline
  68.         results = checker(raw_text)
  69.  
  70.         # Only correct the text if the results of the text-classification are not LABEL_1 or are LABEL_1 with a score below 0.9
  71.         if results[0]["label"] != "LABEL_1" or (
  72.             results[0]["label"] == "LABEL_1" and results[0]["score"] < 0.9
  73.         ):
  74.             # Correct the text using the text-generation pipeline
  75.             corrected_batch = corrector(raw_text)
  76.             corrected_text.append(corrected_batch[0]["generated_text"])
  77.         else:
  78.             corrected_text.append(raw_text)
  79.  
  80.     # Join the corrected text into a single string
  81.     corrected_text = separator.join(corrected_text)
  82.  
  83.     return corrected_text
  84.  
  85. #@title enter text to correct
  86. raw_text = "the toweris 324 met (1,063 ft) tall, about height as .An 81-storey building, and biggest longest structure paris. Is square, measuring 125 metres (410 ft) on each side. During its constructiothe eiffel tower surpassed the washington monument to become the tallest man-made structure in the world, a title it held for 41 yearsuntilthe chryslerbuilding in new york city was finished in 1930. It was the first structure to goat a height of 300 metres. Due 2 the addition ofa brdcasting aerial at the t0pp of the twr in 1957, it now taller than  chrysler building 5.2 metres (17 ft). Exxxcluding transmitters,  eiffel tower is  2ndd tallest ree-standing structure in france after millau viaduct." #@param {type:"string"}
  87.  
  88. import pprint as pp
  89.  
  90. # Initialize the text-classification pipeline
  91. from transformers import pipeline
  92. checker = pipeline("text-classification", "textattack/roberta-base-CoLA")
  93.  
  94. # Initialize the text-generation pipeline
  95. from transformers import pipeline
  96. corrector = pipeline(
  97.         "text2text-generation",
  98.         "pszemraj/flan-t5-large-grammar-synthesis",
  99.     )
  100.  
  101. # Correct the text using the correct_text function
  102. corrected_text = correct_text(raw_text, checker, corrector)
  103. pp.pprint(corrected_text)
  104. ---
  105.  
Tags: chatGPT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement