imseldrith commited on
Commit
b828971
·
1 Parent(s): 0786772

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -3,6 +3,7 @@ from parrot import Parrot
3
  from gtts import gTTS
4
  from IPython.display import Audio
5
  import os
 
6
 
7
  # Create a Parrot instance
8
  parrot = Parrot(model_tag="turing", use_gpu=False)
@@ -32,6 +33,15 @@ def speak_paraphrased_text(text, language):
32
  tts.save("paraphrased_text.mp3")
33
  return Audio("paraphrased_text.mp3", autoplay=True)
34
 
 
 
 
 
 
 
 
 
 
35
  # Create a Gradio interface
36
  interface = gr.Interface(
37
  fn=paraphrase_text,
@@ -40,20 +50,30 @@ interface = gr.Interface(
40
  gr.inputs.Dropdown(list(languages.keys()), label="Select language for paraphrasing:"),
41
  gr.inputs.Dropdown(list(models.keys()), label="Select model for paraphrasing:"),
42
  gr.inputs.Slider(minimum=1, maximum=10, default=5, label="Level of paraphrasing:"),
43
- gr.inputs.Checkbox(label="Generate audio output?")
44
- ],
45
- outputs=gr.outputs.Textbox(label="Paraphrased text:"),
46
- title="Parrot - Text Paraphrasing",
47
- description="Enter a text to paraphrase using Parrot.",
48
- theme="default"
 
 
 
 
49
  )
50
 
51
- # Launch the interface
52
- interface.launch()
 
 
 
 
 
 
 
 
 
 
53
 
54
- # If the user has selected the "Generate audio output?" checkbox, call the speak_paraphrased_text function to generate and play the audio
55
- if interface.inputs[4].value:
56
- speak_paraphrased_text(interface.outputs.value, languages[interface.inputs[1].value])
57
-
58
- # Delete the generated audio file
59
- os.remove("paraphrased_text.mp3")
 
3
  from gtts import gTTS
4
  from IPython.display import Audio
5
  import os
6
+ import difflib
7
 
8
  # Create a Parrot instance
9
  parrot = Parrot(model_tag="turing", use_gpu=False)
 
33
  tts.save("paraphrased_text.mp3")
34
  return Audio("paraphrased_text.mp3", autoplay=True)
35
 
36
+ def highlight_diffs(original_text, paraphrased_text, level):
37
+ # Use diff-match-patch library to highlight the differences between original and paraphrased text
38
+ dmp = difflib.Differ()
39
+ diffs = list(dmp.compare(original_text.split(), paraphrased_text.split()))
40
+ if level == "only_changes":
41
+ return " ".join([diff[2:] for diff in diffs if diff.startswith("+ ")])
42
+ else:
43
+ return " ".join([diff[2:] if diff.startswith("+ ") else diff[2:].replace(diff[0], "<span style='background-color:yellow'>"+diff[0]+"</span>") for diff in diffs])
44
+
45
  # Create a Gradio interface
46
  interface = gr.Interface(
47
  fn=paraphrase_text,
 
50
  gr.inputs.Dropdown(list(languages.keys()), label="Select language for paraphrasing:"),
51
  gr.inputs.Dropdown(list(models.keys()), label="Select model for paraphrasing:"),
52
  gr.inputs.Slider(minimum=1, maximum=10, default=5, label="Level of paraphrasing:"),
53
+ gr.inputs.Checkbox(label="Generate audio output?"),
54
+ gr.inputs.File(label="Upload file containing text to paraphrase (Optional):")
55
+ ],
56
+ outputs=[
57
+ gr.outputs.Textbox(label="Paraphrased text:"),
58
+ gr.outputs.Download(label="Download paraphrased text as text file:"),
59
+ gr.outputs.HTML(label="Highlighted differences:")
60
+ ],
61
+ title="Parrot Paraphrasing Tool",
62
+ description="Enter text to paraphrase, select language and model, and choose level of paraphrasing."
63
  )
64
 
65
+ def app(text, language, model, level, audio_output, file_upload):
66
+ if file_upload:
67
+ with open(file_upload.name, "r", encoding="utf-8") as f:
68
+ text = f.read()
69
+ paraphrased_text = paraphrase_text(text, languages[language], models[model], level)
70
+ if audio_output:
71
+ speak_paraphrased_text(paraphrased_text, languages[language])
72
+ download_output = gr.outputs.Download(paraphrased_text, label="Download paraphrased text as text file:", mime_type="text/plain")
73
+ highlighted_diffs = highlight_diffs(text, paraphrased_text, level)
74
+ return paraphrased_text, download_output, highlighted_diffs
75
+
76
+ interface.test_launch(app)
77
 
78
+ if name == "main":
79
+ interface.launch()