wiqasali commited on
Commit
381bc5d
·
verified ·
1 Parent(s): e152d9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -24
app.py CHANGED
@@ -1,27 +1,41 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
-
4
- # Initialize the translation pipeline from Hugging Face
5
- @st.cache_resource
6
- def load_translation_model():
7
- return pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
8
-
9
- translator = load_translation_model()
10
-
11
- # Streamlit app layout
12
- st.title("English to Urdu Translator")
13
-
14
- # Input text in English
15
- english_text = st.text_area("Enter English text", placeholder="Type something in English...", height=150)
16
-
17
- # Translate button
18
- if st.button("Translate to Urdu"):
19
- if english_text.strip() != "":
20
- translation = translator(english_text)
21
- urdu_text = translation[0]['translation_text']
22
- st.write("### Translated Text in Urdu:")
23
- st.write(urdu_text)
24
- else:
25
- st.warning("Please enter some text to translate!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
 
 
1
  import streamlit as st
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+
4
+ # Supported languages
5
+ supported_languages = {
6
+ 'English to Urdu': 'Helsinki-NLP/opus-mt-en-ur',
7
+ 'English to French': 'Helsinki-NLP/opus-mt-en-fr',
8
+ 'English to Spanish': 'Helsinki-NLP/opus-mt-en-es',
9
+ 'French to English': 'Helsinki-NLP/opus-mt-fr-en',
10
+ 'Spanish to English': 'Helsinki-NLP/opus-mt-es-en',
11
+ 'Urdu to English': 'Helsinki-NLP/opus-mt-ur-en'
12
+ }
13
+
14
+ # Streamlit App Title
15
+ st.title("Multi-Language Translator")
16
+
17
+ # Input Text
18
+ input_text = st.text_area("Enter the text you want to translate:")
19
+
20
+ # Language selection dropdown
21
+ language_pair = st.selectbox("Choose the language pair:", list(supported_languages.keys()))
22
+
23
+ # Load the model and tokenizer for the selected language pair
24
+ if input_text:
25
+ model_name = supported_languages[language_pair]
26
+ model = MarianMTModel.from_pretrained(model_name)
27
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
28
+
29
+ # Tokenize and translate
30
+ translated = model.generate(**tokenizer(input_text, return_tensors="pt", padding=True))
31
+ translated_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
32
+
33
+ # Display the translated text
34
+ st.subheader("Translated Text:")
35
+ st.write(translated_text[0])
36
+
37
+ # Footer
38
+ st.markdown("Powered by Helsinki-NLP/Opus-MT and Hugging Face")
39
+
40
 
41