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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# Get API key from HuggingFace Secrets
|
| 8 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 9 |
+
|
| 10 |
+
def generate_story(age, theme, language):
|
| 11 |
+
system_msg = {
|
| 12 |
+
"role": "system",
|
| 13 |
+
"content": "You write age-appropriate stories with complete endings and a moral."
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
age_i = int(age)
|
| 17 |
+
if age_i <= 6:
|
| 18 |
+
length = "300 words"
|
| 19 |
+
elif age_i <= 9:
|
| 20 |
+
length = "600 words"
|
| 21 |
+
else:
|
| 22 |
+
length = "1000 words"
|
| 23 |
+
|
| 24 |
+
user_msg = {
|
| 25 |
+
"role": "user",
|
| 26 |
+
"content": (
|
| 27 |
+
f"Write a complete story for a {age}-year-old child. "
|
| 28 |
+
f"Theme: {theme}. Language: {language}. Length about {length}. "
|
| 29 |
+
"End with a clear moral of the story."
|
| 30 |
+
)
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
response = client.chat.completions.create(
|
| 34 |
+
messages=[system_msg, user_msg],
|
| 35 |
+
model="openai/gpt-oss-20b",
|
| 36 |
+
max_tokens=2000,
|
| 37 |
+
temperature=0.8
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
story = response.choices[0].message.content.strip()
|
| 41 |
+
return story
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def story_to_speech(story, language_code='en'):
|
| 45 |
+
filename = "story.mp3"
|
| 46 |
+
tts = gTTS(text=story, lang=language_code)
|
| 47 |
+
tts.save(filename)
|
| 48 |
+
return filename
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def create_story_interface(age, theme, language, tts_option):
|
| 52 |
+
story = generate_story(age, theme, language)
|
| 53 |
+
|
| 54 |
+
story = re.sub(r'[*_#`~]', '', story)
|
| 55 |
+
|
| 56 |
+
lines = story.split("\n", 1)
|
| 57 |
+
if len(lines) > 1:
|
| 58 |
+
title, rest_of_story = lines[0], lines[1]
|
| 59 |
+
else:
|
| 60 |
+
title, rest_of_story = lines[0], ""
|
| 61 |
+
|
| 62 |
+
if language.lower().startswith("u"): # Urdu
|
| 63 |
+
story_html = (
|
| 64 |
+
f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:5px;">{title}</div>'
|
| 65 |
+
f'<div style="font-size:1.2em; direction:rtl; text-align:right;">{rest_of_story}</div>'
|
| 66 |
+
)
|
| 67 |
+
lang_code = "ur"
|
| 68 |
+
else: # English
|
| 69 |
+
story_html = (
|
| 70 |
+
f'<div style="text-align:center; font-size:1.3em; direction:ltr; margin-bottom:5px;">{title}</div>'
|
| 71 |
+
f'<div style="font-size:1.2em; direction:ltr; text-align:left;">{rest_of_story}</div>'
|
| 72 |
+
)
|
| 73 |
+
lang_code = "en"
|
| 74 |
+
|
| 75 |
+
audio_file = story_to_speech(story, lang_code) if tts_option else None
|
| 76 |
+
|
| 77 |
+
return story_html, audio_file
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
age_options = [str(i) for i in range(3, 13)]
|
| 81 |
+
theme_options = [
|
| 82 |
+
"Adventure", "Animals", "Fantasy", "Educational",
|
| 83 |
+
"Friendship", "Magic", "Science", "Mystery",
|
| 84 |
+
"Space", "Nature", "Kindness", "Courage"
|
| 85 |
+
]
|
| 86 |
+
language_options = ["English", "Urdu"]
|
| 87 |
+
|
| 88 |
+
custom_css = """
|
| 89 |
+
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
|
| 90 |
+
|
| 91 |
+
.gradio-container {
|
| 92 |
+
font-family: 'Comic Neue', cursive !important;
|
| 93 |
+
background: linear-gradient(135deg, #ffafbd, #ffc3a0) !important;
|
| 94 |
+
padding-top: 10px !important;
|
| 95 |
+
margin-top: -70px !important;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
.main-header {
|
| 99 |
+
text-align: center;
|
| 100 |
+
font-weight: bold !important;
|
| 101 |
+
font-size: 3.2em !important;
|
| 102 |
+
margin-top: 40px !important;
|
| 103 |
+
margin-bottom: 20px !important;
|
| 104 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 105 |
+
color: #12979B;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
.story-container {
|
| 109 |
+
background:#fff5ee;
|
| 110 |
+
border-radius: 20px;
|
| 111 |
+
padding: 25px;
|
| 112 |
+
border: 5px solid #FFD166;
|
| 113 |
+
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
|
| 114 |
+
min-height: 400px;
|
| 115 |
+
margin-top: -20px !important;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
.control-panel {
|
| 119 |
+
background: #fff5ee;
|
| 120 |
+
border-radius: 15px;
|
| 121 |
+
padding: 20px;
|
| 122 |
+
border: 3px dashed #4ECDC4;
|
| 123 |
+
font-size: 1.2em !important;
|
| 124 |
+
font-weight: 600;
|
| 125 |
+
margin-top: -20px !important;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
.generate-btn {
|
| 129 |
+
background: linear-gradient(45deg, #FF6B6B, #FF9E7D) !important;
|
| 130 |
+
border: none !important;
|
| 131 |
+
border-radius: 50px !important;
|
| 132 |
+
padding: 10px 22px !important;
|
| 133 |
+
font-size: 1em !important;
|
| 134 |
+
font-weight: bold !important;
|
| 135 |
+
color: white !important;
|
| 136 |
+
margin-top: 20px !important;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
.generate-btn:hover {
|
| 140 |
+
transform: scale(1.05);
|
| 141 |
+
box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
.audio-player {
|
| 145 |
+
border-radius: 15px !important;
|
| 146 |
+
margin-top: 20px;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
.subtitle {
|
| 150 |
+
font-size: 1.5em !important;
|
| 151 |
+
font-weight: 600;
|
| 152 |
+
color: white;
|
| 153 |
+
margin-bottom: 20px;
|
| 154 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 155 |
+
}
|
| 156 |
+
"""
|
| 157 |
+
|
| 158 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
| 159 |
+
gr.HTML("<div class='main-header'>🌈 StoryTime AI 🎪</div>")
|
| 160 |
+
gr.HTML("<div class='subtitle'>🎯 Create amazing stories that spark imagination and teach valuable lessons!</div>")
|
| 161 |
+
|
| 162 |
+
with gr.Row():
|
| 163 |
+
with gr.Column(scale=1, min_width=300):
|
| 164 |
+
with gr.Group(elem_classes="control-panel"):
|
| 165 |
+
gr.Markdown("### 🎨 Story Settings")
|
| 166 |
+
|
| 167 |
+
age_input = gr.Dropdown(age_options, label="", value="5")
|
| 168 |
+
theme_input = gr.Dropdown(theme_options, label="", value="Adventure")
|
| 169 |
+
language_input = gr.Dropdown(language_options, label="", value="English")
|
| 170 |
+
tts_input = gr.Checkbox(label="🎵 Include Audio Story", value=True)
|
| 171 |
+
|
| 172 |
+
generate_btn = gr.Button("✨ Create Magical Story! ✨", elem_classes="generate-btn")
|
| 173 |
+
|
| 174 |
+
with gr.Column(scale=2):
|
| 175 |
+
with gr.Group(elem_classes="story-container"):
|
| 176 |
+
story_output = gr.HTML("Your story will appear here! 🎇")
|
| 177 |
+
|
| 178 |
+
audio_output = gr.Audio(label="Audio Story", elem_classes="audio-player")
|
| 179 |
+
|
| 180 |
+
generate_btn.click(
|
| 181 |
+
create_story_interface,
|
| 182 |
+
[age_input, theme_input, language_input, tts_input],
|
| 183 |
+
[story_output, audio_output]
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
iface.launch()
|