Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
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
|
42 |
"""
|
43 |
|
44 |
pipe_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en")
|
45 |
|
46 |
"""### **Primary Function:**
|
47 |
-
This function will
|
48 |
-
1. The
|
49 |
-
2. The
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
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
|
76 |
num_beams=5, num_return_sequences=1)[0]["generated_text"]
|
77 |
-
clean_text = generated_text.replace("-", "") #To get rid of the
|
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=
|
83 |
-
clean_text = generated_text.replace("</s>", "") #To get rid of the </s> generated by the model.
|
84 |
return clean_text
|
85 |
|
86 |
-
"""###
|
87 |
-
This function is responsible for generating
|
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
|
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
|
115 |
"""
|
116 |
|
117 |
-
#Translation
|
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,
|
178 |
"""
|
179 |
|
180 |
my_model = gr.Interface(
|
181 |
-
fn=generate_poem, #The primary function that will
|
182 |
inputs=[
|
183 |
-
gr.Dropdown(["English", "Arabic"], label="Select Language"), #Dropdown menu to select the language, either "English" or "Arabic"
|
184 |
-
gr.Textbox(label="Enter a sentence")
|
|
|
185 |
|
186 |
outputs=[
|
187 |
-
gr.Textbox(label="Generated Poem", lines=10),
|
188 |
-
gr.Audio(label="Generated Audio", type="numpy")
|
189 |
-
|
190 |
-
|
191 |
|
192 |
-
examples=examples, #Predefined examples to guide the user
|
193 |
-
css=custom_css #Applying CSS
|
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()
|