NadaAljohani commited on
Commit
7df626b
·
verified ·
1 Parent(s): 939ae4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -38
app.py CHANGED
@@ -32,38 +32,40 @@ embeddings_dataset_english = load_dataset("Matthijs/cmu-arctic-xvectors", split=
32
  speaker_embedding_english = torch.tensor(embeddings_dataset_english[7306]["xvector"]).unsqueeze(0)
33
 
34
  """### **English Text-To-Image:**
35
- convert the starter of the English poetry to an image.
36
  """
37
 
38
  pipe_image = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
39
 
40
  """### **Translator from Arabic to English:**
41
- The text-to-image model dosn't support Arabic, therefore we need to translate the starter of the Arabic poetry to English in order to generate image.
42
  """
43
 
44
  pipe_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en")
45
 
46
  """### **Primary Function:**
47
- This function will recives 2 inputs from the gradio interface, and excute the thollowing function and retrun 3 outputs:
48
- 1. The generted poem.
49
- 2. The aduio.
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
- if selected_language == "English":
56
- poem = generate_poem_english(text) #retrun the generated poem from the generate_poem_english function
57
- sampling_rate, audio_data = text_to_speech_english(poem) #return the audio from the text_to_speech_english function
58
- # image = generate_image_from_poem(text) #return the image from the generate_image_from_poem function
59
- elif selected_language == "Arabic":
60
- poem = generate_poem_arabic(text) #retrun the generated poem from the generate_poem_arabic function
61
- sampling_rate, audio_data = text_to_speech_arabic(poem) #return the audio from the text_to_speech_arabic function
62
- translated_text = translate_arabic_to_english(text) #return the translated poem from arabic to englsih, using translate_arabic_to_english function
63
- # image = generate_image_from_poem(translated_text) #return the image from the generate_image_from_poem function
64
-
65
- return poem, (sampling_rate, audio_data)
66
- # , image
 
 
67
 
68
  """### **Poem Generation Function:**
69
  This function is responsible for generating a poem (text) in Arabic or English, based on the provided text.
@@ -71,20 +73,24 @@ This function is responsible for generating a poem (text) in Arabic or English,
71
 
72
  # Poem generation for Arabic
73
  def generate_poem_arabic(text):
 
 
 
 
74
  generated_text = pipe_ar(text, max_length=96, do_sample=True, temperature=temp, top_k=topk, top_p=topp, repetition_penalty=penalty,
75
- min_length = 64, no_repeat_ngram_size = 3, return_full_text=True,
76
  num_beams=5, num_return_sequences=1)[0]["generated_text"]
77
- clean_text = generated_text.replace("-", "") #To get rid of the dashs generated by the model.
78
  return clean_text
79
 
80
  # Poem generation for English
81
  def generate_poem_english(text):
82
- generated_text = pipe_en(text, do_sample=True, max_length=100, top_k=0, top_p=0.9, temperature=1.0, num_return_sequences=3,)[0]['generated_text']
83
- clean_text = generated_text.replace("</s>", "") #To get rid of the </s> generated by the model.
84
  return clean_text
85
 
86
- """### **ِAduio Function:**
87
- This function is responsible for generating an audio in Arabic or English, based on the provided text.
88
  """
89
 
90
  # Text-to-speech conversion for Arabic
@@ -105,16 +111,16 @@ def text_to_speech_english(text):
105
  This function is responsible for generating an image based on the provided text.
106
  """
107
 
108
- #Image Function
109
  def generate_image_from_poem(poem_text):
110
  image = pipe_image(poem_text).images[0]
111
  return image
112
 
113
  """### **Translation Function:**
114
- This function is responsible for translating the Arabic input to English, to be used for the image function, which accept only english inputs.
115
  """
116
 
117
- #Translation Function from Arabic to English
118
  def translate_arabic_to_english(text):
119
  translated_text = pipe_translator(text)[0]['translation_text']
120
  return translated_text
@@ -166,7 +172,7 @@ Provide 4 predefined inputs to demonstrate how the interface works.
166
  """
167
 
168
  examples = [
169
- #First parameter is for the dropdown menu, and the second parameter is for the starter of the poem
170
  ["English", "The shining sun rises over the calm ocean"],
171
  ["Arabic", "الورود تتفتح في الربيع"],
172
  ["English", "The night sky is filled with stars and dreams"],
@@ -174,22 +180,23 @@ examples = [
174
  ]
175
 
176
  """### **Gradio Interface:**
177
- Creating a Gradio interface to generate a poem, reading the poem, and generate an image based on that poem.
178
  """
179
 
180
  my_model = gr.Interface(
181
- fn=generate_poem, #The primary function that will recives the inputs (language and the starter of the poem)
182
  inputs=[
183
- gr.Dropdown(["English", "Arabic"], label="Select Language"), #Dropdown menu to select the language, either "English" or "Arabic" for the poem
184
- gr.Textbox(label="Enter a sentence")], #Textbox where the user will input a sentence or phrase to generate the poem (starter of the peom)
 
185
 
186
  outputs=[
187
- gr.Textbox(label="Generated Poem", lines=10), # Textbox to display the generated poem
188
- gr.Audio(label="Generated Audio", type="numpy") #Audio output for the generated poem
189
- # gr.Image(label="Generated Image")
190
- ], #Display an image generated from the starter of the peom
191
 
192
- examples=examples, #Predefined examples to guide the user how to use the interface
193
- css=custom_css #Applying CSS Custeom
194
  )
195
- my_model.launch()
 
32
  speaker_embedding_english = torch.tensor(embeddings_dataset_english[7306]["xvector"]).unsqueeze(0)
33
 
34
  """### **English Text-To-Image:**
35
+ Convert the starter of the English poetry to an image.
36
  """
37
 
38
  pipe_image = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
39
 
40
  """### **Translator from Arabic to English:**
41
+ The text-to-image model doesn't support Arabic, therefore we need to translate the starter of the Arabic poetry to English in order to generate image.
42
  """
43
 
44
  pipe_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en")
45
 
46
  """### **Primary Function:**
47
+ This function will receive 2 inputs from the Gradio interface, and execute the following functions and return 3 outputs:
48
+ 1. The generated poem.
49
+ 2. The audio.
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:
68
+ return f"Error: {str(e)}", None, None
69
 
70
  """### **Poem Generation Function:**
71
  This function is responsible for generating a poem (text) in Arabic or English, based on the provided text.
 
73
 
74
  # Poem generation for Arabic
75
  def generate_poem_arabic(text):
76
+ temp = 1.0
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
 
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
118
 
119
  """### **Translation Function:**
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
 
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"],
 
180
  ]
181
 
182
  """### **Gradio Interface:**
183
+ Creating a Gradio interface to generate a poem, read the poem, and generate an image based on that poem.
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()