KevSun commited on
Commit
42fcd4b
·
verified ·
1 Parent(s): a0ca7bd

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -5
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import sacrebleu
3
  from bert_score import score as bert_score
 
4
 
5
  # Function to calculate BLEU score
6
  def calculate_bleu(translations, references):
@@ -19,27 +20,83 @@ def calculate_chrf(translations, references):
19
  return chrf.score
20
 
21
  # Function to calculate BERTScore
22
- def calculate_bertscore(translations, references):
23
- P, R, F1 = bert_score(translations, references, lang="en")
24
  return F1.mean().item()
25
 
26
  # Streamlit app
27
  st.title("Machine Translation Quality Evaluation")
28
  st.write("Input the translated text and the reference translation to compute BLEU, TER, CHRF, and BERTScore metrics.")
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # Input fields for translations and references
31
  translation_input = st.text_area("Translated Text", height=200)
32
  reference_input = st.text_area("Reference Translation", height=200)
33
 
34
  # Evaluate button
35
  if st.button("Evaluate"):
36
- translations = [translation_input]
37
- references = [reference_input]
 
 
 
 
 
38
 
39
  bleu_score = calculate_bleu(translations, references)
40
  ter_score = calculate_ter(translations, references)
41
  chrf_score = calculate_chrf(translations, references)
42
- bertscore = calculate_bertscore(translations, references)
 
43
 
44
  st.write(f"**BLEU Score:** {bleu_score:.2f}")
45
  st.write(f"**TER Score:** {ter_score:.2f}")
 
1
  import streamlit as st
2
  import sacrebleu
3
  from bert_score import score as bert_score
4
+ import jieba
5
 
6
  # Function to calculate BLEU score
7
  def calculate_bleu(translations, references):
 
20
  return chrf.score
21
 
22
  # Function to calculate BERTScore
23
+ def calculate_bertscore(translations, references, target_lang):
24
+ P, R, F1 = bert_score(translations, references, lang=target_lang)
25
  return F1.mean().item()
26
 
27
  # Streamlit app
28
  st.title("Machine Translation Quality Evaluation")
29
  st.write("Input the translated text and the reference translation to compute BLEU, TER, CHRF, and BERTScore metrics.")
30
 
31
+ # List of supported languages
32
+ languages = {
33
+ "English": "en",
34
+ "Chinese": "zh",
35
+ "French": "fr",
36
+ "German": "de",
37
+ "Spanish": "es",
38
+ "Russian": "ru",
39
+ "Japanese": "ja",
40
+ "Korean": "ko",
41
+ "Arabic": "ar",
42
+ "Italian": "it",
43
+ "Dutch": "nl",
44
+ "Portuguese": "pt",
45
+ "Turkish": "tr",
46
+ "Polish": "pl",
47
+ "Czech": "cs",
48
+ "Swedish": "sv",
49
+ "Danish": "da",
50
+ "Finnish": "fi",
51
+ "Greek": "el",
52
+ "Hungarian": "hu",
53
+ "Indonesian": "id",
54
+ "Norwegian": "no",
55
+ "Romanian": "ro",
56
+ "Thai": "th",
57
+ "Vietnamese": "vi",
58
+ "Hebrew": "he",
59
+ "Hindi": "hi",
60
+ "Bengali": "bn",
61
+ "Tamil": "ta",
62
+ "Urdu": "ur",
63
+ "Other": "other"
64
+ }
65
+
66
+ # Language selection
67
+ source_lang = st.selectbox("Select Source Language", list(languages.keys()))
68
+ target_lang = st.selectbox("Select Target Language", list(languages.keys()))
69
+
70
+ # Input fields for custom language codes if "Other" is selected
71
+ if source_lang == "Other":
72
+ source_lang_code = st.text_input("Enter Source Language Code (ISO 639-1):")
73
+ else:
74
+ source_lang_code = languages[source_lang]
75
+
76
+ if target_lang == "Other":
77
+ target_lang_code = st.text_input("Enter Target Language Code (ISO 639-1):")
78
+ else:
79
+ target_lang_code = languages[target_lang]
80
+
81
  # Input fields for translations and references
82
  translation_input = st.text_area("Translated Text", height=200)
83
  reference_input = st.text_area("Reference Translation", height=200)
84
 
85
  # Evaluate button
86
  if st.button("Evaluate"):
87
+ translations = [translation_input.strip()]
88
+ references = [reference_input.strip()]
89
+
90
+ # Handle tokenization if necessary (e.g., for Chinese)
91
+ if source_lang_code == "zh" or target_lang_code == "zh":
92
+ translations = [' '.join(jieba.cut(text)) for text in translations]
93
+ references = [' '.join(jieba.cut(text)) for text in references]
94
 
95
  bleu_score = calculate_bleu(translations, references)
96
  ter_score = calculate_ter(translations, references)
97
  chrf_score = calculate_chrf(translations, references)
98
+
99
+ bertscore = calculate_bertscore(translations, references, target_lang_code)
100
 
101
  st.write(f"**BLEU Score:** {bleu_score:.2f}")
102
  st.write(f"**TER Score:** {ter_score:.2f}")