richylyq commited on
Commit
592978b
1 Parent(s): 0d22421

if else for same language for target and result

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -34,11 +34,7 @@ def detect_lang(article, target_lang):
34
  """
35
 
36
  result_lang = langid.classify(article)
37
- print(result_lang[0])
38
- if result_lang == target_lang:
39
- return result_lang[0]
40
- else:
41
- return result_lang[0]
42
 
43
 
44
  def opus_trans(article, target_language):
@@ -54,17 +50,21 @@ def opus_trans(article, target_language):
54
  string: translated piece of article based off target_language
55
  """
56
  result_lang = detect_lang(article, target_language)
 
57
  if target_language == "English":
58
  target_lang = "en"
59
  elif target_language == "Chinese":
60
  target_lang = "zh"
61
  # result_lang = detect_lang(article, target_language)
62
 
63
- task_name = f"translation_{result_lang}_to_{target_lang}"
64
- model_name = f"Helsinki-NLP/opus-mt-{result_lang}-{target_lang}"
65
- translator = pipeline(task_name, model=model_name, tokenizer=model_name)
66
- translated = translator(article)[0]["translation_text"]
67
- print(translated)
 
 
 
68
  return translated
69
 
70
 
 
34
  """
35
 
36
  result_lang = langid.classify(article)
37
+ return result_lang[0]
 
 
 
 
38
 
39
 
40
  def opus_trans(article, target_language):
 
50
  string: translated piece of article based off target_language
51
  """
52
  result_lang = detect_lang(article, target_language)
53
+
54
  if target_language == "English":
55
  target_lang = "en"
56
  elif target_language == "Chinese":
57
  target_lang = "zh"
58
  # result_lang = detect_lang(article, target_language)
59
 
60
+ if result_lang != target_lang:
61
+ task_name = f"translation_{result_lang}_to_{target_lang}"
62
+ model_name = f"Helsinki-NLP/opus-mt-{result_lang}-{target_lang}"
63
+ translator = pipeline(task_name, model=model_name, tokenizer=model_name)
64
+ translated = translator(article)[0]["translation_text"]
65
+ # print(translated)
66
+ else:
67
+ translated = "Error"
68
  return translated
69
 
70