AItool commited on
Commit
03b8d5a
·
verified ·
1 Parent(s): 07d9709

detect error fixed

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -1,6 +1,3 @@
1
- """
2
- @author: idoia lerchundi
3
- """
4
  import streamlit as st
5
  from language_directions import *
6
  from transformers import pipeline
@@ -69,19 +66,24 @@ def input_changed(source_language_dropdown, input_text=""):
69
 
70
  def translate(input_text, source, target):
71
  source_readable = source
 
 
72
  if source == "Detect" or source.startswith("Detected"):
73
- source, _ = auto_detect_language_code(input_text)
74
- if source in source_lang_dict.keys():
75
- source = source_lang_dict[source]
76
- target_lang_dict, _ = get_target_languages(source)
 
 
 
 
77
  try:
78
- target = target_lang_dict[target]
79
- model = f"Helsinki-NLP/opus-mt-{source}-{target}"
80
- pipe = pipeline("translation", model=model)
81
- translation = pipe(input_text)
82
- return translation[0]['translation_text']
83
  except KeyError:
84
- return f"Error: Translation from {source_readable} to {target} is not supported by Translation Models"
85
 
86
  with st.container():
87
  st.header("Basque (EU) Euskera Multilingual Translator (Basque as the Starting Point)")
 
 
 
 
1
  import streamlit as st
2
  from language_directions import *
3
  from transformers import pipeline
 
66
 
67
  def translate(input_text, source, target):
68
  source_readable = source
69
+
70
+ # Detect source if needed
71
  if source == "Detect" or source.startswith("Detected"):
72
+ source, _ = auto_detect_language_code(input_text)
73
+
74
+ # Map from human-readable name to code if needed
75
+ if source in source_lang_dict:
76
+ source = source_lang_dict[source]
77
+ if target in target_lang_dict:
78
+ target = target_lang_dict[target]
79
+
80
  try:
81
+ model_id = f"Helsinki-NLP/opus-mt-{source}-{target}"
82
+ pipe = pipeline("translation", model=model_id, device=-1) # CPU
83
+ translation = pipe(input_text)
84
+ return translation[0]['translation_text']
 
85
  except KeyError:
86
+ return f"Error: Translation from {source_readable} to {target} is not supported"
87
 
88
  with st.container():
89
  st.header("Basque (EU) Euskera Multilingual Translator (Basque as the Starting Point)")