Sabina Muratova
commited on
Commit
•
bfa4aa2
1
Parent(s):
1b23224
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
|
4 |
+
def initialize_translator(model_name):
|
5 |
+
return pipeline("translation", model=model_name)
|
6 |
+
|
7 |
+
model_name = "Helsinki-NLP/opus-mt-en-ru"
|
8 |
+
translator = initialize_translator(model_name)
|
9 |
+
|
10 |
+
def translate_text(text):
|
11 |
+
if text:
|
12 |
+
result = translator(text)
|
13 |
+
return result[0]['translation_text']
|
14 |
+
return ""
|
15 |
+
|
16 |
+
st.title("Text Translation App")
|
17 |
+
|
18 |
+
st.sidebar.header("Settings")
|
19 |
+
language_pair = st.sidebar.selectbox(
|
20 |
+
"Choose language pair:",
|
21 |
+
[
|
22 |
+
"English to Russian (Helsinki-NLP/opus-mt-en-ru)",
|
23 |
+
"Russian to English (Helsinki-NLP/opus-mt-ru-en)"
|
24 |
+
]
|
25 |
+
)
|
26 |
+
|
27 |
+
if "Russian to English" in language_pair:
|
28 |
+
model_name = "Helsinki-NLP/opus-mt-ru-en"
|
29 |
+
else:
|
30 |
+
model_name = "Helsinki-NLP/opus-mt-en-ru"
|
31 |
+
|
32 |
+
translator = initialize_translator(model_name)
|
33 |
+
|
34 |
+
st.subheader("Enter text to translate:")
|
35 |
+
|
36 |
+
user_input = st.text_area("Your text here (e.g., 'The weather is nice today.'):", height=200)
|
37 |
+
|
38 |
+
if st.button("Translate"):
|
39 |
+
translation = translate_text(user_input)
|
40 |
+
st.subheader("Translated Text:")
|
41 |
+
st.write(translation)
|
42 |
+
else:
|
43 |
+
st.info("Enter text and click 'Translate' to see the result.")
|
44 |
+
|
45 |
+
|
46 |
+
if __name__ == '__main__':
|
47 |
+
import streamlit.web.cli as stcli
|
48 |
+
stcli.main()
|