KubraBashir commited on
Commit
313db47
·
verified ·
1 Parent(s): 825e88a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ torch
4
+ sentencepiece
5
+ sacremoses
6
+
7
+ import streamlit as st
8
+ from transformers import pipeline
9
+
10
+ # Initialize the translation pipeline
11
+ @st.cache_resource
12
+ def load_translator():
13
+ return pipeline("translation", model="Helsinki-NLP/opus-mt-en-{target}")
14
+
15
+ # Supported languages (ISO 639-1 codes mapped to language names)
16
+ supported_languages = {
17
+ "fr": "French",
18
+ "es": "Spanish",
19
+ "de": "German",
20
+ "zh": "Chinese",
21
+ "hi": "Hindi",
22
+ "ar": "Arabic",
23
+ "ru": "Russian",
24
+ "ja": "Japanese",
25
+ "ko": "Korean",
26
+ "it": "Italian",
27
+ }
28
+
29
+ # Streamlit App
30
+ st.title("Language Translator App")
31
+ st.write("Translate text from English to a selected target language using Hugging Face models.")
32
+
33
+ # Input text from user
34
+ input_text = st.text_area("Enter text in English:", placeholder="Type here...")
35
+
36
+ # Language selection
37
+ target_language = st.selectbox(
38
+ "Select target language:",
39
+ options=list(supported_languages.keys()),
40
+ format_func=lambda lang: supported_languages[lang],
41
+ )
42
+
43
+ # Translate button
44
+ if st.button("Translate"):
45
+ if input_text.strip() == "":
46
+ st.error("Please enter text to translate.")
47
+ else:
48
+ translator = load_translator()
49
+ # Replace `{target}` with the user-selected language in the model
50
+ translator = pipeline("translation", model=f"Helsinki-NLP/opus-mt-en-{target_language}")
51
+ translation = translator(input_text)[0]["translation_text"]
52
+ st.success("Translated Text:")
53
+ st.write(translation)