Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 8 |
+
|
| 9 |
+
def initialize_messages():
|
| 10 |
+
return [{"role": "system",
|
| 11 |
+
"content": "You are a talented mythological storyteller. Tell short, engaging Indian mythology stories in a simple, emotional style. End every story with a moral."}]
|
| 12 |
+
|
| 13 |
+
messages_prmt = initialize_messages()
|
| 14 |
+
|
| 15 |
+
def generate_story(user_input):
|
| 16 |
+
global messages_prmt
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
messages_prmt.append({"role": "user", "content": user_input})
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
messages=messages_prmt,
|
| 22 |
+
model="llama3-8b-8192",
|
| 23 |
+
)
|
| 24 |
+
story = response.choices[0].message.content
|
| 25 |
+
messages_prmt.append({"role": "assistant", "content": story})
|
| 26 |
+
|
| 27 |
+
# TTS generation
|
| 28 |
+
audio_path = "story.mp3"
|
| 29 |
+
try:
|
| 30 |
+
tts = gTTS(story)
|
| 31 |
+
tts.save(audio_path)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print("TTS Error:", e)
|
| 34 |
+
audio_path = None
|
| 35 |
+
|
| 36 |
+
return story, audio_path
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"⚠️ Error: {str(e)}", None
|
| 40 |
+
|
| 41 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 42 |
+
gr.Markdown("""
|
| 43 |
+
# 🕉️ Mythology Storyteller Bot
|
| 44 |
+
_Ask for short Indian mythology stories and hear them come alive!_
|
| 45 |
+
""")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column(scale=2):
|
| 49 |
+
user_input = gr.Textbox(
|
| 50 |
+
label="Your Prompt",
|
| 51 |
+
placeholder="e.g. Tell me a story about Krishna",
|
| 52 |
+
lines=2
|
| 53 |
+
)
|
| 54 |
+
generate_btn = gr.Button("🪄 Generate Story")
|
| 55 |
+
|
| 56 |
+
with gr.Column(scale=3):
|
| 57 |
+
story_output = gr.Textbox(label="📜 Story", lines=8, interactive=False)
|
| 58 |
+
audio_output = gr.Audio(label="🔊 Listen", autoplay=True)
|
| 59 |
+
|
| 60 |
+
generate_btn.click(fn=generate_story, inputs=user_input, outputs=[story_output, audio_output])
|
| 61 |
+
|
| 62 |
+
app.launch()
|