File size: 1,933 Bytes
3ec9226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import gradio as gr
from groq import Groq
from gtts import gTTS
import os

client = Groq(api_key=os.environ["GROQ_API_KEY"])

def initialize_messages():
    return [{"role": "system",
             "content": "You are a talented mythological storyteller. Tell short, engaging Indian mythology stories in a simple, emotional style. End every story with a moral."}]

messages_prmt = initialize_messages()

def generate_story(user_input):
    global messages_prmt

    try:
        messages_prmt.append({"role": "user", "content": user_input})
        response = client.chat.completions.create(
            messages=messages_prmt,
            model="llama3-8b-8192",
        )
        story = response.choices[0].message.content
        messages_prmt.append({"role": "assistant", "content": story})

        # TTS generation
        audio_path = "story.mp3"
        try:
            tts = gTTS(story)
            tts.save(audio_path)
        except Exception as e:
            print("TTS Error:", e)
            audio_path = None

        return story, audio_path

    except Exception as e:
        return f"⚠️ Error: {str(e)}", None

with gr.Blocks(theme=gr.themes.Soft()) as app:
    gr.Markdown("""
    # 🕉️ Mythology Storyteller Bot
    _Ask for short Indian mythology stories and hear them come alive!_
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            user_input = gr.Textbox(
                label="Your Prompt",
                placeholder="e.g. Tell me a story about Krishna",
                lines=2
            )
            generate_btn = gr.Button("🪄 Generate Story")

        with gr.Column(scale=3):
            story_output = gr.Textbox(label="📜 Story", lines=8, interactive=False)
            audio_output = gr.Audio(label="🔊 Listen", autoplay=True)

    generate_btn.click(fn=generate_story, inputs=user_input, outputs=[story_output, audio_output])

app.launch()