SarahMarzouq commited on
Commit
1c8f664
·
verified ·
1 Parent(s): ca56bd9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from datasets import load_dataset
3
+ import gradio as gr
4
+ import torch
5
+ from diffusers import DiffusionPipeline
6
+
7
+ pipe_ar = pipeline('text-generation', framework='pt', model='akhooli/ap2023', tokenizer='akhooli/ap2023')
8
+ pipe_en = pipeline("text-generation", model="ismaelfaro/gpt2-poems.en")
9
+ pipe_image = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
10
+ pipe_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en")
11
+
12
+ # Initialize text-to-speech models for Arabic and English
13
+ # Arabic: text-to-speech
14
+ synthesiser_arabic = pipeline("text-to-speech", model="MBZUAI/speecht5_tts_clartts_ar")
15
+ embeddings_dataset_arabic = load_dataset("herwoww/arabic_xvector_embeddings", split="validation")
16
+ speaker_embedding_arabic = torch.tensor(embeddings_dataset_arabic[105]["speaker_embeddings"]).unsqueeze(0)
17
+
18
+ # English: text-to-speech
19
+ synthesiser_english = pipeline("text-to-speech", model="microsoft/speecht5_tts")
20
+ embeddings_dataset_english = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
21
+ speaker_embedding_english = torch.tensor(embeddings_dataset_english[7306]["xvector"]).unsqueeze(0)
22
+
23
+ # Generate poem based on language and convert it to audio and image
24
+ def generate_poem(selected_language, text):
25
+ if selected_language == "English":
26
+ poem = generate_poem_english(text) #retrun the generated poem from the generate_poem_english function
27
+ sampling_rate, audio_data = text_to_speech_english(poem) #return the audio from the text_to_speech_english function
28
+ image = generate_image_from_poem(text) #return the image from the generate_image_from_poem function
29
+ elif selected_language == "Arabic":
30
+ poem = generate_poem_arabic(text) #retrun the generated poem from the generate_poem_arabic function
31
+ sampling_rate, audio_data = text_to_speech_arabic(poem) #return the audio from the text_to_speech_arabic function
32
+ translated_text = translate_arabic_to_english(text) #return the translated poem from arabic to englsih, using translate_arabic_to_english function
33
+ image = generate_image_from_poem(translated_text) #return the image from the generate_image_from_poem function
34
+
35
+ return poem, (sampling_rate, audio_data), image
36
+
37
+ # Poem generation for Arabic
38
+ def generate_poem_arabic(text):
39
+ generated_text = pipe_ar(text, do_sample=True, max_length=96, top_k=50, top_p=1.0, temperature=1.0, num_return_sequences=1,
40
+ no_repeat_ngram_size = 3, return_full_text=True)[0]["generated_text"]
41
+ clean_text = generated_text.replace("-", "") #To get rid of the dashs generated by the model.
42
+ return clean_text
43
+
44
+ # Poem generation for English
45
+ def generate_poem_english(text):
46
+ generated_text = pipe_en(text, do_sample=True, max_length=50)[0]['generated_text']
47
+ clean_text = generated_text.replace("-", "") # Remove dashes generated by the model
48
+ clean_text = clean_text.replace("\\n", " ") # Replace newlines with a space
49
+ return clean_text
50
+
51
+ # Text-to-speech conversion for Arabic
52
+ def text_to_speech_arabic(text):
53
+ speech = synthesiser_arabic(text, forward_params={"speaker_embeddings": speaker_embedding_arabic})
54
+ audio_data = speech["audio"]
55
+ sampling_rate = speech["sampling_rate"]
56
+ return (sampling_rate, audio_data)
57
+
58
+ # Text-to-speech conversion for English
59
+ def text_to_speech_english(text):
60
+ speech = synthesiser_english(text, forward_params={"speaker_embeddings": speaker_embedding_english})
61
+ audio_data = speech["audio"]
62
+ sampling_rate = speech["sampling_rate"]
63
+ return (sampling_rate, audio_data)
64
+
65
+ #Image Function
66
+ def generate_image_from_poem(poem_text):
67
+ image = pipe_image(poem_text).images[0]
68
+ return image
69
+
70
+ #Translation Function from Arabic to English
71
+ def translate_arabic_to_english(text):
72
+ translated_text = pipe_translator(text)[0]['translation_text']
73
+ return translated_text
74
+
75
+ custom_css = """
76
+ body {
77
+ background-color: #f4f4f9;
78
+ color: #333;
79
+ }
80
+ .gradio-container {
81
+ border-radius: 10px;
82
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
83
+ background-color: #fff;
84
+ }
85
+ label {
86
+ color: #4A90E2;
87
+ font-weight: bold;
88
+ }
89
+
90
+ input[type="text"],
91
+ textarea {
92
+ border: 1px solid #4A90E2;
93
+ }
94
+ textarea {
95
+ height: 150px;
96
+ }
97
+
98
+ button {
99
+ background-color: #4A90E2;
100
+ color: #fff;
101
+ border-radius: 5px;
102
+ cursor: pointer;
103
+ }
104
+ button:hover {
105
+ background-color: #357ABD;
106
+ }
107
+
108
+ .dropdown {
109
+ border: 1px solid #4A90E2;
110
+ border-radius: 4px;
111
+ }
112
+
113
+ """
114
+
115
+ examples = [
116
+ #First parameter is for the dropdown menu, and the second parameter is for the starter of the poem
117
+ ["English", "The shining sun rises over the calm ocean"],
118
+ ["Arabic", "الورود تتفتح في الربيع"],
119
+ ["English", "The night sky is filled with stars and dreams"],
120
+ ["Arabic", "اشعة الشمس المشرقة"]
121
+ ]
122
+
123
+ my_model = gr.Interface(
124
+ fn=generate_poem, #The primary function that will recives the inputs (language and the starter of the poem)
125
+ inputs=[
126
+ gr.Dropdown(["English", "Arabic"], label="Select Language"), #Dropdown menu to select the language, either "English" or "Arabic" for the poem
127
+ gr.Textbox(label="Enter a sentence")], #Textbox where the user will input a sentence or phrase to generate the poem (starter of the peom)
128
+
129
+ outputs=[
130
+ gr.Textbox(label="Generated Poem", lines=10), # Textbox to display the generated poem
131
+ gr.Audio(label="Generated Audio", type="numpy"), #Audio output for the generated poem
132
+ gr.Image(label="Generated Image")], #Display an image generated from the starter of the peom
133
+
134
+ examples=examples, #Predefined examples to guide the user how to use the interface
135
+ css=custom_css #Applying CSS Custeom
136
+ )
137
+ my_model.launch()