Spaces:
Sleeping
Sleeping
engr-awaisjamal
commited on
Create requirements.txt
Browse files- requirements.txt +51 -0
requirements.txt
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
|
4 |
+
# Function to load model and tokenizer
|
5 |
+
@st.cache_resource
|
6 |
+
def load_model_and_tokenizer(model_name):
|
7 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
8 |
+
model = MarianMTModel.from_pretrained(model_name)
|
9 |
+
return tokenizer, model
|
10 |
+
|
11 |
+
# Function to perform translation
|
12 |
+
def translate_text(text, tokenizer, model):
|
13 |
+
tokenized_text = tokenizer.prepare_seq2seq_batch([text], return_tensors="pt", padding=True)
|
14 |
+
translated_tokens = model.generate(**tokenized_text)
|
15 |
+
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
16 |
+
return translated_text
|
17 |
+
|
18 |
+
# Available language pairs (from Helsinki-NLP models)
|
19 |
+
language_pairs = {
|
20 |
+
"English to French": "Helsinki-NLP/opus-mt-en-fr",
|
21 |
+
"French to English": "Helsinki-NLP/opus-mt-fr-en",
|
22 |
+
"English to German": "Helsinki-NLP/opus-mt-en-de",
|
23 |
+
"German to English": "Helsinki-NLP/opus-mt-de-en",
|
24 |
+
"English to Spanish": "Helsinki-NLP/opus-mt-en-es",
|
25 |
+
"Spanish to English": "Helsinki-NLP/opus-mt-es-en",
|
26 |
+
# Add more pairs as needed
|
27 |
+
}
|
28 |
+
|
29 |
+
# Streamlit App
|
30 |
+
st.title("Language Translation App")
|
31 |
+
st.write("Translate text between multiple languages using open-source models.")
|
32 |
+
|
33 |
+
# User selects language pair
|
34 |
+
language_pair = st.selectbox("Select Language Pair:", list(language_pairs.keys()))
|
35 |
+
model_name = language_pairs[language_pair]
|
36 |
+
|
37 |
+
# Load model and tokenizer
|
38 |
+
with st.spinner("Loading translation model..."):
|
39 |
+
tokenizer, model = load_model_and_tokenizer(model_name)
|
40 |
+
|
41 |
+
# Input text
|
42 |
+
input_text = st.text_area("Enter text to translate:")
|
43 |
+
|
44 |
+
if st.button("Translate"):
|
45 |
+
if input_text.strip():
|
46 |
+
with st.spinner("Translating..."):
|
47 |
+
translated_text = translate_text(input_text, tokenizer, model)
|
48 |
+
st.success("Translation complete!")
|
49 |
+
st.text_area("Translated Text:", translated_text, height=200)
|
50 |
+
else:
|
51 |
+
st.warning("Please enter text to translate.")
|