Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394# app.pyimport gradio as grfrom groq import Groqfrom gtts import gTTSfrom googletrans import Translatorimport os# 🔐 Replace this with your actual Groq API keyclient = Groq(api_key=os.environ["GROQ_API_KEY"])# 🌐 Translator instancetranslator = Translator()# 🧠 System prompt for the botdef 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()# 🧠 Main functiondef generate_story(user_input, selected_language):global messages_prmttry: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.contentmessages_prmt.append({"role": "assistant", "content": story})# 🌐 Translate story if selected language is not Englishlang_map = {"English": "en", "Hindi": "hi", "Malayalam": "ml"}tts_lang_code = lang_map.get(selected_language, "en")if tts_lang_code != "en":try:story_translated = translator.translate(story, dest=tts_lang_code).textexcept Exception as e:print("Translation error:", e)story_translated = storyelse:story_translated = story# 🔊 Text to Speechtts = gTTS(story_translated, lang=tts_lang_code)audio_path = "story.mp3"tts.save(audio_path)return story, audio_pathexcept Exception as e:return f"⚠️ Error generating story: {str(e)}", None# 🎨 UIwith gr.Blocks(theme=gr.themes.Soft()) as app:gr.Markdown("""# 🕉️ Mythology Storyteller Bot_Get short, engaging Indian mythology stories with morals – and hear them too!_""")with gr.Row():with gr.Column(scale=2):user_input = gr.Textbox(placeholder="e.g., Tell me a story about Krishna",label="Ask for a story",lines=2)lang_dropdown = gr.Dropdown(choices=["English", "Hindi", "Malayalam"],value="English",label="Select Language for Audio")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, lang_dropdown],outputs=[story_output, audio_output])app.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667from groq import Groqimport gradio as grfrom gtts import gTTSfrom googletrans import Translatorimport osclient = Groq(api_key=os.environ["GROQ_API_KEY"])translator = Translator()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, language):global messages_prmttry: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.contentmessages_prmt.append({"role": "assistant", "content": story})# Translate if neededif language == "Hindi":translated = translator.translate(story, dest='hi').texttts = gTTS(translated, lang='hi')output_text = translatedelse:tts = gTTS(story, lang='en')output_text = storyaudio_path = "story.mp3"tts.save(audio_path)return output_text, audio_pathexcept Exception as e:return f"⚠️ Error: {str(e)}", Nonewith 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)lang_select = gr.Dropdown(label="Language", choices=["English", "Hindi"], value="English")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, lang_select], outputs=[story_output, audio_output])app.launch()
app.py+20-47