Moezulhaq24 commited on
Commit
a4a9246
·
verified ·
1 Parent(s): a616f63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -1,42 +1,40 @@
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()
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
+ # Define model mapping for each target language
5
+ model_mapping = {
 
 
 
 
 
6
  "French": "Helsinki-NLP/opus-mt-en-fr",
7
  "Spanish": "Helsinki-NLP/opus-mt-en-es",
8
  "German": "Helsinki-NLP/opus-mt-en-de",
9
+ "Italian": "Helsinki-NLP/opus-mt-en-it",
10
+ "Urdu": "Helsinki-NLP/opus-mt-en-ur" # Added Urdu translation
11
  }
12
 
13
  # Streamlit app
14
+ st.title("Language Translator")
15
+
16
+ # Sidebar for language selection and translation output
17
+ with st.sidebar:
18
+ st.header("Translate Text")
19
+
20
+ # Input text
21
+ text = st.text_area("Enter text in English:")
22
+
23
+ # Target language selection
24
+ selected_language = st.selectbox("Select target language:", list(model_mapping.keys()))
25
+
26
+ # Translation button
27
  if st.button("Translate"):
28
+ if text:
29
+ # Get the appropriate model based on selected language
30
+ model_name = model_mapping[selected_language]
31
+ translator = pipeline("translation", model=model_name)
32
+
33
+ # Perform translation
34
+ translation = translator(text, max_length=400)[0]['translation_text']
35
+
36
+ # Display translation
37
+ st.write(f"*Translation in {selected_language}:*")
38
+ st.write(translation)
39
  else:
40
+ st.write("Please enter some text to translate.")