File size: 5,050 Bytes
7b94c4e | 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 | import streamlit as st
import tempfile
import os
from agent import TranslationAgent
# Page config
st.set_page_config(
page_title="Multi-Language Translation Agent",
page_icon="π",
layout="wide"
)
# Initialize agent
@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()
# Create columns for layout
col1, col2 = st.columns(2)
with col1:
st.subheader("π Input")
# Source language selection
source_lang = st.selectbox(
"Source Language:",
options=list(agent.languages.keys()),
format_func=lambda x: f"{agent.languages[x]} ({x})",
index=0 # Default to English
)
# Text input
input_text = st.text_area(
"Enter text to translate:",
height=150,
placeholder="Type your text here..."
)
# Auto-detect option
auto_detect = st.checkbox("Auto-detect source language", value=True)
with col2:
st.subheader("π― Output")
# Target language selection
target_lang = st.selectbox(
"Target Language:",
options=list(agent.languages.keys()),
format_func=lambda x: f"{agent.languages[x]} ({x})",
index=1 # Default to Spanish
)
# Translation output area
translation_output = st.empty()
# Audio output area
audio_output = st.empty()
# Process button
if st.button("π Translate & Generate Speech", type="primary"):
if input_text.strip():
with st.spinner("Processing..."):
try:
# Perform translation
result = agent.translate_and_speak(
text=input_text,
source_lang=None if auto_detect else source_lang,
target_lang=target_lang
)
# Display results
with translation_output.container():
st.text_area(
f"Translation to {agent.languages[target_lang]}:",
value=result['translated_text'],
height=150,
key="translation_result"
)
# Show detected language if auto-detect was used
if auto_detect and result['detected_language']:
st.info(f"Detected language: {agent.languages.get(result['detected_language'], result['detected_language'])}")
# Display audio
with audio_output.container():
st.audio(result['audio_file'], format='audio/mp3')
# Download button
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")
# Language support info
with st.expander("π Supported Languages"):
st.write("This agent supports translation between any of these languages:")
# Create columns for better display
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})")
# Instructions
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() |