TANVEERMAKHDOOM
commited on
Commit
•
b3fa491
1
Parent(s):
ded024c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
|
4 |
+
# Define a dictionary of available languages and their corresponding Hugging Face models
|
5 |
+
LANGUAGE_MODELS = {
|
6 |
+
"French": "Helsinki-NLP/opus-mt-en-fr",
|
7 |
+
"German": "Helsinki-NLP/opus-mt-en-de",
|
8 |
+
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
9 |
+
"Italian": "Helsinki-NLP/opus-mt-en-it",
|
10 |
+
"Portuguese": "Helsinki-NLP/opus-mt-en-pt",
|
11 |
+
# Add more languages and their models here
|
12 |
+
}
|
13 |
+
|
14 |
+
def load_model(language):
|
15 |
+
model_name = LANGUAGE_MODELS.get(language)
|
16 |
+
if model_name is None:
|
17 |
+
st.error("Selected language not supported.")
|
18 |
+
return None, None
|
19 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
20 |
+
model = MarianMTModel.from_pretrained(model_name)
|
21 |
+
return tokenizer, model
|
22 |
+
|
23 |
+
def translate_text(text, tokenizer, model):
|
24 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
25 |
+
translated = model.generate(**inputs)
|
26 |
+
translation = tokenizer.decode(translated[0], skip_special_tokens=True)
|
27 |
+
return translation
|
28 |
+
|
29 |
+
def main():
|
30 |
+
st.title("Language Translator")
|
31 |
+
|
32 |
+
st.write("This app translates English text into selected languages using Hugging Face models.")
|
33 |
+
|
34 |
+
# Select target language
|
35 |
+
target_language = st.selectbox("Select target language", list(LANGUAGE_MODELS.keys()))
|
36 |
+
|
37 |
+
# Input text
|
38 |
+
input_text = st.text_area("Enter text in English")
|
39 |
+
|
40 |
+
if st.button("Translate"):
|
41 |
+
if not input_text:
|
42 |
+
st.error("Please enter text to translate.")
|
43 |
+
else:
|
44 |
+
# Load the model and tokenizer for the selected language
|
45 |
+
tokenizer, model = load_model(target_language)
|
46 |
+
if tokenizer and model:
|
47 |
+
# Translate the text
|
48 |
+
translated_text = translate_text(input_text, tokenizer, model)
|
49 |
+
st.subheader("Translated Text")
|
50 |
+
st.write(translated_text)
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
main()
|
54 |
+
|
55 |
+
streamlit run app.py
|