Ansa12 commited on
Commit
8656728
Β·
verified Β·
1 Parent(s): 3ad1d16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -39
app.py CHANGED
@@ -4,16 +4,13 @@ import gradio as gr
4
  from gtts import gTTS
5
  import re
6
 
7
- # Get API key from Hugging Face Secrets
8
  groq_key = os.environ.get("Groq_API_Key")
9
  if not groq_key:
10
  raise ValueError("Groq_API_Key not found in environment variables. Please add it to your Hugging Face Space secrets.")
11
 
12
  client = Groq(api_key=groq_key)
13
 
14
- # -------------------------------------------------------------
15
- # STORY GENERATION LOGIC
16
- # -------------------------------------------------------------
17
  def generate_story(age, theme, language):
18
  system_msg = {
19
  "role": "system",
@@ -47,33 +44,23 @@ def generate_story(age, theme, language):
47
  return response.choices[0].message.content.strip()
48
 
49
 
50
- # -------------------------------------------------------------
51
- # TEXT TO SPEECH
52
- # -------------------------------------------------------------
53
- def story_to_speech(story, language_code='en'):
54
  tts = gTTS(text=story, lang=language_code)
55
  filename = "story.mp3"
56
  tts.save(filename)
57
  return filename
58
 
59
 
60
- # -------------------------------------------------------------
61
- # PROCESS STORY + AUDIO
62
- # -------------------------------------------------------------
63
  def create_story_interface(age, theme, language, tts_option):
64
  story = generate_story(age, theme, language)
65
-
66
- # Remove Markdown symbols
67
  story = re.sub(r'[*_#`~]', '', story)
68
 
69
- # Extract title from first line
70
  lines = story.split("\n", 1)
71
  if len(lines) > 1:
72
  title, rest_of_story = lines[0], lines[1]
73
  else:
74
  title, rest_of_story = lines[0], ""
75
 
76
- # Urdu RTL formatting
77
  if language.lower().startswith("u"):
78
  story_html = (
79
  f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:5px;">{title}</div>'
@@ -87,13 +74,11 @@ def create_story_interface(age, theme, language, tts_option):
87
  )
88
  lang_code = "en"
89
 
90
- # Generate audio file
91
  audio_file = story_to_speech(story, lang_code) if tts_option else None
92
 
93
  return story_html, audio_file
94
 
95
 
96
- # UI OPTIONS
97
  age_options = [str(i) for i in range(3, 13)]
98
  theme_options = [
99
  "Adventure", "Animals", "Fantasy", "Educational",
@@ -102,9 +87,7 @@ theme_options = [
102
  ]
103
  language_options = ["English", "Urdu"]
104
 
105
- # -------------------------------------------------------------
106
- # CUSTOM CSS
107
- # -------------------------------------------------------------
108
  custom_css = """
109
  @import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
110
  .gradio-container {
@@ -184,52 +167,57 @@ custom_css = """
184
  }
185
  """
186
 
187
- # -------------------------------------------------------------
188
- # BUILD GRADIO INTERFACE
189
- # -------------------------------------------------------------
190
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
191
 
192
- # Inject CSS manually
193
  gr.HTML(f"<style>{custom_css}</style>")
194
 
195
- gr.HTML('<div class="main-header">🌈 StoryTime AIπŸŽͺ</div>')
196
- gr.HTML('<div class="subtitle">🎯 Create amazing stories that spark imagination and teach valuable lessons!</div>')
 
 
 
 
 
197
 
198
  with gr.Row():
199
- # LEFT PANEL
200
  with gr.Column(scale=1, min_width=300):
201
  with gr.Group(elem_classes="control-panel"):
202
  gr.Markdown("### 🎨 Story Settings")
203
 
204
  with gr.Group():
205
  gr.Markdown("πŸ‘Ά *Child's Age*")
206
- age_input = gr.Dropdown(age_options, label="", value="5", show_label=False)
207
 
208
  with gr.Group():
209
  gr.Markdown("🎭 *Story Theme*")
210
- theme_input = gr.Dropdown(theme_options, label="", value="Adventure", show_label=False)
211
 
212
  with gr.Group():
213
  gr.Markdown("🌍 *Language*")
214
- language_input = gr.Dropdown(language_options, label="", value="English", show_label=False)
215
 
216
  with gr.Group():
217
  gr.Markdown("πŸ”Š *Audio Options*")
218
  tts_input = gr.Checkbox(label="🎡 Include Audio Story", value=True)
219
 
220
- generate_btn = gr.Button("✨ Create Magical Story! ✨", elem_classes="generate-btn", size="lg")
221
 
222
- # RIGHT PANEL
223
  with gr.Column(scale=2):
224
  with gr.Group(elem_classes="story-container"):
 
225
  story_output = gr.Markdown(
226
  "Your story will appear here! Choose settings and click the magic button! πŸŽ‡",
227
  show_label=False
228
  )
229
 
230
- audio_output = gr.Audio(label="Audio Story", elem_classes="audio-player", show_label=False)
 
 
231
 
232
- gr.HTML('<div class="footer">Made with ❀ for young readers everywhere! | Watch your stories come to life! ✨</div>')
 
 
233
 
234
  generate_btn.click(
235
  create_story_interface,
@@ -237,8 +225,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
237
  [story_output, audio_output]
238
  )
239
 
240
- # -------------------------------------------------------------
241
- # LAUNCH APP
242
- # -------------------------------------------------------------
243
- if __name__ == "__main__":
244
- iface.launch()
 
4
  from gtts import gTTS
5
  import re
6
 
 
7
  groq_key = os.environ.get("Groq_API_Key")
8
  if not groq_key:
9
  raise ValueError("Groq_API_Key not found in environment variables. Please add it to your Hugging Face Space secrets.")
10
 
11
  client = Groq(api_key=groq_key)
12
 
13
+
 
 
14
  def generate_story(age, theme, language):
15
  system_msg = {
16
  "role": "system",
 
44
  return response.choices[0].message.content.strip()
45
 
46
 
47
+ def story_to_speech(story, language_code="en"):
 
 
 
48
  tts = gTTS(text=story, lang=language_code)
49
  filename = "story.mp3"
50
  tts.save(filename)
51
  return filename
52
 
53
 
 
 
 
54
  def create_story_interface(age, theme, language, tts_option):
55
  story = generate_story(age, theme, language)
 
 
56
  story = re.sub(r'[*_#`~]', '', story)
57
 
 
58
  lines = story.split("\n", 1)
59
  if len(lines) > 1:
60
  title, rest_of_story = lines[0], lines[1]
61
  else:
62
  title, rest_of_story = lines[0], ""
63
 
 
64
  if language.lower().startswith("u"):
65
  story_html = (
66
  f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:5px;">{title}</div>'
 
74
  )
75
  lang_code = "en"
76
 
 
77
  audio_file = story_to_speech(story, lang_code) if tts_option else None
78
 
79
  return story_html, audio_file
80
 
81
 
 
82
  age_options = [str(i) for i in range(3, 13)]
83
  theme_options = [
84
  "Adventure", "Animals", "Fantasy", "Educational",
 
87
  ]
88
  language_options = ["English", "Urdu"]
89
 
90
+
 
 
91
  custom_css = """
92
  @import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
93
  .gradio-container {
 
167
  }
168
  """
169
 
170
+
 
 
171
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
172
 
 
173
  gr.HTML(f"<style>{custom_css}</style>")
174
 
175
+ gr.HTML("""
176
+ <div class="main-header">🌈 StoryTime AIπŸŽͺ</div>
177
+ """)
178
+
179
+ gr.HTML("""
180
+ <div class="subtitle">🎯 Create amazing stories that spark imagination and teach valuable lessons!</div>
181
+ """)
182
 
183
  with gr.Row():
 
184
  with gr.Column(scale=1, min_width=300):
185
  with gr.Group(elem_classes="control-panel"):
186
  gr.Markdown("### 🎨 Story Settings")
187
 
188
  with gr.Group():
189
  gr.Markdown("πŸ‘Ά *Child's Age*")
190
+ age_input = gr.Dropdown(age_options, value="5", show_label=False)
191
 
192
  with gr.Group():
193
  gr.Markdown("🎭 *Story Theme*")
194
+ theme_input = gr.Dropdown(theme_options, value="Adventure", show_label=False)
195
 
196
  with gr.Group():
197
  gr.Markdown("🌍 *Language*")
198
+ language_input = gr.Dropdown(language_options, value="English", show_label=False)
199
 
200
  with gr.Group():
201
  gr.Markdown("πŸ”Š *Audio Options*")
202
  tts_input = gr.Checkbox(label="🎡 Include Audio Story", value=True)
203
 
204
+ generate_btn = gr.Button("✨ Create Magical Story! ✨", elem_classes="generate-btn")
205
 
 
206
  with gr.Column(scale=2):
207
  with gr.Group(elem_classes="story-container"):
208
+ gr.Markdown("### πŸ“– Your Magical Story")
209
  story_output = gr.Markdown(
210
  "Your story will appear here! Choose settings and click the magic button! πŸŽ‡",
211
  show_label=False
212
  )
213
 
214
+ with gr.Group():
215
+ gr.Markdown("### 🎧 Listen to Your Story")
216
+ audio_output = gr.Audio(elem_classes="audio-player", show_label=False)
217
 
218
+ gr.HTML("""
219
+ <div class="footer">Made with ❀ for young readers everywhere! | Watch your stories come to life! ✨</div>
220
+ """)
221
 
222
  generate_btn.click(
223
  create_story_interface,
 
225
  [story_output, audio_output]
226
  )
227
 
228
+
229
+ if _name_ == "_main_":
230
+ Β Β iface.launch()