| | import streamlit as st
|
| | import tempfile
|
| | import os
|
| | from agent import TranslationAgent
|
| |
|
| |
|
| | st.set_page_config(
|
| | page_title="Multi-Language Translation Agent",
|
| | page_icon="π",
|
| | layout="wide"
|
| | )
|
| |
|
| |
|
| | @st.cache_resource
|
| | def get_agent():
|
| | return TranslationAgent()
|
| |
|
| | def main():
|
| | st.title("π Multi-Language Translation Agent")
|
| | st.markdown("### Translate text between 70+ languages and convert to speech")
|
| |
|
| | agent = get_agent()
|
| |
|
| |
|
| | col1, col2 = st.columns(2)
|
| |
|
| | with col1:
|
| | st.subheader("π Input")
|
| |
|
| |
|
| | source_lang = st.selectbox(
|
| | "Source Language:",
|
| | options=list(agent.languages.keys()),
|
| | format_func=lambda x: f"{agent.languages[x]} ({x})",
|
| | index=0
|
| | )
|
| |
|
| |
|
| | input_text = st.text_area(
|
| | "Enter text to translate:",
|
| | height=150,
|
| | placeholder="Type your text here..."
|
| | )
|
| |
|
| |
|
| | auto_detect = st.checkbox("Auto-detect source language", value=True)
|
| |
|
| | with col2:
|
| | st.subheader("π― Output")
|
| |
|
| |
|
| | target_lang = st.selectbox(
|
| | "Target Language:",
|
| | options=list(agent.languages.keys()),
|
| | format_func=lambda x: f"{agent.languages[x]} ({x})",
|
| | index=1
|
| | )
|
| |
|
| |
|
| | translation_output = st.empty()
|
| |
|
| |
|
| | audio_output = st.empty()
|
| |
|
| |
|
| | if st.button("π Translate & Generate Speech", type="primary"):
|
| | if input_text.strip():
|
| | with st.spinner("Processing..."):
|
| | try:
|
| |
|
| | result = agent.translate_and_speak(
|
| | text=input_text,
|
| | source_lang=None if auto_detect else source_lang,
|
| | target_lang=target_lang
|
| | )
|
| |
|
| |
|
| | with translation_output.container():
|
| | st.text_area(
|
| | f"Translation to {agent.languages[target_lang]}:",
|
| | value=result['translated_text'],
|
| | height=150,
|
| | key="translation_result"
|
| | )
|
| |
|
| |
|
| | if auto_detect and result['detected_language']:
|
| | st.info(f"Detected language: {agent.languages.get(result['detected_language'], result['detected_language'])}")
|
| |
|
| |
|
| | with audio_output.container():
|
| | st.audio(result['audio_file'], format='audio/mp3')
|
| |
|
| |
|
| | with open(result['audio_file'], 'rb') as audio_file:
|
| | st.download_button(
|
| | label="π₯ Download Audio",
|
| | data=audio_file.read(),
|
| | file_name=f"translation_{target_lang}.mp3",
|
| | mime="audio/mpeg"
|
| | )
|
| |
|
| | st.success("β
Translation and speech generation completed!")
|
| |
|
| | except Exception as e:
|
| | st.error(f"β Error: {str(e)}")
|
| | else:
|
| | st.warning("β οΈ Please enter text to translate")
|
| |
|
| |
|
| | with st.expander("π Supported Languages"):
|
| | st.write("This agent supports translation between any of these languages:")
|
| |
|
| |
|
| | lang_cols = st.columns(4)
|
| | lang_items = list(agent.languages.items())
|
| |
|
| | for i, (code, name) in enumerate(lang_items):
|
| | with lang_cols[i % 4]:
|
| | st.write(f"**{name}** ({code})")
|
| |
|
| |
|
| | with st.expander("π How to Use"):
|
| | st.markdown("""
|
| | 1. **Select Source Language** or enable auto-detect
|
| | 2. **Enter your text** in the input area
|
| | 3. **Choose Target Language** for translation
|
| | 4. **Click Translate & Generate Speech** button
|
| | 5. **Listen to the audio** and download if needed
|
| |
|
| | **Features:**
|
| | - β
20+ languages supported
|
| | - β
Auto-detect source language
|
| | - β
High-quality translations
|
| | - β
Natural-sounding speech
|
| | - β
Download audio files
|
| | """)
|
| |
|
| | if __name__ == "__main__":
|
| | main() |