File size: 7,843 Bytes
bbc5824
 
46fcda1
bbc5824
46fcda1
 
7903146
 
 
b3dd657
bbc5824
b3dd657
bbc5824
 
46fcda1
2f370dc
 
 
46fcda1
bbc5824
 
 
 
 
46fcda1
 
 
 
 
 
 
e1e334a
bbc5824
 
 
 
 
 
 
569d0ae
e1e334a
bbc5824
 
 
 
 
 
e1e334a
bbc5824
46fcda1
2f370dc
 
 
8656728
bbc5824
 
 
 
46fcda1
2f370dc
 
 
46fcda1
 
e1e334a
 
bbc5824
 
 
 
 
 
60744d4
bbc5824
 
 
 
46fcda1
60744d4
bbc5824
 
 
 
46fcda1
e1e334a
46fcda1
bbc5824
46fcda1
2f370dc
 
 
46fcda1
75db4f2
 
 
 
 
46fcda1
 
2f370dc
 
 
bbc5824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60744d4
bbc5824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f370dc
 
 
7903146
60744d4
 
2f370dc
 
e1e334a
46fcda1
2f370dc
75db4f2
bbc5824
 
 
 
7903146
8656728
bbc5824
 
7903146
8656728
bbc5824
 
7903146
8656728
bbc5824
 
7903146
bbc5824
 
8656728
bbc5824
2f370dc
75db4f2
bbc5824
 
 
 
 
 
2f370dc
bbc5824
2f370dc
e1e334a
46fcda1
 
bbc5824
 
46fcda1
60744d4
2f370dc
 
 
b2a086b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
from groq import Groq
import gradio as gr
from gtts import gTTS
import re

# -------------------------------------------------------------
# GET API KEY
# -------------------------------------------------------------
groq_key = os.environ.get("Groq_API_Key")
if not groq_key:
    raise ValueError("Groq_API_Key not found in environment variables. Please add it to your Hugging Face Space secrets.")

client = Groq(api_key=groq_key)

# -------------------------------------------------------------
# STORY GENERATION LOGIC
# -------------------------------------------------------------
def generate_story(age, theme, language):
    system_msg = {
        "role": "system",
        "content": "You write age-appropriate stories with complete endings and a moral."
    }

    age_i = int(age)
    if age_i <= 6:
        length = "300 words"
    elif age_i <= 9:
        length = "600 words"
    else:
        length = "1000 words"

    user_msg = {
        "role": "user",
        "content": (
            f"Write a complete story for a {age}-year-old child. "
            f"Theme: {theme}. Language: {language}. Length about {length}. "
            "End with a clear moral of the story."
        )
    }

    response = client.chat.completions.create(
        messages=[system_msg, user_msg],
        model="llama-3.3-70b-versatile",
        max_tokens=2000,
        temperature=0.8
    )

    return response.choices[0].message.content.strip()

# -------------------------------------------------------------
# TEXT TO SPEECH
# -------------------------------------------------------------
def story_to_speech(story, language_code="en"):
    tts = gTTS(text=story, lang=language_code)
    filename = "story.mp3"
    tts.save(filename)
    return filename

# -------------------------------------------------------------
# PROCESS STORY + AUDIO
# -------------------------------------------------------------
def create_story_interface(age, theme, language, tts_option):
    story = generate_story(age, theme, language)
    story = re.sub(r'[*_#`~]', '', story)

    lines = story.split("\n", 1)
    if len(lines) > 1:
        title, rest_of_story = lines[0], lines[1]
    else:
        title, rest_of_story = lines[0], ""

    if language.lower().startswith("u"):
        story_html = (
            f'<div style="text-align:center; font-size:1.3em; direction:rtl; margin-bottom:5px;">{title}</div>'
            f'<div style="font-size:1.2em; direction:rtl; text-align:right;">{rest_of_story}</div>'
        )
        lang_code = "ur"
    else:
        story_html = (
            f'<div style="text-align:center; font-size:1.3em; direction:ltr; margin-bottom:5px;">{title}</div>'
            f'<div style="font-size:1.2em; direction:ltr; text-align:left;">{rest_of_story}</div>'
        )
        lang_code = "en"

    audio_file = story_to_speech(story, lang_code) if tts_option else None
    return story_html, audio_file

