Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,13 +5,16 @@ from gtts import gTTS
|
|
| 5 |
import re
|
| 6 |
|
| 7 |
# Get API key from Hugging Face Secrets
|
| 8 |
-
groq_key = os.environ.get("
|
| 9 |
|
| 10 |
if not groq_key:
|
| 11 |
-
raise ValueError("
|
| 12 |
|
| 13 |
client = Groq(api_key=groq_key)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
def generate_story(age, theme, language):
|
| 16 |
system_msg = {
|
| 17 |
"role": "system",
|
|
@@ -44,43 +47,54 @@ def generate_story(age, theme, language):
|
|
| 44 |
|
| 45 |
return response.choices[0].message.content.strip()
|
| 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 |
def create_story_interface(age, theme, language, tts_option):
|
| 54 |
story = generate_story(age, theme, language)
|
| 55 |
|
| 56 |
# Remove Markdown symbols
|
| 57 |
story = re.sub(r'[*_#`~]', '', story)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
lines = story.split("\n", 1)
|
| 61 |
if len(lines) > 1:
|
| 62 |
title, rest_of_story = lines[0], lines[1]
|
| 63 |
else:
|
| 64 |
title, rest_of_story = lines[0], ""
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
story_html = (
|
| 68 |
f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:5px;">{title}</div>'
|
| 69 |
f'<div style="font-size:1.2em; direction:rtl; text-align:right;">{rest_of_story}</div>'
|
| 70 |
)
|
| 71 |
lang_code = "ur"
|
| 72 |
-
else:
|
| 73 |
story_html = (
|
| 74 |
f'<div style="text-align:center; font-size:1.3em; direction:ltr; margin-bottom:5px;">{title}</div>'
|
| 75 |
f'<div style="font-size:1.2em; direction:ltr; text-align:left;">{rest_of_story}</div>'
|
| 76 |
)
|
| 77 |
lang_code = "en"
|
| 78 |
|
| 79 |
-
# Generate audio
|
| 80 |
audio_file = story_to_speech(story, lang_code) if tts_option else None
|
| 81 |
|
| 82 |
return story_html, audio_file
|
| 83 |
|
|
|
|
|
|
|
| 84 |
age_options = [str(i) for i in range(3, 13)]
|
| 85 |
theme_options = [
|
| 86 |
"Adventure", "Animals", "Fantasy", "Educational",
|
|
@@ -89,17 +103,18 @@ theme_options = [
|
|
| 89 |
]
|
| 90 |
language_options = ["English", "Urdu"]
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
custom_css = """
|
| 94 |
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
|
| 95 |
-
|
| 96 |
.gradio-container {
|
| 97 |
font-family: 'Comic Neue', cursive !important;
|
| 98 |
background: linear-gradient(135deg, #ffafbd, #ffc3a0) !important;
|
| 99 |
padding-top: 10px !important;
|
| 100 |
margin-top: -70px !important;
|
| 101 |
}
|
| 102 |
-
|
| 103 |
.main-header {
|
| 104 |
text-align: center;
|
| 105 |
font-weight: bold !important;
|
|
@@ -109,7 +124,6 @@ custom_css = """
|
|
| 109 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 110 |
color: #12979B;
|
| 111 |
}
|
| 112 |
-
|
| 113 |
.story-container {
|
| 114 |
background:#fff5ee;
|
| 115 |
border-radius: 20px;
|
|
@@ -119,9 +133,8 @@ custom_css = """
|
|
| 119 |
min-height: 400px;
|
| 120 |
margin-top: -20px !important;
|
| 121 |
}
|
| 122 |
-
|
| 123 |
.control-panel {
|
| 124 |
-
background: #fff5ee
|
| 125 |
border-radius: 15px;
|
| 126 |
padding: 20px;
|
| 127 |
border: 3px dashed #4ECDC4;
|
|
@@ -129,7 +142,6 @@ custom_css = """
|
|
| 129 |
font-weight: 600;
|
| 130 |
margin-top: -20px !important;
|
| 131 |
}
|
| 132 |
-
|
| 133 |
.generate-btn {
|
| 134 |
background: linear-gradient(45deg, #FF6B6B, #FF9E7D) !important;
|
| 135 |
border: none !important;
|
|
@@ -140,23 +152,19 @@ custom_css = """
|
|
| 140 |
color: white !important;
|
| 141 |
margin-top: 20px !important;
|
| 142 |
}
|
| 143 |
-
|
| 144 |
.generate-btn:hover {
|
| 145 |
transform: scale(1.05);
|
| 146 |
box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
|
| 147 |
}
|
| 148 |
-
|
| 149 |
.dropdown, .checkbox {
|
| 150 |
background: white !important;
|
| 151 |
border-radius: 10px !important;
|
| 152 |
border: 2px solid #96CEB4 !important;
|
| 153 |
}
|
| 154 |
-
|
| 155 |
.audio-player {
|
| 156 |
border-radius: 15px !important;
|
| 157 |
margin-top: 20px;
|
| 158 |
}
|
| 159 |
-
|
| 160 |
.subtitle {
|
| 161 |
text-align: left;
|
| 162 |
font-size: 1.5em !important;
|
|
@@ -166,15 +174,6 @@ custom_css = """
|
|
| 166 |
margin-bottom: 20px;
|
| 167 |
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 168 |
}
|
| 169 |
-
|
| 170 |
-
.control-panel .gr-Markdown {
|
| 171 |
-
font-size: 1.5em !important;
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
.story-container .gr-Markdown {
|
| 175 |
-
font-size: 1.5em !important;
|
| 176 |
-
}
|
| 177 |
-
|
| 178 |
.footer {
|
| 179 |
text-align: center;
|
| 180 |
margin-top: 20px;
|
|
@@ -182,13 +181,20 @@ custom_css = """
|
|
| 182 |
font-style: italic;
|
| 183 |
font-size: 1.5em !important;
|
| 184 |
}
|
| 185 |
-
|
| 186 |
.main-header + div {
|
| 187 |
margin-top: -145px !important;
|
| 188 |
}
|
| 189 |
"""
|
| 190 |
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
gr.HTML("""
|
| 193 |
<div class="main-header">
|
| 194 |
🌈 StoryTime AI🎪
|
|
@@ -202,28 +208,30 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 202 |
""")
|
| 203 |
|
| 204 |
with gr.Row():
|
|
|
|
| 205 |
with gr.Column(scale=1, min_width=300):
|
| 206 |
with gr.Group(elem_classes="control-panel"):
|
| 207 |
gr.Markdown("### 🎨 Story Settings")
|
| 208 |
|
| 209 |
with gr.Group():
|
| 210 |
-
gr.Markdown("👶
|
| 211 |
age_input = gr.Dropdown(age_options, label="", value="5", show_label=False)
|
| 212 |
|
| 213 |
with gr.Group():
|
| 214 |
-
gr.Markdown("🎭
|
| 215 |
theme_input = gr.Dropdown(theme_options, label="", value="Adventure", show_label=False)
|
| 216 |
|
| 217 |
with gr.Group():
|
| 218 |
-
gr.Markdown("🌍
|
| 219 |
language_input = gr.Dropdown(language_options, label="", value="English", show_label=False)
|
| 220 |
|
| 221 |
with gr.Group():
|
| 222 |
-
gr.Markdown("🔊
|
| 223 |
tts_input = gr.Checkbox(label="🎵 Include Audio Story", value=True)
|
| 224 |
|
| 225 |
generate_btn = gr.Button("✨ Create Magical Story! ✨", elem_classes="generate-btn", size="lg")
|
| 226 |
|
|
|
|
| 227 |
with gr.Column(scale=2):
|
| 228 |
with gr.Group(elem_classes="story-container"):
|
| 229 |
gr.Markdown("### 📖 Your Magical Story")
|
|
@@ -242,7 +250,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 242 |
|
| 243 |
gr.HTML("""
|
| 244 |
<div class="footer">
|
| 245 |
-
Made with
|
| 246 |
</div>
|
| 247 |
""")
|
| 248 |
|
|
@@ -251,5 +259,10 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 251 |
[age_input, theme_input, language_input, tts_input],
|
| 252 |
[story_output, audio_output]
|
| 253 |
)
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import re
|
| 6 |
|
| 7 |
# Get API key from Hugging Face Secrets
|
| 8 |
+
groq_key = os.environ.get("GROQ_API_KEY")
|
| 9 |
|
| 10 |
if not groq_key:
|
| 11 |
+
raise ValueError("GROQ_API_KEY not found in environment variables. Please add it to your Hugging Face Space secrets.")
|
| 12 |
|
| 13 |
client = Groq(api_key=groq_key)
|
| 14 |
|
| 15 |
+
# -------------------------------------------------------------
|
| 16 |
+
# STORY GENERATION LOGIC
|
| 17 |
+
# -------------------------------------------------------------
|
| 18 |
def generate_story(age, theme, language):
|
| 19 |
system_msg = {
|
| 20 |
"role": "system",
|
|
|
|
| 47 |
|
| 48 |
return response.choices[0].message.content.strip()
|
| 49 |
|
| 50 |
+
|
| 51 |
+
# -------------------------------------------------------------
|
| 52 |
+
# TEXT TO SPEECH
|
| 53 |
+
# -------------------------------------------------------------
|
| 54 |
def story_to_speech(story, language_code='en'):
|
| 55 |
tts = gTTS(text=story, lang=language_code)
|
| 56 |
filename = "story.mp3"
|
| 57 |
tts.save(filename)
|
| 58 |
return filename
|
| 59 |
|
| 60 |
+
|
| 61 |
+
# -------------------------------------------------------------
|
| 62 |
+
# PROCESS STORY + AUDIO
|
| 63 |
+
# -------------------------------------------------------------
|
| 64 |
def create_story_interface(age, theme, language, tts_option):
|
| 65 |
story = generate_story(age, theme, language)
|
| 66 |
|
| 67 |
# Remove Markdown symbols
|
| 68 |
story = re.sub(r'[*_#`~]', '', story)
|
| 69 |
|
| 70 |
+
# Extract title from first line
|
| 71 |
lines = story.split("\n", 1)
|
| 72 |
if len(lines) > 1:
|
| 73 |
title, rest_of_story = lines[0], lines[1]
|
| 74 |
else:
|
| 75 |
title, rest_of_story = lines[0], ""
|
| 76 |
|
| 77 |
+
# Urdu RTL formatting
|
| 78 |
+
if language.lower().startswith("u"):
|
| 79 |
story_html = (
|
| 80 |
f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:5px;">{title}</div>'
|
| 81 |
f'<div style="font-size:1.2em; direction:rtl; text-align:right;">{rest_of_story}</div>'
|
| 82 |
)
|
| 83 |
lang_code = "ur"
|
| 84 |
+
else:
|
| 85 |
story_html = (
|
| 86 |
f'<div style="text-align:center; font-size:1.3em; direction:ltr; margin-bottom:5px;">{title}</div>'
|
| 87 |
f'<div style="font-size:1.2em; direction:ltr; text-align:left;">{rest_of_story}</div>'
|
| 88 |
)
|
| 89 |
lang_code = "en"
|
| 90 |
|
| 91 |
+
# Generate audio file
|
| 92 |
audio_file = story_to_speech(story, lang_code) if tts_option else None
|
| 93 |
|
| 94 |
return story_html, audio_file
|
| 95 |
|
| 96 |
+
|
| 97 |
+
# UI OPTIONS
|
| 98 |
age_options = [str(i) for i in range(3, 13)]
|
| 99 |
theme_options = [
|
| 100 |
"Adventure", "Animals", "Fantasy", "Educational",
|
|
|
|
| 103 |
]
|
| 104 |
language_options = ["English", "Urdu"]
|
| 105 |
|
| 106 |
+
|
| 107 |
+
# -------------------------------------------------------------
|
| 108 |
+
# CUSTOM CSS
|
| 109 |
+
# -------------------------------------------------------------
|
| 110 |
custom_css = """
|
| 111 |
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
|
|
|
|
| 112 |
.gradio-container {
|
| 113 |
font-family: 'Comic Neue', cursive !important;
|
| 114 |
background: linear-gradient(135deg, #ffafbd, #ffc3a0) !important;
|
| 115 |
padding-top: 10px !important;
|
| 116 |
margin-top: -70px !important;
|
| 117 |
}
|
|
|
|
| 118 |
.main-header {
|
| 119 |
text-align: center;
|
| 120 |
font-weight: bold !important;
|
|
|
|
| 124 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 125 |
color: #12979B;
|
| 126 |
}
|
|
|
|
| 127 |
.story-container {
|
| 128 |
background:#fff5ee;
|
| 129 |
border-radius: 20px;
|
|
|
|
| 133 |
min-height: 400px;
|
| 134 |
margin-top: -20px !important;
|
| 135 |
}
|
|
|
|
| 136 |
.control-panel {
|
| 137 |
+
background: #fff5ee;
|
| 138 |
border-radius: 15px;
|
| 139 |
padding: 20px;
|
| 140 |
border: 3px dashed #4ECDC4;
|
|
|
|
| 142 |
font-weight: 600;
|
| 143 |
margin-top: -20px !important;
|
| 144 |
}
|
|
|
|
| 145 |
.generate-btn {
|
| 146 |
background: linear-gradient(45deg, #FF6B6B, #FF9E7D) !important;
|
| 147 |
border: none !important;
|
|
|
|
| 152 |
color: white !important;
|
| 153 |
margin-top: 20px !important;
|
| 154 |
}
|
|
|
|
| 155 |
.generate-btn:hover {
|
| 156 |
transform: scale(1.05);
|
| 157 |
box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
|
| 158 |
}
|
|
|
|
| 159 |
.dropdown, .checkbox {
|
| 160 |
background: white !important;
|
| 161 |
border-radius: 10px !important;
|
| 162 |
border: 2px solid #96CEB4 !important;
|
| 163 |
}
|
|
|
|
| 164 |
.audio-player {
|
| 165 |
border-radius: 15px !important;
|
| 166 |
margin-top: 20px;
|
| 167 |
}
|
|
|
|
| 168 |
.subtitle {
|
| 169 |
text-align: left;
|
| 170 |
font-size: 1.5em !important;
|
|
|
|
| 174 |
margin-bottom: 20px;
|
| 175 |
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 176 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
.footer {
|
| 178 |
text-align: center;
|
| 179 |
margin-top: 20px;
|
|
|
|
| 181 |
font-style: italic;
|
| 182 |
font-size: 1.5em !important;
|
| 183 |
}
|
|
|
|
| 184 |
.main-header + div {
|
| 185 |
margin-top: -145px !important;
|
| 186 |
}
|
| 187 |
"""
|
| 188 |
|
| 189 |
+
|
| 190 |
+
# -------------------------------------------------------------
|
| 191 |
+
# BUILD GRADIO INTERFACE (Correct for Gradio 4.x)
|
| 192 |
+
# -------------------------------------------------------------
|
| 193 |
+
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 194 |
+
|
| 195 |
+
# Inject CSS manually (Gradio 4.x)
|
| 196 |
+
gr.HTML(f"<style>{custom_css}</style>")
|
| 197 |
+
|
| 198 |
gr.HTML("""
|
| 199 |
<div class="main-header">
|
| 200 |
🌈 StoryTime AI🎪
|
|
|
|
| 208 |
""")
|
| 209 |
|
| 210 |
with gr.Row():
|
| 211 |
+
# LEFT PANEL
|
| 212 |
with gr.Column(scale=1, min_width=300):
|
| 213 |
with gr.Group(elem_classes="control-panel"):
|
| 214 |
gr.Markdown("### 🎨 Story Settings")
|
| 215 |
|
| 216 |
with gr.Group():
|
| 217 |
+
gr.Markdown("👶 *Child's Age*")
|
| 218 |
age_input = gr.Dropdown(age_options, label="", value="5", show_label=False)
|
| 219 |
|
| 220 |
with gr.Group():
|
| 221 |
+
gr.Markdown("🎭 *Story Theme*")
|
| 222 |
theme_input = gr.Dropdown(theme_options, label="", value="Adventure", show_label=False)
|
| 223 |
|
| 224 |
with gr.Group():
|
| 225 |
+
gr.Markdown("🌍 *Language*")
|
| 226 |
language_input = gr.Dropdown(language_options, label="", value="English", show_label=False)
|
| 227 |
|
| 228 |
with gr.Group():
|
| 229 |
+
gr.Markdown("🔊 *Audio Options*")
|
| 230 |
tts_input = gr.Checkbox(label="🎵 Include Audio Story", value=True)
|
| 231 |
|
| 232 |
generate_btn = gr.Button("✨ Create Magical Story! ✨", elem_classes="generate-btn", size="lg")
|
| 233 |
|
| 234 |
+
# RIGHT PANEL
|
| 235 |
with gr.Column(scale=2):
|
| 236 |
with gr.Group(elem_classes="story-container"):
|
| 237 |
gr.Markdown("### 📖 Your Magical Story")
|
|
|
|
| 250 |
|
| 251 |
gr.HTML("""
|
| 252 |
<div class="footer">
|
| 253 |
+
Made with ❤ for young readers everywhere! | Watch your stories come to life! ✨
|
| 254 |
</div>
|
| 255 |
""")
|
| 256 |
|
|
|
|
| 259 |
[age_input, theme_input, language_input, tts_input],
|
| 260 |
[story_output, audio_output]
|
| 261 |
)
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
# -------------------------------------------------------------
|
| 265 |
+
# LAUNCH APP
|
| 266 |
+
# -------------------------------------------------------------
|
| 267 |
+
if _name_ == "_main_":
|
| 268 |
+
iface.launch()
|