NadaAljohani commited on
Commit
6166a91
1 Parent(s): 81b3c9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -35
app.py CHANGED
@@ -14,7 +14,7 @@ pipe_ar = pipeline('text-generation', framework='pt', model='akhooli/ap2023', to
14
  Generate a poetry in English.
15
  """
16
 
17
- pipe_en = pipeline("text-generation", model="ashiqabdulkhader/GPT2-Poet", from_tf=True)
18
 
19
  """### **Arabic and English: Text-To-Speech:**
20
  Convert the Arabic/English poetry to speech.
@@ -50,18 +50,17 @@ This function will receive 2 inputs from the Gradio interface, and execute the f
50
  3. The image.
51
  """
52
 
53
- # Generate poem based on language and convert it to audio and image
54
  def generate_poem(selected_language, text):
55
  try:
56
  if selected_language == "English":
57
- poem = generate_poem_english(text) # Return the generated poem from the generate_poem_english function
58
- sampling_rate, audio_data = text_to_speech_english(poem) # Return the audio from the text_to_speech_english function
59
- image = generate_image_from_poem(text) # Return the image from the generate_image_from_poem function
60
  elif selected_language == "Arabic":
61
- poem = generate_poem_arabic(text) # Return the generated poem from the generate_poem_arabic function
62
- sampling_rate, audio_data = text_to_speech_arabic(poem) # Return the audio from the text_to_speech_arabic function
63
- translated_text = translate_arabic_to_english(text) # Return the translated poem from Arabic to English
64
- image = generate_image_from_poem(translated_text) # Return the image from the generate_image_from_poem function
65
 
66
  return poem, (sampling_rate, audio_data), image
67
  except Exception as e:
@@ -77,32 +76,49 @@ def generate_poem_arabic(text):
77
  topk = 50
78
  topp = 0.9
79
  penalty = 1.2
80
- generated_text = pipe_ar(text, max_length=96, do_sample=True, temperature=temp, top_k=topk, top_p=topp, repetition_penalty=penalty,
81
- min_length=64, no_repeat_ngram_size=3, return_full_text=True,
82
- num_beams=5, num_return_sequences=1)[0]["generated_text"]
83
- clean_text = generated_text.replace("-", "") # To get rid of the dashes generated by the model.
 
 
 
 
 
 
 
 
 
 
 
84
  return clean_text
85
 
86
  # Poem generation for English
87
  def generate_poem_english(text):
88
- generated_text = pipe_en(text, do_sample=True, max_length=100, top_k=50, top_p=0.9, temperature=1.0, num_return_sequences=3)[0]['generated_text']
89
- clean_text = generated_text.replace("</s>", "") # To get rid of the </s> generated by the model.
 
 
 
 
 
 
 
 
90
  return clean_text
91
 
92
  """### **Audio Function:**
93
  This function is responsible for generating audio in Arabic or English, based on the provided text.
94
  """
95
 
96
- # Text-to-speech conversion for Arabic
97
  def text_to_speech_arabic(text):
98
- speech = synthesiser_arabic(text, forward_params={"speaker_embeddings": speaker_embedding_arabic})
99
  audio_data = speech["audio"]
100
  sampling_rate = speech["sampling_rate"]
101
  return (sampling_rate, audio_data)
102
 
103
- # Text-to-speech conversion for English
104
  def text_to_speech_english(text):
105
- speech = synthesiser_english(text, forward_params={"speaker_embeddings": speaker_embedding_english})
106
  audio_data = speech["audio"]
107
  sampling_rate = speech["sampling_rate"]
108
  return (sampling_rate, audio_data)
@@ -111,7 +127,6 @@ def text_to_speech_english(text):
111
  This function is responsible for generating an image based on the provided text.
112
  """
113
 
114
- # Image generation function
115
  def generate_image_from_poem(poem_text):
116
  image = pipe_image(poem_text).images[0]
117
  return image
@@ -120,7 +135,6 @@ def generate_image_from_poem(poem_text):
120
  This function is responsible for translating Arabic input to English, to be used for the image function, which accepts only English inputs.
121
  """
122
 
123
- # Translation function from Arabic to English
124
  def translate_arabic_to_english(text):
125
  translated_text = pipe_translator(text)[0]['translation_text']
126
  return translated_text
@@ -164,7 +178,6 @@ button:hover {
164
  border: 1px solid #4A90E2;
165
  border-radius: 4px;
166
  }
167
-
168
  """
169
 
170
  """### **Examples for Gradio:**
@@ -172,11 +185,10 @@ Provide 4 predefined inputs to demonstrate how the interface works.
172
  """
173
 
174
  examples = [
175
- # First parameter is for the dropdown menu, and the second parameter is for the starter of the poem
176
  ["English", "The shining sun rises over the calm ocean"],
177
  ["Arabic", "الورود تتفتح في الربيع"],
178
  ["English", "The night sky is filled with stars and dreams"],
179
- ["Arabic", "اشعة الشمس المشرقة"]
180
  ]
181
 
182
  """### **Gradio Interface:**
@@ -184,19 +196,17 @@ Creating a Gradio interface to generate a poem, read the poem, and generate an i
184
  """
185
 
186
  my_model = gr.Interface(
187
- fn=generate_poem, # The primary function that will receive the inputs (language and the starter of the poem)
188
  inputs=[
189
- gr.Dropdown(["English", "Arabic"], label="Select Language"), # Dropdown menu to select the language, either "English" or "Arabic"
190
- gr.Textbox(label="Enter a sentence") # Textbox where the user will input a sentence or phrase to generate the poem
191
  ],
192
-
193
  outputs=[
194
- gr.Textbox(label="Generated Poem", lines=10), # Textbox to display the generated poem
195
- gr.Audio(label="Generated Audio", type="numpy"), # Audio output for the generated poem
196
- gr.Image(label="Generated Image") # Image output for the generated image
197
  ],
198
-
199
- examples=examples, # Predefined examples to guide the user
200
- css=custom_css # Applying custom CSS
201
  )
202
- my_model.launch()
 
14
  Generate a poetry in English.
15
  """
16
 
17
+ pipe_en = pipeline("text-generation", model="ismaelfaro/gpt2-poems.en")
18
 
19
  """### **Arabic and English: Text-To-Speech:**
20
  Convert the Arabic/English poetry to speech.
 
50
  3. The image.
51
  """
52
 
 
53
  def generate_poem(selected_language, text):
54
  try:
55
  if selected_language == "English":
56
+ poem = generate_poem_english(text)
57
+ sampling_rate, audio_data = text_to_speech_english(poem)
58
+ image = generate_image_from_poem(text)
59
  elif selected_language == "Arabic":
60
+ poem = generate_poem_arabic(text)
61
+ sampling_rate, audio_data = text_to_speech_arabic(poem)
62
+ translated_text = translate_arabic_to_english(text)
63
+ image = generate_image_from_poem(translated_text)
64
 
65
  return poem, (sampling_rate, audio_data), image
66
  except Exception as e:
 
76
  topk = 50
77
  topp = 0.9
78
  penalty = 1.2
79
+ generated_text = pipe_ar(
80
+ text,
81
+ max_length=96,
82
+ do_sample=True,
83
+ temperature=temp,
84
+ top_k=topk,
85
+ top_p=topp,
86
+ repetition_penalty=penalty,
87
+ min_length=64,
88
+ no_repeat_ngram_size=3,
89
+ return_full_text=True,
90
+ num_beams=5,
91
+ num_return_sequences=1
92
+ )[0]["generated_text"]
93
+ clean_text = generated_text.replace("-", "") # To get rid of the dashes generated by the model.
94
  return clean_text
95
 
96
  # Poem generation for English
97
  def generate_poem_english(text):
98
+ generated_text = pipe_en(
99
+ text,
100
+ do_sample=True,
101
+ max_length=100,
102
+ top_k=50,
103
+ top_p=0.9,
104
+ temperature=1.0,
105
+ num_return_sequences=1
106
+ )[0]['generated_text']
107
+ clean_text = generated_text.replace("</s>", "") # To get rid of the </s> generated by the model.
108
  return clean_text
109
 
110
  """### **Audio Function:**
111
  This function is responsible for generating audio in Arabic or English, based on the provided text.
112
  """
113
 
 
114
  def text_to_speech_arabic(text):
115
+ speech = synthesiser_arabic(text, speaker_embeddings=speaker_embedding_arabic)
116
  audio_data = speech["audio"]
117
  sampling_rate = speech["sampling_rate"]
118
  return (sampling_rate, audio_data)
119
 
 
120
  def text_to_speech_english(text):
121
+ speech = synthesiser_english(text, speaker_embeddings=speaker_embedding_english)
122
  audio_data = speech["audio"]
123
  sampling_rate = speech["sampling_rate"]
124
  return (sampling_rate, audio_data)
 
127
  This function is responsible for generating an image based on the provided text.
128
  """
129
 
 
130
  def generate_image_from_poem(poem_text):
131
  image = pipe_image(poem_text).images[0]
132
  return image
 
135
  This function is responsible for translating Arabic input to English, to be used for the image function, which accepts only English inputs.
136
  """
137
 
 
138
  def translate_arabic_to_english(text):
139
  translated_text = pipe_translator(text)[0]['translation_text']
140
  return translated_text
 
178
  border: 1px solid #4A90E2;
179
  border-radius: 4px;
180
  }
 
181
  """
182
 
183
  """### **Examples for Gradio:**
 
185
  """
186
 
187
  examples = [
 
188
  ["English", "The shining sun rises over the calm ocean"],
189
  ["Arabic", "الورود تتفتح في الربيع"],
190
  ["English", "The night sky is filled with stars and dreams"],
191
+ ["Arabic", "أشعة الشمس المشرقة"]
192
  ]
193
 
194
  """### **Gradio Interface:**
 
196
  """
197
 
198
  my_model = gr.Interface(
199
+ fn=generate_poem,
200
  inputs=[
201
+ gr.Dropdown(["English", "Arabic"], label="Select Language"),
202
+ gr.Textbox(label="Enter a sentence")
203
  ],
 
204
  outputs=[
205
+ gr.Textbox(label="Generated Poem", lines=10),
206
+ gr.Audio(label="Generated Audio", type="numpy"),
207
+ gr.Image(label="Generated Image")
208
  ],
209
+ examples=examples,
210
+ css=custom_css
 
211
  )
212
+ my_model.launch()