Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,13 +4,16 @@ import gradio as gr
|
|
| 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,6 +47,9 @@ def generate_story(age, theme, language):
|
|
| 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"
|
|
@@ -51,6 +57,9 @@ def story_to_speech(story, language_code="en"):
|
|
| 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)
|
|
@@ -75,10 +84,12 @@ def create_story_interface(age, theme, language, tts_option):
|
|
| 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",
|
|
@@ -88,6 +99,9 @@ theme_options = [
|
|
| 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 {
|
|
@@ -168,19 +182,19 @@ custom_css = """
|
|
| 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="
|
| 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")
|
|
@@ -203,21 +217,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 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 |
-
|
| 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,6 +235,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 225 |
[story_output, audio_output]
|
| 226 |
)
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
| 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 |
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"
|
|
|
|
| 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 |
story = re.sub(r'[*_#`~]', '', story)
|
|
|
|
| 84 |
lang_code = "en"
|
| 85 |
|
| 86 |
audio_file = story_to_speech(story, lang_code) if tts_option else None
|
|
|
|
| 87 |
return story_html, audio_file
|
| 88 |
|
| 89 |
|
| 90 |
+
# -------------------------------------------------------------
|
| 91 |
+
# UI OPTIONS
|
| 92 |
+
# -------------------------------------------------------------
|
| 93 |
age_options = [str(i) for i in range(3, 13)]
|
| 94 |
theme_options = [
|
| 95 |
"Adventure", "Animals", "Fantasy", "Educational",
|
|
|
|
| 99 |
language_options = ["English", "Urdu"]
|
| 100 |
|
| 101 |
|
| 102 |
+
# -------------------------------------------------------------
|
| 103 |
+
# CUSTOM CSS
|
| 104 |
+
# -------------------------------------------------------------
|
| 105 |
custom_css = """
|
| 106 |
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
|
| 107 |
.gradio-container {
|
|
|
|
| 182 |
"""
|
| 183 |
|
| 184 |
|
| 185 |
+
# -------------------------------------------------------------
|
| 186 |
+
# BUILD GRADIO INTERFACE
|
| 187 |
+
# -------------------------------------------------------------
|
| 188 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 189 |
|
| 190 |
+
# Inject CSS manually
|
| 191 |
gr.HTML(f"<style>{custom_css}</style>")
|
| 192 |
|
| 193 |
+
gr.HTML('<div class="main-header">🌈 StoryTime AI🎪</div>')
|
| 194 |
+
gr.HTML('<div class="subtitle">🎯 Create amazing stories that spark imagination and teach valuable lessons!</div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
with gr.Row():
|
| 197 |
+
# LEFT PANEL
|
| 198 |
with gr.Column(scale=1, min_width=300):
|
| 199 |
with gr.Group(elem_classes="control-panel"):
|
| 200 |
gr.Markdown("### 🎨 Story Settings")
|
|
|
|
| 217 |
|
| 218 |
generate_btn = gr.Button("✨ Create Magical Story! ✨", elem_classes="generate-btn")
|
| 219 |
|
| 220 |
+
# RIGHT PANEL
|
| 221 |
with gr.Column(scale=2):
|
| 222 |
with gr.Group(elem_classes="story-container"):
|
|
|
|
| 223 |
story_output = gr.Markdown(
|
| 224 |
"Your story will appear here! Choose settings and click the magic button! 🎇",
|
| 225 |
show_label=False
|
| 226 |
)
|
| 227 |
|
| 228 |
+
audio_output = gr.Audio(elem_classes="audio-player", show_label=False)
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
gr.HTML('<div class="footer">Made with ❤ for young readers everywhere! | Watch your stories come to life! ✨</div>')
|
|
|
|
|
|
|
| 231 |
|
| 232 |
generate_btn.click(
|
| 233 |
create_story_interface,
|
|
|
|
| 235 |
[story_output, audio_output]
|
| 236 |
)
|
| 237 |
|
| 238 |
+
# -------------------------------------------------------------
|
| 239 |
+
# LAUNCH APP
|
| 240 |
+
# -------------------------------------------------------------
|
| 241 |
+
if __name__ == "__main__":
|
| 242 |
+
iface.launch()
|