Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the translation pipeline from Hugging Face
|
5 |
+
@st.cache_resource
|
6 |
+
def load_translation_pipeline(model_name):
|
7 |
+
return pipeline("translation", model=model_name)
|
8 |
+
|
9 |
+
# Dictionary of available models and target languages
|
10 |
+
models = {
|
11 |
+
"French": "Helsinki-NLP/opus-mt-en-fr",
|
12 |
+
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
13 |
+
"German": "Helsinki-NLP/opus-mt-en-de",
|
14 |
+
"Chinese": "Helsinki-NLP/opus-mt-en-zh",
|
15 |
+
"Hindi": "Helsinki-NLP/opus-mt-en-hi",
|
16 |
+
}
|
17 |
+
|
18 |
+
# Streamlit app
|
19 |
+
def main():
|
20 |
+
st.title("Language Translator")
|
21 |
+
|
22 |
+
# Input text from the user
|
23 |
+
text_to_translate = st.text_area("Enter the text in English:")
|
24 |
+
|
25 |
+
# Select the target language
|
26 |
+
target_language = st.selectbox("Select the target language:", list(models.keys()))
|
27 |
+
|
28 |
+
# Translate button
|
29 |
+
if st.button("Translate"):
|
30 |
+
# Load the appropriate translation model
|
31 |
+
translation_pipeline = load_translation_pipeline(models[target_language])
|
32 |
+
|
33 |
+
# Translate the text
|
34 |
+
if text_to_translate.strip():
|
35 |
+
translated_text = translation_pipeline(text_to_translate)[0]['translation_text']
|
36 |
+
st.success(f"Translated Text ({target_language}):")
|
37 |
+
st.write(translated_text)
|
38 |
+
else:
|
39 |
+
st.error("Please enter some text to translate.")
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
main()
|