Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from googletrans import Translator, LANGUAGES
|
3 |
+
|
4 |
+
# Streamlit app
|
5 |
+
st.title("Text Translator")
|
6 |
+
|
7 |
+
# Input text
|
8 |
+
user_text = st.text_area("Enter the text you want to translate:")
|
9 |
+
|
10 |
+
# Language selection
|
11 |
+
st.subheader("Select the target language for translation:")
|
12 |
+
|
13 |
+
# Create a list of language codes and names for selection
|
14 |
+
language_options = {lang: code for code, lang in LANGUAGES.items()}
|
15 |
+
selected_language = st.selectbox("Select a language:", list(language_options.keys()))
|
16 |
+
|
17 |
+
# Initialize the translator
|
18 |
+
translator = Translator()
|
19 |
+
|
20 |
+
# Translate the text when the user clicks the "Translate" button
|
21 |
+
if st.button("Translate"):
|
22 |
+
try:
|
23 |
+
# Translate the text to the selected language
|
24 |
+
translated_text = translator.translate(user_text, dest=language_options[selected_language]).text
|
25 |
+
st.success(f"Translated text: {translated_text}")
|
26 |
+
except Exception as e:
|
27 |
+
st.error(f"Translation failed: {str(e)}")
|