Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,52 @@
|
|
1 |
import streamlit as st
|
2 |
-
import transformers
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Streamlit page config
|
9 |
st.set_page_config(page_title="Translator", layout="wide")
|
10 |
|
11 |
-
#
|
12 |
-
languages = ["English", "Hindi", "
|
13 |
|
14 |
-
# UI
|
15 |
st.markdown("""
|
16 |
<style>
|
17 |
.big-font { font-size:25px !important; font-weight: bold; }
|
18 |
-
.tab-style { display: flex; gap: 20px; font-size:18px; }
|
19 |
-
.tab-style div { cursor: pointer; }
|
20 |
.output-box { background-color: #f9f9f9; padding: 20px; border-radius: 10px; min-height: 300px; font-size: 22px; }
|
21 |
-
.copy-button { margin-top: 10px; }
|
22 |
</style>
|
23 |
""", unsafe_allow_html=True)
|
24 |
|
25 |
-
#
|
26 |
col1, col2 = st.columns(2)
|
27 |
|
28 |
with col1:
|
29 |
-
# Detected Language Dropdown
|
30 |
source_lang = st.selectbox("Detected Language", languages, index=0)
|
31 |
-
|
32 |
-
# Text input
|
33 |
st.markdown('<div class="big-font">Enter your text:</div>', unsafe_allow_html=True)
|
34 |
user_input = st.text_area("", "hi how are you", height=250)
|
35 |
-
|
36 |
-
# Translate Button
|
37 |
translate_button = st.button("Translate", use_container_width=True)
|
38 |
|
39 |
with col2:
|
40 |
-
# Target Language Dropdown
|
41 |
target_lang = st.selectbox("Translate To", languages, index=1)
|
42 |
|
43 |
if translate_button and user_input:
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
else:
|
48 |
translated_text = ""
|
49 |
-
|
50 |
-
# Show translated text
|
51 |
st.markdown('<div class="output-box">' + (translated_text if translated_text else "") + '</div>', unsafe_allow_html=True)
|
52 |
|
53 |
-
|
54 |
-
if translated_text:
|
55 |
st.button("Copy", use_container_width=True)
|
56 |
st.button("Paraphrase", use_container_width=True)
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Language pair to model mapping
|
5 |
+
model_mapping = {
|
6 |
+
("English", "Hindi"): "Helsinki-NLP/opus-mt-en-hi",
|
7 |
+
("Hindi", "English"): "ai4bharat/indic-translator-hi-en",
|
8 |
+
("English", "Telugu"): "ai4bharat/indic-translator-en-te",
|
9 |
+
("Telugu", "English"): "ai4bharat/indic-translator-te-en",
|
10 |
+
}
|
11 |
|
12 |
# Streamlit page config
|
13 |
st.set_page_config(page_title="Translator", layout="wide")
|
14 |
|
15 |
+
# Only allowed languages
|
16 |
+
languages = ["English", "Hindi", "Telugu"]
|
17 |
|
18 |
+
# UI
|
19 |
st.markdown("""
|
20 |
<style>
|
21 |
.big-font { font-size:25px !important; font-weight: bold; }
|
|
|
|
|
22 |
.output-box { background-color: #f9f9f9; padding: 20px; border-radius: 10px; min-height: 300px; font-size: 22px; }
|
|
|
23 |
</style>
|
24 |
""", unsafe_allow_html=True)
|
25 |
|
26 |
+
# Two columns
|
27 |
col1, col2 = st.columns(2)
|
28 |
|
29 |
with col1:
|
|
|
30 |
source_lang = st.selectbox("Detected Language", languages, index=0)
|
|
|
|
|
31 |
st.markdown('<div class="big-font">Enter your text:</div>', unsafe_allow_html=True)
|
32 |
user_input = st.text_area("", "hi how are you", height=250)
|
|
|
|
|
33 |
translate_button = st.button("Translate", use_container_width=True)
|
34 |
|
35 |
with col2:
|
|
|
36 |
target_lang = st.selectbox("Translate To", languages, index=1)
|
37 |
|
38 |
if translate_button and user_input:
|
39 |
+
if (source_lang, target_lang) in model_mapping:
|
40 |
+
model_name = model_mapping[(source_lang, target_lang)]
|
41 |
+
translator = pipeline("translation", model=model_name)
|
42 |
+
translated_text = translator(user_input, max_length=400)[0]['translation_text']
|
43 |
+
else:
|
44 |
+
translated_text = "❌ Translation not available for selected language pair."
|
45 |
else:
|
46 |
translated_text = ""
|
47 |
+
|
|
|
48 |
st.markdown('<div class="output-box">' + (translated_text if translated_text else "") + '</div>', unsafe_allow_html=True)
|
49 |
|
50 |
+
if translated_text and "❌" not in translated_text:
|
|
|
51 |
st.button("Copy", use_container_width=True)
|
52 |
st.button("Paraphrase", use_container_width=True)
|