Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
from agent import TranslationAgent
|
| 5 |
+
|
| 6 |
+
# Page config
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="Multi-Language Translation Agent",
|
| 9 |
+
page_icon="π",
|
| 10 |
+
layout="wide"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Initialize agent
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def get_agent():
|
| 16 |
+
return TranslationAgent()
|
| 17 |
+
|
| 18 |
+
def main():
|
| 19 |
+
st.title("π Multi-Language Translation Agent")
|
| 20 |
+
st.markdown("### Translate text between 70+ languages and convert to speech")
|
| 21 |
+
|
| 22 |
+
agent = get_agent()
|
| 23 |
+
|
| 24 |
+
# Create columns for layout
|
| 25 |
+
col1, col2 = st.columns(2)
|
| 26 |
+
|
| 27 |
+
with col1:
|
| 28 |
+
st.subheader("π Input")
|
| 29 |
+
|
| 30 |
+
# Source language selection
|
| 31 |
+
source_lang = st.selectbox(
|
| 32 |
+
"Source Language:",
|
| 33 |
+
options=list(agent.languages.keys()),
|
| 34 |
+
format_func=lambda x: f"{agent.languages[x]} ({x})",
|
| 35 |
+
index=0 # Default to English
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Text input
|
| 39 |
+
input_text = st.text_area(
|
| 40 |
+
"Enter text to translate:",
|
| 41 |
+
height=150,
|
| 42 |
+
placeholder="Type your text here..."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Auto-detect option
|
| 46 |
+
auto_detect = st.checkbox("Auto-detect source language", value=True)
|
| 47 |
+
|
| 48 |
+
with col2:
|
| 49 |
+
st.subheader("π― Output")
|
| 50 |
+
|
| 51 |
+
# Target language selection
|
| 52 |
+
target_lang = st.selectbox(
|
| 53 |
+
"Target Language:",
|
| 54 |
+
options=list(agent.languages.keys()),
|
| 55 |
+
format_func=lambda x: f"{agent.languages[x]} ({x})",
|
| 56 |
+
index=1 # Default to Spanish
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Translation output area
|
| 60 |
+
translation_output = st.empty()
|
| 61 |
+
|
| 62 |
+
# Audio output area
|
| 63 |
+
audio_output = st.empty()
|
| 64 |
+
|
| 65 |
+
# Process button
|
| 66 |
+
if st.button("π Translate & Generate Speech", type="primary"):
|
| 67 |
+
if input_text.strip():
|
| 68 |
+
with st.spinner("Processing..."):
|
| 69 |
+
try:
|
| 70 |
+
# Perform translation
|
| 71 |
+
result = agent.translate_and_speak(
|
| 72 |
+
text=input_text,
|
| 73 |
+
source_lang=None if auto_detect else source_lang,
|
| 74 |
+
target_lang=target_lang
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Display results
|
| 78 |
+
with translation_output.container():
|
| 79 |
+
st.text_area(
|
| 80 |
+
f"Translation to {agent.languages[target_lang]}:",
|
| 81 |
+
value=result['translated_text'],
|
| 82 |
+
height=150,
|
| 83 |
+
key="translation_result"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Show detected language if auto-detect was used
|
| 87 |
+
if auto_detect and result['detected_language']:
|
| 88 |
+
st.info(f"Detected language: {agent.languages.get(result['detected_language'], result['detected_language'])}")
|
| 89 |
+
|
| 90 |
+
# Display audio
|
| 91 |
+
with audio_output.container():
|
| 92 |
+
st.audio(result['audio_file'], format='audio/mp3')
|
| 93 |
+
|
| 94 |
+
# Download button
|
| 95 |
+
with open(result['audio_file'], 'rb') as audio_file:
|
| 96 |
+
st.download_button(
|
| 97 |
+
label="π₯ Download Audio",
|
| 98 |
+
data=audio_file.read(),
|
| 99 |
+
file_name=f"translation_{target_lang}.mp3",
|
| 100 |
+
mime="audio/mpeg"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
st.success("β
Translation and speech generation completed!")
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
st.error(f"β Error: {str(e)}")
|
| 107 |
+
else:
|
| 108 |
+
st.warning("β οΈ Please enter text to translate")
|
| 109 |
+
|
| 110 |
+
# Language support info
|
| 111 |
+
with st.expander("π Supported Languages"):
|
| 112 |
+
st.write("This agent supports translation between any of these languages:")
|
| 113 |
+
|
| 114 |
+
# Create columns for better display
|
| 115 |
+
lang_cols = st.columns(4)
|
| 116 |
+
lang_items = list(agent.languages.items())
|
| 117 |
+
|
| 118 |
+
for i, (code, name) in enumerate(lang_items):
|
| 119 |
+
with lang_cols[i % 4]:
|
| 120 |
+
st.write(f"**{name}** ({code})")
|
| 121 |
+
|
| 122 |
+
# Instructions
|
| 123 |
+
with st.expander("π How to Use"):
|
| 124 |
+
st.markdown("""
|
| 125 |
+
1. **Select Source Language** or enable auto-detect
|
| 126 |
+
2. **Enter your text** in the input area
|
| 127 |
+
3. **Choose Target Language** for translation
|
| 128 |
+
4. **Click Translate & Generate Speech** button
|
| 129 |
+
5. **Listen to the audio** and download if needed
|
| 130 |
+
|
| 131 |
+
**Features:**
|
| 132 |
+
- β
20+ languages supported
|
| 133 |
+
- β
Auto-detect source language
|
| 134 |
+
- β
High-quality translations
|
| 135 |
+
- β
Natural-sounding speech
|
| 136 |
+
- β
Download audio files
|
| 137 |
+
""")
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
main()
|