Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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(
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
| 207 |
|
| 208 |
with gr.Group():
|
| 209 |
gr.Markdown("π *Story Theme*")
|
| 210 |
-
theme_input = gr.Dropdown(theme_options,
|
| 211 |
|
| 212 |
with gr.Group():
|
| 213 |
gr.Markdown("π *Language*")
|
| 214 |
-
language_input = gr.Dropdown(language_options,
|
| 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"
|
| 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 |
-
|
|
|
|
|
|
|
| 231 |
|
| 232 |
-
gr.HTML(
|
|
|
|
|
|
|
| 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 |
-
|
| 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()
|
|
|
|
|
|