Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
from gtts import gTTS
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
def generate_story(age, theme, language):
|
| 10 |
-
system_msg = {
|
| 11 |
-
"role": "system",
|
| 12 |
-
"content": "You write age-appropriate stories with complete endings and a moral."
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
age_i = int(age)
|
| 16 |
if age_i <= 6:
|
| 17 |
length = "300 words"
|
|
@@ -20,29 +18,52 @@ def generate_story(age, theme, language):
|
|
| 20 |
else:
|
| 21 |
length = "1000 words"
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
response =
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
except Exception as e:
|
| 41 |
-
return f"Error generating story: {str(e)}"
|
| 42 |
|
| 43 |
def story_to_speech(story, language_code='en'):
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
filename = "story.mp3"
|
| 47 |
tts.save(filename)
|
| 48 |
return filename
|
|
@@ -51,10 +72,14 @@ def story_to_speech(story, language_code='en'):
|
|
| 51 |
return None
|
| 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'[*_
|
| 58 |
|
| 59 |
# Split first line as title
|
| 60 |
lines = story.split("\n", 1)
|
|
@@ -63,16 +88,16 @@ def create_story_interface(age, theme, language, tts_option):
|
|
| 63 |
else:
|
| 64 |
title, rest_of_story = lines[0], ""
|
| 65 |
|
| 66 |
-
if language.lower()
|
| 67 |
story_html = (
|
| 68 |
-
f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:
|
| 69 |
-
f'<div style="font-size:1.2em; direction:rtl; text-align:right;">{rest_of_story}</div>'
|
| 70 |
)
|
| 71 |
lang_code = "ur"
|
| 72 |
else: # English
|
| 73 |
story_html = (
|
| 74 |
-
f'<div style="text-align:center; font-size:1.3em; direction:ltr; margin-bottom:
|
| 75 |
-
f'<div style="font-size:1.2em; direction:ltr; text-align:left;">{rest_of_story}</div>'
|
| 76 |
)
|
| 77 |
lang_code = "en"
|
| 78 |
|
|
@@ -99,45 +124,47 @@ custom_css = """
|
|
| 99 |
.gradio-container {
|
| 100 |
font-family: 'Comic Neue', cursive !important;
|
| 101 |
background: linear-gradient(135deg, #ffafbd, #ffc3a0) !important;
|
| 102 |
-
padding
|
| 103 |
-
margin-top: -70px !important;
|
| 104 |
}
|
| 105 |
.main-header {
|
| 106 |
text-align: center;
|
| 107 |
font-weight: bold !important;
|
| 108 |
-
font-size:
|
| 109 |
-
margin
|
| 110 |
-
margin-bottom: 20px !important;
|
| 111 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 112 |
color: #12979B;
|
| 113 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
.story-container {
|
| 115 |
-
background
|
| 116 |
border-radius: 20px;
|
| 117 |
padding: 25px;
|
| 118 |
border: 5px solid #FFD166;
|
| 119 |
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
|
| 120 |
min-height: 400px;
|
| 121 |
-
margin-top: -20px !important;
|
| 122 |
}
|
| 123 |
.control-panel {
|
| 124 |
background: #fff5ee;
|
| 125 |
border-radius: 15px;
|
| 126 |
padding: 20px;
|
| 127 |
border: 3px dashed #4ECDC4;
|
| 128 |
-
font-size: 1.2em !important;
|
| 129 |
-
font-weight: 600;
|
| 130 |
-
margin-top: -20px !important;
|
| 131 |
}
|
| 132 |
.generate-btn {
|
| 133 |
background: linear-gradient(45deg, #FF6B6B, #FF9E7D) !important;
|
| 134 |
border: none !important;
|
| 135 |
border-radius: 50px !important;
|
| 136 |
-
padding:
|
| 137 |
-
font-size:
|
| 138 |
font-weight: bold !important;
|
| 139 |
color: white !important;
|
| 140 |
-
margin
|
|
|
|
| 141 |
}
|
| 142 |
.generate-btn:hover {
|
| 143 |
transform: scale(1.05);
|
|
@@ -147,35 +174,17 @@ custom_css = """
|
|
| 147 |
background: white !important;
|
| 148 |
border-radius: 10px !important;
|
| 149 |
border: 2px solid #96CEB4 !important;
|
|
|
|
| 150 |
}
|
| 151 |
.audio-player {
|
| 152 |
border-radius: 15px !important;
|
| 153 |
-
margin
|
| 154 |
-
}
|
| 155 |
-
.subtitle {
|
| 156 |
-
text-align: left;
|
| 157 |
-
font-size: 1.5em !important;
|
| 158 |
-
font-weight: 600;
|
| 159 |
-
color: white;
|
| 160 |
-
margin-top: -10px !important;
|
| 161 |
-
margin-bottom: 20px;
|
| 162 |
-
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 163 |
-
}
|
| 164 |
-
.control-panel .gr-Markdown {
|
| 165 |
-
font-size: 1.5em !important;
|
| 166 |
-
}
|
| 167 |
-
.story-container .gr-Markdown {
|
| 168 |
-
font-size: 1.5em !important;
|
| 169 |
}
|
| 170 |
.footer {
|
| 171 |
text-align: center;
|
| 172 |
margin-top: 20px;
|
| 173 |
color: #666;
|
| 174 |
font-style: italic;
|
| 175 |
-
font-size: 1.5em !important;
|
| 176 |
-
}
|
| 177 |
-
.main-header + div {
|
| 178 |
-
margin-top: -145px !important;
|
| 179 |
}
|
| 180 |
"""
|
| 181 |
|
|
@@ -183,7 +192,7 @@ custom_css = """
|
|
| 183 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
| 184 |
gr.HTML("""
|
| 185 |
<div class="main-header">
|
| 186 |
-
π StoryTime AIπͺ
|
| 187 |
</div>
|
| 188 |
""")
|
| 189 |
|
|
@@ -198,37 +207,49 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 198 |
with gr.Group(elem_classes="control-panel"):
|
| 199 |
gr.Markdown("### π¨ Story Settings")
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
generate_btn = gr.Button(
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
with gr.Column(scale=2):
|
| 220 |
with gr.Group(elem_classes="story-container"):
|
| 221 |
gr.Markdown("### π Your Magical Story")
|
| 222 |
-
story_output = gr.
|
| 223 |
-
"Your story will appear here! Choose settings and click the magic button!
|
| 224 |
-
show_label=False
|
| 225 |
)
|
| 226 |
|
| 227 |
with gr.Group():
|
| 228 |
gr.Markdown("### π§ Listen to Your Story")
|
| 229 |
audio_output = gr.Audio(
|
| 230 |
-
label="
|
| 231 |
-
elem_classes="audio-player",
|
| 232 |
show_label=False
|
| 233 |
)
|
| 234 |
|
|
@@ -238,6 +259,17 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 238 |
</div>
|
| 239 |
""")
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
generate_btn.click(
|
| 242 |
create_story_interface,
|
| 243 |
[age_input, theme_input, language_input, tts_input],
|
|
@@ -245,4 +277,4 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
| 245 |
)
|
| 246 |
|
| 247 |
if __name__ == "__main__":
|
| 248 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
from gtts import gTTS
|
| 5 |
import re
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Groq API configuration
|
| 9 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 10 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
|
| 12 |
def generate_story(age, theme, language):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
age_i = int(age)
|
| 14 |
if age_i <= 6:
|
| 15 |
length = "300 words"
|
|
|
|
| 18 |
else:
|
| 19 |
length = "1000 words"
|
| 20 |
|
| 21 |
+
# Prepare the prompt
|
| 22 |
+
prompt = (
|
| 23 |
+
f"Write a complete story for a {age}-year-old child. "
|
| 24 |
+
f"Theme: {theme}. Language: {language}. Length about {length}. "
|
| 25 |
+
"End with a clear moral of the story. "
|
| 26 |
+
"Make it engaging and age-appropriate."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Prepare the request payload
|
| 30 |
+
payload = {
|
| 31 |
+
"messages": [
|
| 32 |
+
{
|
| 33 |
+
"role": "system",
|
| 34 |
+
"content": "You write age-appropriate stories with complete endings and a moral."
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"role": "user",
|
| 38 |
+
"content": prompt
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
"model": "llama-3.1-8b-instant", # Using a model that's available on Groq
|
| 42 |
+
"max_tokens": 2000,
|
| 43 |
+
"temperature": 0.8
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
headers = {
|
| 47 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 48 |
+
"Content-Type": "application/json"
|
| 49 |
}
|
| 50 |
|
| 51 |
try:
|
| 52 |
+
response = requests.post(GROQ_API_URL, json=payload, headers=headers)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
+
|
| 55 |
+
data = response.json()
|
| 56 |
+
story = data["choices"][0]["message"]["content"].strip()
|
| 57 |
+
return story
|
| 58 |
+
|
| 59 |
except Exception as e:
|
| 60 |
+
return f"Error generating story: {str(e)}\n\nPlease check your API key and try again."
|
| 61 |
|
| 62 |
def story_to_speech(story, language_code='en'):
|
| 63 |
try:
|
| 64 |
+
# Clean story for TTS
|
| 65 |
+
clean_story = re.sub(r'[*_#~`]', '', story)
|
| 66 |
+
tts = gTTS(text=clean_story, lang=language_code, slow=False)
|
| 67 |
filename = "story.mp3"
|
| 68 |
tts.save(filename)
|
| 69 |
return filename
|
|
|
|
| 72 |
return None
|
| 73 |
|
| 74 |
def create_story_interface(age, theme, language, tts_option):
|
| 75 |
+
if not GROQ_API_KEY:
|
| 76 |
+
error_html = '<div style="color: red; text-align: center; font-size: 1.5em;">β οΈ Please set your GROQ_API_KEY in the Hugging Face Space secrets</div>'
|
| 77 |
+
return error_html, None
|
| 78 |
+
|
| 79 |
story = generate_story(age, theme, language)
|
| 80 |
|
| 81 |
+
# Remove Markdown symbols
|
| 82 |
+
story = re.sub(r'[*_#~`]', '', story)
|
| 83 |
|
| 84 |
# Split first line as title
|
| 85 |
lines = story.split("\n", 1)
|
|
|
|
| 88 |
else:
|
| 89 |
title, rest_of_story = lines[0], ""
|
| 90 |
|
| 91 |
+
if language.lower() == "urdu": # Urdu
|
| 92 |
story_html = (
|
| 93 |
+
f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:15px; font-weight: bold; color: #2c5530;">{title}</div>'
|
| 94 |
+
f'<div style="font-size:1.2em; direction:rtl; text-align:right; line-height: 1.6;">{rest_of_story}</div>'
|
| 95 |
)
|
| 96 |
lang_code = "ur"
|
| 97 |
else: # English
|
| 98 |
story_html = (
|
| 99 |
+
f'<div style="text-align:center; font-size:1.3em; direction:ltr; margin-bottom:15px; font-weight: bold; color: #2c5530;">{title}</div>'
|
| 100 |
+
f'<div style="font-size:1.2em; direction:ltr; text-align:left; line-height: 1.6;">{rest_of_story}</div>'
|
| 101 |
)
|
| 102 |
lang_code = "en"
|
| 103 |
|
|
|
|
| 124 |
.gradio-container {
|
| 125 |
font-family: 'Comic Neue', cursive !important;
|
| 126 |
background: linear-gradient(135deg, #ffafbd, #ffc3a0) !important;
|
| 127 |
+
padding: 20px;
|
|
|
|
| 128 |
}
|
| 129 |
.main-header {
|
| 130 |
text-align: center;
|
| 131 |
font-weight: bold !important;
|
| 132 |
+
font-size: 3em !important;
|
| 133 |
+
margin: 20px 0;
|
|
|
|
| 134 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 135 |
color: #12979B;
|
| 136 |
}
|
| 137 |
+
.subtitle {
|
| 138 |
+
text-align: center;
|
| 139 |
+
font-size: 1.3em !important;
|
| 140 |
+
color: white;
|
| 141 |
+
margin-bottom: 30px;
|
| 142 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.2);
|
| 143 |
+
}
|
| 144 |
.story-container {
|
| 145 |
+
background: #fff5ee;
|
| 146 |
border-radius: 20px;
|
| 147 |
padding: 25px;
|
| 148 |
border: 5px solid #FFD166;
|
| 149 |
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
|
| 150 |
min-height: 400px;
|
|
|
|
| 151 |
}
|
| 152 |
.control-panel {
|
| 153 |
background: #fff5ee;
|
| 154 |
border-radius: 15px;
|
| 155 |
padding: 20px;
|
| 156 |
border: 3px dashed #4ECDC4;
|
|
|
|
|
|
|
|
|
|
| 157 |
}
|
| 158 |
.generate-btn {
|
| 159 |
background: linear-gradient(45deg, #FF6B6B, #FF9E7D) !important;
|
| 160 |
border: none !important;
|
| 161 |
border-radius: 50px !important;
|
| 162 |
+
padding: 15px 30px !important;
|
| 163 |
+
font-size: 1.2em !important;
|
| 164 |
font-weight: bold !important;
|
| 165 |
color: white !important;
|
| 166 |
+
margin: 20px 0;
|
| 167 |
+
width: 100%;
|
| 168 |
}
|
| 169 |
.generate-btn:hover {
|
| 170 |
transform: scale(1.05);
|
|
|
|
| 174 |
background: white !important;
|
| 175 |
border-radius: 10px !important;
|
| 176 |
border: 2px solid #96CEB4 !important;
|
| 177 |
+
margin: 10px 0;
|
| 178 |
}
|
| 179 |
.audio-player {
|
| 180 |
border-radius: 15px !important;
|
| 181 |
+
margin: 20px 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
}
|
| 183 |
.footer {
|
| 184 |
text-align: center;
|
| 185 |
margin-top: 20px;
|
| 186 |
color: #666;
|
| 187 |
font-style: italic;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
"""
|
| 190 |
|
|
|
|
| 192 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
| 193 |
gr.HTML("""
|
| 194 |
<div class="main-header">
|
| 195 |
+
π StoryTime AI πͺ
|
| 196 |
</div>
|
| 197 |
""")
|
| 198 |
|
|
|
|
| 207 |
with gr.Group(elem_classes="control-panel"):
|
| 208 |
gr.Markdown("### π¨ Story Settings")
|
| 209 |
|
| 210 |
+
age_input = gr.Dropdown(
|
| 211 |
+
age_options,
|
| 212 |
+
label="πΆ Child's Age",
|
| 213 |
+
value="5",
|
| 214 |
+
info="Select age 3-12 years"
|
| 215 |
+
)
|
| 216 |
|
| 217 |
+
theme_input = gr.Dropdown(
|
| 218 |
+
theme_options,
|
| 219 |
+
label="π Story Theme",
|
| 220 |
+
value="Adventure",
|
| 221 |
+
info="Choose your favorite theme"
|
| 222 |
+
)
|
| 223 |
|
| 224 |
+
language_input = gr.Dropdown(
|
| 225 |
+
language_options,
|
| 226 |
+
label="π Language",
|
| 227 |
+
value="English",
|
| 228 |
+
info="English or Urdu"
|
| 229 |
+
)
|
| 230 |
|
| 231 |
+
tts_input = gr.Checkbox(
|
| 232 |
+
label="π Include Audio Story",
|
| 233 |
+
value=True,
|
| 234 |
+
info="Listen to the story"
|
| 235 |
+
)
|
| 236 |
|
| 237 |
+
generate_btn = gr.Button(
|
| 238 |
+
"β¨ Create Magical Story! β¨",
|
| 239 |
+
elem_classes="generate-btn"
|
| 240 |
+
)
|
| 241 |
|
| 242 |
with gr.Column(scale=2):
|
| 243 |
with gr.Group(elem_classes="story-container"):
|
| 244 |
gr.Markdown("### π Your Magical Story")
|
| 245 |
+
story_output = gr.HTML(
|
| 246 |
+
"<div style='text-align: center; color: #666; font-style: italic;'>Your story will appear here! Choose settings and click the magic button! π</div>"
|
|
|
|
| 247 |
)
|
| 248 |
|
| 249 |
with gr.Group():
|
| 250 |
gr.Markdown("### π§ Listen to Your Story")
|
| 251 |
audio_output = gr.Audio(
|
| 252 |
+
label="",
|
|
|
|
| 253 |
show_label=False
|
| 254 |
)
|
| 255 |
|
|
|
|
| 259 |
</div>
|
| 260 |
""")
|
| 261 |
|
| 262 |
+
# Handle the case when API key is not set
|
| 263 |
+
if not GROQ_API_KEY:
|
| 264 |
+
gr.HTML("""
|
| 265 |
+
<div style="background: #ffebee; padding: 20px; border-radius: 10px; border: 2px solid #f44336; margin: 20px 0;">
|
| 266 |
+
<h3 style="color: #c62828; margin: 0;">β οΈ Configuration Required</h3>
|
| 267 |
+
<p style="margin: 10px 0 0 0; color: #d32f2f;">
|
| 268 |
+
Please set your GROQ_API_KEY in the Hugging Face Space secrets to use this app.
|
| 269 |
+
</p>
|
| 270 |
+
</div>
|
| 271 |
+
""")
|
| 272 |
+
|
| 273 |
generate_btn.click(
|
| 274 |
create_story_interface,
|
| 275 |
[age_input, theme_input, language_input, tts_input],
|
|
|
|
| 277 |
)
|
| 278 |
|
| 279 |
if __name__ == "__main__":
|
| 280 |
+
iface.launch(share=False)
|