Spaces:
Sleeping
Sleeping
oscarvillafuerte
commited on
Commit
•
2f419e1
1
Parent(s):
5a11a25
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import time
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
st.title("Enhanced Multilingual Translator Chatbot")
|
6 |
+
|
7 |
+
# Choose the translation models from Hugging Face
|
8 |
+
translation_models = {
|
9 |
+
"English to German": "Helsinki-NLP/opus-mt-en-de",
|
10 |
+
"German to English": "Helsinki-NLP/opus-mt-de-en",
|
11 |
+
"English to French": "Helsinki-NLP/opus-mt-en-fr",
|
12 |
+
"French to English": "Helsinki-NLP/opus-mt-fr-en",
|
13 |
+
"English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
|
14 |
+
"Urdu to English": "Helsinki-NLP/opus-mt-ur-en",
|
15 |
+
"English to Spanish": "Helsinki-NLP/opus-mt-en-es",
|
16 |
+
"Spanish to English": "Helsinki-NLP/opus-mt-es-en",
|
17 |
+
"English to Chinese": "Helsinki-NLP/opus-mt-en-zh",
|
18 |
+
"Chinese to English": "Helsinki-NLP/opus-mt-zh-en",
|
19 |
+
# Add more language pairs as needed
|
20 |
+
}
|
21 |
+
|
22 |
+
selected_translation = st.selectbox("Select translation model", list(translation_models.keys()))
|
23 |
+
|
24 |
+
# Load the translation pipeline
|
25 |
+
translator = pipeline(task="translation", model=translation_models[selected_translation])
|
26 |
+
|
27 |
+
# User input for translation
|
28 |
+
user_input = st.text_area("Enter text for translation:", "")
|
29 |
+
|
30 |
+
# Display loading indicator
|
31 |
+
if st.button("Translate"):
|
32 |
+
with st.spinner("Translating..."):
|
33 |
+
# Simulate translation delay for demonstration
|
34 |
+
time.sleep(2)
|
35 |
+
if user_input:
|
36 |
+
# Perform translation
|
37 |
+
translated_text = translator(user_input, max_length=500)[0]['translation_text']
|
38 |
+
st.success(f"Translated Text: {translated_text}")
|
39 |
+
else:
|
40 |
+
st.warning("Please enter text for translation.")
|
41 |
+
|
42 |
+
# Clear button to reset input and result
|
43 |
+
if st.button("Clear"):
|
44 |
+
user_input = ""
|
45 |
+
st.success("Input cleared.")
|
46 |
+
st.empty() # Clear previous results if any
|
47 |
+
|
48 |
+
st.markdown("---")
|
49 |
+
|
50 |
+
st.subheader("About")
|
51 |
+
st.write(
|
52 |
+
"This is an enhanced Multilingual Translator chatbot that uses the Hugging Face Transformers library."
|
53 |
+
)
|
54 |
+
st.write(
|
55 |
+
"Select a translation model from the dropdown, enter text, and click 'Translate' to see the translation."
|
56 |
+
)
|