Jaane commited on
Commit
3a2543a
·
verified ·
1 Parent(s): 275943b
Files changed (1) hide show
  1. app.py +59 -1
app.py CHANGED
@@ -1,6 +1,64 @@
 
 
1
  import warnings
2
  import random
3
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import os
5
  import gradio as gr
6
  from parrot import Parrot
@@ -83,4 +141,4 @@ with gr.Blocks() as demo:
83
  copy_button.click(copy_to_clipboard, inputs=output_text, outputs=None) # Copy the output to clipboard
84
 
85
  # Launch the app
86
- demo.launch()
 
1
+
2
+ import os
3
  import warnings
4
  import random
5
  import torch
6
+ import gradio as gr
7
+ from parrot import Parrot
8
+ from concurrent.futures import ThreadPoolExecutor, as_completed
9
+
10
+ warnings.filterwarnings("ignore")
11
+ os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1'
12
+
13
+
14
+ def random_state(seed):
15
+ torch.manual_seed(seed)
16
+ if torch.cuda.is_available():
17
+ torch.cuda.manual_seed_all(seed)
18
+
19
+
20
+ random_state(1234)
21
+
22
+ parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5")
23
+
24
+
25
+ def paraphrase_sentence(sentence):
26
+ paraphrases = parrot.augment(input_phrase=sentence, max_return_phrases=5, max_length=100, adequacy_threshold=0.75,
27
+ fluency_threshold=0.75)
28
+ return random.choice(paraphrases)[0] if paraphrases else sentence
29
+
30
+
31
+ def split_text_by_fullstop(text):
32
+ return [sentence.strip() for sentence in text.split('.') if sentence]
33
+
34
+
35
+ def process_text_by_fullstop(text):
36
+ sentences = split_text_by_fullstop(text)
37
+
38
+ with ThreadPoolExecutor() as executor:
39
+ future_to_sentence = {executor.submit(paraphrase_sentence, sentence + '.'): sentence for sentence in sentences}
40
+ paraphrased_sentences = []
41
+ for future in as_completed(future_to_sentence):
42
+ paraphrased_sentences.append(future.result())
43
+
44
+ return ' '.join(paraphrased_sentences)
45
+
46
+
47
+ def gradio_interface(input_text):
48
+ paraphrased_text = process_text_by_fullstop(input_text)
49
+ return paraphrased_text
50
+
51
+
52
+
53
+ interface = gr.Interface(fn=gradio_interface,
54
+ inputs="text",
55
+ outputs="text",
56
+ title="Text Paraphraser",
57
+ description="Enter text to get paraphrased output.")
58
+ interface.launch()
59
+ '''import warnings
60
+ import random
61
+ import torch
62
  import os
63
  import gradio as gr
64
  from parrot import Parrot
 
141
  copy_button.click(copy_to_clipboard, inputs=output_text, outputs=None) # Copy the output to clipboard
142
 
143
  # Launch the app
144
+ demo.launch()'''