# -------------------------------------------------------------
# UI OPTIONS
# -------------------------------------------------------------
age_options = [str(i) for i in range(3, 13)]
theme_options = [
    "Adventure", "Animals", "Fantasy", "Educational",
    "Friendship", "Magic", "Science", "Mystery",
    "Space", "Nature", "Kindness", "Courage"
]
language_options = ["English", "Urdu"]

# -------------------------------------------------------------
# CUSTOM CSS
# -------------------------------------------------------------
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');
.gradio-container {
    font-family: 'Comic Neue', cursive !important;
    background: linear-gradient(135deg, #ffafbd, #ffc3a0) !important;
    padding-top: 10px !important;
    margin-top: -70px !important;
}
.main-header {
    text-align: center;
    font-weight: bold !important;
    font-size: 3.2em !important;
    margin-top: 40px !important;
    margin-bottom: 20px !important;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    color: #12979B;
}
.story-container {
    background:#fff5ee;
    border-radius: 20px;
    padding: 25px;
    border: 5px solid #FFD166;
    box-shadow: 0 8px 25px rgba(0,0,0,0.1);
    min-height: 400px;
    margin-top: -20px !important;
}
.control-panel {
    background: #fff5ee;
    border-radius: 15px;
    padding: 20px;
    border: 3px dashed #4ECDC4;
    font-size: 1.2em !important;
    font-weight: 600;
    margin-top: -20px !important;
}
.generate-btn {
    background: linear-gradient(45deg, #FF6B6B, #FF9E7D) !important;
    border: none !important;
    border-radius: 50px !important;
    padding: 10px 22px !important;
    font-size: 1em !important;
    font-weight: bold !important;
    color: white !important;
    margin-top: 20px !important;
}
.generate-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
}
.dropdown, .checkbox {
    background: white !important;
    border-radius: 10px !important;
    border: 2px solid #96CEB4 !important;
}
.audio-player {
    border-radius: 15px !important;
    margin-top: 20px;
}
.subtitle {
    text-align: left;
    font-size: 1.5em !important;
    font-weight: 600;
    color: white;
    margin-top: -10px !important;
    margin-bottom: 20px;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}
.footer {
    text-align: center;
    margin-top: 20px;
    color: #666;
    font-style: italic;
    font-size: 1.5em !important;
}
.main-header + div {
    margin-top: -145px !important;
}
"""

# -------------------------------------------------------------
# BUILD GRADIO INTERFACE
# -------------------------------------------------------------
with gr.Blocks() as iface:  # Removed theme argument
    gr.HTML(f"<style>{custom_css}</style>")

    gr.HTML('<div class="main-header">🌈 StoryTime AIπŸŽͺ</div>')
    gr.HTML('<div class="subtitle">🎯 Create amazing stories that spark imagination and teach valuable lessons!</div>')

    with gr.Row():
        # LEFT PANEL
        with gr.Column(scale=1, min_width=300):
            with gr.Group(elem_classes="control-panel"):
                gr.Markdown("### 🎨 Story Settings")

                with gr.Group():
                    gr.Markdown("πŸ‘Ά Child's Age")
                    age_input = gr.Dropdown(age_options, value="5", show_label=False)

                with gr.Group():
                    gr.Markdown("🎭 Story Theme")
                    theme_input = gr.Dropdown(theme_options, value="Adventure", show_label=False)

                with gr.Group():
                    gr.Markdown("🌍 Language")
                    language_input = gr.Dropdown(language_options, value="English", show_label=False)

                with gr.Group():
                    gr.Markdown("πŸ”Š Audio Options")
                    tts_input = gr.Checkbox(label="🎡 Include Audio Story", value=True)

                generate_btn = gr.Button("✨ Create Magical Story! ✨", elem_classes="generate-btn")

        # RIGHT PANEL
        with gr.Column(scale=2):
            with gr.Group(elem_classes="story-container"):
                story_output = gr.Markdown(
                    "Your story will appear here! Choose settings and click the magic button! πŸŽ‡",
                    show_label=False
                )

            audio_output = gr.Audio(elem_classes="audio-player", show_label=False)

    gr.HTML('<div class="footer">Made with ❀ for young readers everywhere! | Watch your stories come to life! ✨</div>')

    generate_btn.click(
        create_story_interface,
        [age_input, theme_input, language_input, tts_input],
        [story_output, audio_output]
    )

# -------------------------------------------------------------
# LAUNCH APP
# -------------------------------------------------------------
if __name__ == "__main__":
    iface.launch()