Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,14 +60,12 @@ def generate_story(age, theme, language):
|
|
| 60 |
|
| 61 |
def story_to_speech(story, language_code='en'):
|
| 62 |
try:
|
| 63 |
-
# For Hugging Face deployment, we'll use a simple audio generation
|
| 64 |
from gtts import gTTS
|
| 65 |
tts = gTTS(text=story, lang=language_code)
|
| 66 |
filename = "story.mp3"
|
| 67 |
tts.save(filename)
|
| 68 |
return filename
|
| 69 |
except Exception as e:
|
| 70 |
-
# Fallback: Return a message about audio being unavailable
|
| 71 |
print(f"TTS Error: {e}")
|
| 72 |
return None
|
| 73 |
|
|
@@ -77,27 +75,12 @@ def create_story_interface(age, theme, language, tts_option):
|
|
| 77 |
# Remove Markdown symbols (*, #, _, etc.) to prevent asterisks showing up
|
| 78 |
story = re.sub(r'[*_#`~]', '', story)
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
lines = story.split("\n", 1)
|
| 82 |
-
if len(lines) > 1:
|
| 83 |
-
title, rest_of_story = lines[0], lines[1]
|
| 84 |
-
else:
|
| 85 |
-
title, rest_of_story = lines[0], ""
|
| 86 |
-
|
| 87 |
if language.lower().startswith("u"): # Urdu
|
| 88 |
-
story_html = (
|
| 89 |
-
f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:5px;">{title}</div>'
|
| 90 |
-
f'<div style="font-size:1.2em; direction:rtl; text-align:right;">{rest_of_story}</div>'
|
| 91 |
-
)
|
| 92 |
lang_code = "ur"
|
| 93 |
else: # English
|
| 94 |
-
story_html = (
|
| 95 |
-
f'<div style="text-align:center; font-size:1.3em; direction:ltr; margin-bottom:5px;">{title}</div>'
|
| 96 |
-
f'<div style="font-size:1.2em; direction:ltr; text-align:left;">{rest_of_story}</div>'
|
| 97 |
-
)
|
| 98 |
lang_code = "en"
|
| 99 |
|
| 100 |
-
# Generate audio if TTS is enabled
|
| 101 |
audio_file = story_to_speech(story, lang_code) if tts_option else None
|
| 102 |
|
| 103 |
return story, audio_file
|
|
@@ -114,119 +97,29 @@ theme_options = [
|
|
| 114 |
|
| 115 |
language_options = ["English", "Urdu"]
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
<style>
|
| 120 |
-
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
|
| 121 |
-
|
| 122 |
-
.gradio-container {
|
| 123 |
-
font-family: 'Comic Neue', cursive !important;
|
| 124 |
-
background: linear-gradient(135deg, #ffafbd, #ffc3a0) !important;
|
| 125 |
-
padding: 20px;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
.main-header {
|
| 129 |
-
text-align: center;
|
| 130 |
-
font-weight: bold !important;
|
| 131 |
-
font-size: 3.2em !important;
|
| 132 |
-
margin: 20px 0;
|
| 133 |
-
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 134 |
-
color: #12979B;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
.story-container {
|
| 138 |
-
background: #fff5ee;
|
| 139 |
-
border-radius: 20px;
|
| 140 |
-
padding: 25px;
|
| 141 |
-
border: 5px solid #FFD166;
|
| 142 |
-
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
|
| 143 |
-
min-height: 400px;
|
| 144 |
-
margin: 20px 0;
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
.control-panel {
|
| 148 |
-
background: #fff5ee;
|
| 149 |
-
border-radius: 15px;
|
| 150 |
-
padding: 20px;
|
| 151 |
-
border: 3px dashed #4ECDC4;
|
| 152 |
-
margin: 20px 0;
|
| 153 |
-
}
|
| 154 |
-
|
| 155 |
-
.generate-btn {
|
| 156 |
-
background: linear-gradient(45deg, #FF6B6B, #FF9E7D) !important;
|
| 157 |
-
border: none !important;
|
| 158 |
-
border-radius: 50px !important;
|
| 159 |
-
padding: 15px 30px !important;
|
| 160 |
-
font-size: 1.2em !important;
|
| 161 |
-
font-weight: bold !important;
|
| 162 |
-
color: white !important;
|
| 163 |
-
margin: 20px 0;
|
| 164 |
-
width: 100%;
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
.generate-btn:hover {
|
| 168 |
-
transform: scale(1.05);
|
| 169 |
-
box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
.dropdown, .checkbox {
|
| 173 |
-
background: white !important;
|
| 174 |
-
border-radius: 10px !important;
|
| 175 |
-
border: 2px solid #96CEB4 !important;
|
| 176 |
-
margin: 10px 0;
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
.audio-player {
|
| 180 |
-
border-radius: 15px !important;
|
| 181 |
-
margin: 20px 0;
|
| 182 |
-
}
|
| 183 |
-
|
| 184 |
-
.subtitle {
|
| 185 |
-
text-align: center;
|
| 186 |
-
font-size: 1.5em !important;
|
| 187 |
-
font-weight: 600;
|
| 188 |
-
color: white;
|
| 189 |
-
margin: 20px 0;
|
| 190 |
-
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 191 |
-
}
|
| 192 |
-
|
| 193 |
-
.footer {
|
| 194 |
-
text-align: center;
|
| 195 |
-
margin: 20px 0;
|
| 196 |
-
color: #666;
|
| 197 |
-
font-style: italic;
|
| 198 |
-
font-size: 1.1em !important;
|
| 199 |
-
}
|
| 200 |
-
</style>
|
| 201 |
-
"""
|
| 202 |
-
|
| 203 |
-
# Create the interface without css parameter
|
| 204 |
-
with gr.Blocks() as iface:
|
| 205 |
-
# Inject custom CSS as HTML
|
| 206 |
-
gr.HTML(custom_css)
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
</div>
|
| 218 |
-
""")
|
| 219 |
|
| 220 |
# API Key warning
|
| 221 |
if not GROQ_API_KEY:
|
| 222 |
gr.Warning("β οΈ Please set GROQ_API_KEY in Hugging Face Space secrets to use this app")
|
| 223 |
|
| 224 |
with gr.Row():
|
|
|
|
| 225 |
with gr.Column(scale=1, min_width=300):
|
|
|
|
|
|
|
| 226 |
with gr.Group():
|
| 227 |
-
gr.HTML('<div class="control-panel">')
|
| 228 |
-
gr.Markdown("### π¨ Story Settings")
|
| 229 |
-
|
| 230 |
age_input = gr.Dropdown(
|
| 231 |
age_options,
|
| 232 |
label="πΆ Child's Age",
|
|
@@ -251,23 +144,23 @@ with gr.Blocks() as iface:
|
|
| 251 |
tts_input = gr.Checkbox(
|
| 252 |
label="π Include Audio Story",
|
| 253 |
value=True,
|
| 254 |
-
info="Listen to the story
|
| 255 |
)
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
| 261 |
|
|
|
|
| 262 |
with gr.Column(scale=2):
|
| 263 |
with gr.Group():
|
| 264 |
-
gr.HTML('<div class="story-container">')
|
| 265 |
gr.Markdown("### π Your Magical Story")
|
| 266 |
story_output = gr.Markdown(
|
| 267 |
"Your story will appear here! Choose settings and click the magic button! π",
|
| 268 |
show_label=False
|
| 269 |
)
|
| 270 |
-
gr.HTML('</div>')
|
| 271 |
|
| 272 |
with gr.Group():
|
| 273 |
gr.Markdown("### π§ Listen to Your Story")
|
|
@@ -276,11 +169,14 @@ with gr.Blocks() as iface:
|
|
| 276 |
show_label=False
|
| 277 |
)
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
generate_btn.click(
|
| 286 |
create_story_interface,
|
|
|
|
| 60 |
|
| 61 |
def story_to_speech(story, language_code='en'):
|
| 62 |
try:
|
|
|
|
| 63 |
from gtts import gTTS
|
| 64 |
tts = gTTS(text=story, lang=language_code)
|
| 65 |
filename = "story.mp3"
|
| 66 |
tts.save(filename)
|
| 67 |
return filename
|
| 68 |
except Exception as e:
|
|
|
|
| 69 |
print(f"TTS Error: {e}")
|
| 70 |
return None
|
| 71 |
|
|
|
|
| 75 |
# Remove Markdown symbols (*, #, _, etc.) to prevent asterisks showing up
|
| 76 |
story = re.sub(r'[*_#`~]', '', story)
|
| 77 |
|
| 78 |
+
# Generate audio if TTS is enabled
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if language.lower().startswith("u"): # Urdu
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
lang_code = "ur"
|
| 81 |
else: # English
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
lang_code = "en"
|
| 83 |
|
|
|
|
| 84 |
audio_file = story_to_speech(story, lang_code) if tts_option else None
|
| 85 |
|
| 86 |
return story, audio_file
|
|
|
|
| 97 |
|
| 98 |
language_options = ["English", "Urdu"]
|
| 99 |
|
| 100 |
+
# Create the interface with simple styling
|
| 101 |
+
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# Header
|
| 104 |
+
gr.Markdown(
|
| 105 |
+
"""
|
| 106 |
+
<div style="text-align: center; background: linear-gradient(135deg, #ffafbd, #ffc3a0); padding: 30px; border-radius: 15px; margin-bottom: 20px;">
|
| 107 |
+
<h1 style="color: #12979B; margin: 0; font-size: 3em;">π StoryTime AI πͺ</h1>
|
| 108 |
+
<p style="color: white; font-size: 1.5em; margin: 10px 0 0 0;">π― Create amazing stories that spark imagination and teach valuable lessons!</p>
|
| 109 |
+
</div>
|
| 110 |
+
"""
|
| 111 |
+
)
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# API Key warning
|
| 114 |
if not GROQ_API_KEY:
|
| 115 |
gr.Warning("β οΈ Please set GROQ_API_KEY in Hugging Face Space secrets to use this app")
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
+
# Left Column - Controls
|
| 119 |
with gr.Column(scale=1, min_width=300):
|
| 120 |
+
gr.Markdown("### π¨ Story Settings")
|
| 121 |
+
|
| 122 |
with gr.Group():
|
|
|
|
|
|
|
|
|
|
| 123 |
age_input = gr.Dropdown(
|
| 124 |
age_options,
|
| 125 |
label="πΆ Child's Age",
|
|
|
|
| 144 |
tts_input = gr.Checkbox(
|
| 145 |
label="π Include Audio Story",
|
| 146 |
value=True,
|
| 147 |
+
info="Listen to the story"
|
| 148 |
)
|
| 149 |
|
| 150 |
+
generate_btn = gr.Button(
|
| 151 |
+
"β¨ Create Magical Story! β¨",
|
| 152 |
+
size="lg",
|
| 153 |
+
variant="primary"
|
| 154 |
+
)
|
| 155 |
|
| 156 |
+
# Right Column - Output
|
| 157 |
with gr.Column(scale=2):
|
| 158 |
with gr.Group():
|
|
|
|
| 159 |
gr.Markdown("### π Your Magical Story")
|
| 160 |
story_output = gr.Markdown(
|
| 161 |
"Your story will appear here! Choose settings and click the magic button! π",
|
| 162 |
show_label=False
|
| 163 |
)
|
|
|
|
| 164 |
|
| 165 |
with gr.Group():
|
| 166 |
gr.Markdown("### π§ Listen to Your Story")
|
|
|
|
| 169 |
show_label=False
|
| 170 |
)
|
| 171 |
|
| 172 |
+
# Footer
|
| 173 |
+
gr.Markdown(
|
| 174 |
+
"""
|
| 175 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; background: #f5f5f5; border-radius: 10px;">
|
| 176 |
+
<p style="color: #666; font-style: italic; margin: 0;">Made with β€οΈ for young readers everywhere! | Watch your stories come to life! β¨</p>
|
| 177 |
+
</div>
|
| 178 |
+
"""
|
| 179 |
+
)
|
| 180 |
|
| 181 |
generate_btn.click(
|
| 182 |
create_story_interface,
|