NadaAljohani commited on
Commit
e56f928
1 Parent(s): 169e0c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -1
app.py CHANGED
@@ -66,4 +66,134 @@ def generate_poem(selected_language, text):
66
  image = generate_image_from_poem(translated_text) # Return the image from the generate_image_from_poem function
67
 
68
  return poem, (sampling_rate, audio_data), image
69
- except Exc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  image = generate_image_from_poem(translated_text) # Return the image from the generate_image_from_poem function
67
 
68
  return poem, (sampling_rate, audio_data), image
69
+ except Exception as e:
70
+ return f"Error: {str(e)}", None, None
71
+
72
+ """### **Poem Generation Function:**
73
+ This function is responsible for generating a poem (text) in Arabic or English, based on the provided text.
74
+ """
75
+
76
+ # Poem generation for Arabic
77
+ def generate_poem_arabic(text):
78
+ generated_text = pipe_ar(text, max_length=96, do_sample=True, temperature=1.0, top_k=50, top_p=0.9,
79
+ repetition_penalty=1.2, min_length=64, no_repeat_ngram_size=3, num_beams=5,
80
+ num_return_sequences=1)[0]["generated_text"]
81
+ clean_text = generated_text.replace("-", "") # To get rid of the dashes generated by the model.
82
+ return clean_text
83
+
84
+ # Poem generation for English using AutoTokenizer and AutoModelForCausalLM
85
+ def generate_poem_english(text):
86
+ inputs = tokenizer_en(text, return_tensors="pt")
87
+ outputs = model_en.generate(**inputs, max_length=100, do_sample=True, top_k=50, top_p=0.9, temperature=1.0)
88
+ generated_text = tokenizer_en.decode(outputs[0], skip_special_tokens=True)
89
+ clean_text = generated_text.replace("</s>", "") # Clean any unwanted tokens
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)
109
+
110
+ """### **Image Function:**
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
127
+
128
+ """### **CSS Styling:**"""
129
+
130
+ custom_css = """
131
+ body {
132
+ background-color: #f4f4f9;
133
+ color: #333;
134
+ }
135
+ .gradio-container {
136
+ border-radius: 10px;
137
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
138
+ background-color: #fff;
139
+ }
140
+ label {
141
+ color: #4A90E2;
142
+ font-weight: bold;
143
+ }
144
+
145
+ input[type="text"],
146
+ textarea {
147
+ border: 1px solid #4A90E2;
148
+ }
149
+ textarea {
150
+ height: 150px;
151
+ }
152
+
153
+ button {
154
+ background-color: #4A90E2;
155
+ color: #fff;
156
+ border-radius: 5px;
157
+ cursor: pointer;
158
+ }
159
+ button:hover {
160
+ background-color: #357ABD;
161
+ }
162
+
163
+ .dropdown {
164
+ border: 1px solid #4A90E2;
165
+ border-radius: 4px;
166
+ }
167
+
168
+ """
169
+
170
+ """### **Examples for Gradio:**
171
+ Provide 4 predefined inputs to demonstrate how the interface works.
172
+ """
173
+
174
+ examples = [
175
+ ["English", "The shining sun rises over the calm ocean"],
176
+ ["Arabic", "الورود تتفتح في الربيع"],
177
+ ["English", "The night sky is filled with stars and dreams"],
178
+ ["Arabic", "اشعة الشمس المشرقة"]
179
+ ]
180
+
181
+ """### **Gradio Interface:**
182
+ Creating a Gradio interface to generate a poem, read the poem, and generate an image based on that poem.
183
+ """
184
+
185
+ my_model = gr.Interface(
186
+ fn=generate_poem, # The primary function that will receive the inputs (language and the starter of the poem)
187
+ inputs=[
188
+ gr.Dropdown(["English", "Arabic"], label="Select Language"), # Dropdown menu to select the language
189
+ gr.Textbox(label="Enter a sentence") # Textbox where the user will input a sentence or phrase to generate the poem
190
+ ],
191
+ outputs=[
192
+ gr.Textbox(label="Generated Poem", lines=10), # Textbox to display the generated poem
193
+ gr.Audio(label="Generated Audio", type="numpy"), # Audio output for the generated poem
194
+ gr.Image(label="Generated Image") # Image output for the generated image
195
+ ],
196
+ examples=examples, # Predefined examples to guide the user
197
+ css=custom_css # Applying custom CSS
198
+ )
199
+ my_model.launch()