saima730 commited on
Commit
228b24d
·
verified ·
1 Parent(s): 218785c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip install streamlit transformers
2
+ import streamlit as st
3
+ from transformers import pipeline
4
+
5
+ # Load the translation pipeline from Hugging Face
6
+ @st.cache_resource
7
+ def load_translation_pipeline(model_name="Helsinki-NLP/opus-mt-en-ROMANCE"):
8
+ return pipeline("translation", model=model_name)
9
+
10
+ # Function to translate text
11
+ def translate_text(translation_pipeline, text, target_language):
12
+ # Modify the model name to reflect the target language
13
+ if target_language == "French":
14
+ translation_pipeline.model_name = "Helsinki-NLP/opus-mt-en-fr"
15
+ elif target_language == "Spanish":
16
+ translation_pipeline.model_name = "Helsinki-NLP/opus-mt-en-es"
17
+ elif target_language == "German":
18
+ translation_pipeline.model_name = "Helsinki-NLP/opus-mt-en-de"
19
+ else:
20
+ st.error("Target language not supported!")
21
+ return None
22
+
23
+ translation = translation_pipeline(text)
24
+ return translation[0]['translation_text']
25
+
26
+ # Streamlit app layout
27
+ st.title("Language Translator")
28
+
29
+ # Input text to translate
30
+ text = st.text_area("Enter text in English to translate:")
31
+
32
+ # Select target language
33
+ target_language = st.selectbox(
34
+ "Select target language:",
35
+ ["French", "Spanish", "German"] # Add more languages if needed
36
+ )
37
+
38
+ # Load the translation pipeline
39
+ translation_pipeline = load_translation_pipeline()
40
+
41
+ # Translate button
42
+ if st.button("Translate"):
43
+ if text:
44
+ translated_text = translate_text(translation_pipeline, text, target_language)
45
+ if translated_text:
46
+ st.write(f"**Translated text in {target_language}:**
47
+ streamlit run your_script_name.py