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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -146
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import pipeline
2
  from datasets import load_dataset
3
  import gradio as gr
4
  import torch
@@ -11,10 +11,12 @@ Generate a poetry in Arabic.
11
  pipe_ar = pipeline('text-generation', framework='pt', model='akhooli/ap2023', tokenizer='akhooli/ap2023')
12
 
13
  """### **English: Text-Generation:**
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.
@@ -54,149 +56,14 @@ This function will receive 2 inputs from the Gradio interface, and execute the f
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.
72
- """
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
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
- # 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:**
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()
 
1
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
2
  from datasets import load_dataset
3
  import gradio as gr
4
  import torch
 
11
  pipe_ar = pipeline('text-generation', framework='pt', model='akhooli/ap2023', tokenizer='akhooli/ap2023')
12
 
13
  """### **English: Text-Generation:**
14
+ Generate a poetry in English using GPT-2 Poet model with AutoTokenizer and AutoModelForCausalLM.
15
  """
16
 
17
+ # Load the tokenizer and model for the GPT-2 poetry model
18
+ tokenizer_en = AutoTokenizer.from_pretrained("ashiqabdulkhader/GPT2-Poet")
19
+ model_en = AutoModelForCausalLM.from_pretrained("ashiqabdulkhader/GPT2-Poet")
20
 
21
  """### **Arabic and English: Text-To-Speech:**
22
  Convert the Arabic/English poetry to speech.
 
56
  def generate_poem(selected_language, text):
57
  try:
58
  if selected_language == "English":
59
+ poem = generate_poem_english(text) # Return the generated poem from the generate_poem_english function
60
+ sampling_rate, audio_data = text_to_speech_english(poem) # Return the audio from the text_to_speech_english function
61
+ image = generate_image_from_poem(poem) # Return the image from the generate_image_from_poem function
62
  elif selected_language == "Arabic":
63
+ poem = generate_poem_arabic(text) # Return the generated poem from the generate_poem_arabic function
64
+ sampling_rate, audio_data = text_to_speech_arabic(poem) # Return the audio from the text_to_speech_arabic function
65
+ translated_text = translate_arabic_to_english(poem) # Return the translated poem from Arabic to English
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