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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
2
  from datasets import load_dataset
3
  import gradio as gr
4
  import torch
@@ -11,12 +11,10 @@ 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 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,14 +54,14 @@ This function will receive 2 inputs from the Gradio interface, and execute the f
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 Exception as e:
@@ -75,18 +73,20 @@ This function is responsible for generating a poem (text) in Arabic or English,
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:**
@@ -172,6 +172,7 @@ 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"],
@@ -185,15 +186,17 @@ Creating a Gradio interface to generate a poem, read the poem, and generate an i
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()
 
1
+ from transformers import pipeline
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.
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
  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:
 
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:**
 
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"],
 
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()