saima730 commited on
Commit
fd778e9
·
verified ·
1 Parent(s): ca40be0

Update app.py

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