Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,19 @@ import jieba
|
|
| 5 |
|
| 6 |
# Function to calculate BLEU score
|
| 7 |
def calculate_bleu(translations, references):
|
| 8 |
-
|
| 9 |
-
return bleu.score
|
| 10 |
|
| 11 |
# Function to calculate TER score
|
| 12 |
def calculate_ter(translations, references):
|
| 13 |
-
|
| 14 |
-
ter_score = ter.score
|
| 15 |
-
return ter_score
|
| 16 |
|
| 17 |
# Function to calculate CHRF score
|
| 18 |
def calculate_chrf(translations, references):
|
| 19 |
-
|
| 20 |
-
return chrf.score
|
| 21 |
|
| 22 |
# Function to calculate BERTScore
|
| 23 |
-
def calculate_bertscore(translations, references,
|
| 24 |
-
P, R, F1 = bert_score(translations, references, lang=
|
| 25 |
return F1.mean().item()
|
| 26 |
|
| 27 |
# Streamlit app
|
|
@@ -68,15 +64,8 @@ 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 |
-
|
| 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)
|
|
@@ -84,22 +73,23 @@ reference_input = st.text_area("Reference Translation", height=200)
|
|
| 84 |
|
| 85 |
# Evaluate button
|
| 86 |
if st.button("Evaluate"):
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
| 5 |
|
| 6 |
# Function to calculate BLEU score
|
| 7 |
def calculate_bleu(translations, references):
|
| 8 |
+
return sacrebleu.corpus_bleu(translations, [references]).score
|
|
|
|
| 9 |
|
| 10 |
# Function to calculate TER score
|
| 11 |
def calculate_ter(translations, references):
|
| 12 |
+
return sacrebleu.corpus_ter(translations, [references]).score
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Function to calculate CHRF score
|
| 15 |
def calculate_chrf(translations, references):
|
| 16 |
+
return sacrebleu.corpus_chrf(translations, [references]).score
|
|
|
|
| 17 |
|
| 18 |
# Function to calculate BERTScore
|
| 19 |
+
def calculate_bertscore(translations, references, lang):
|
| 20 |
+
P, R, F1 = bert_score(translations, references, lang=lang)
|
| 21 |
return F1.mean().item()
|
| 22 |
|
| 23 |
# Streamlit app
|
|
|
|
| 64 |
target_lang = st.selectbox("Select Target Language", list(languages.keys()))
|
| 65 |
|
| 66 |
# Input fields for custom language codes if "Other" is selected
|
| 67 |
+
source_lang_code = st.text_input("Enter Source Language Code (ISO 639-1):", value=languages[source_lang]) if source_lang == "Other" else languages[source_lang]
|
| 68 |
+
target_lang_code = st.text_input("Enter Target Language Code (ISO 639-1):", value=languages[target_lang]) if target_lang == "Other" else languages[target_lang]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Input fields for translations and references
|
| 71 |
translation_input = st.text_area("Translated Text", height=200)
|
|
|
|
| 73 |
|
| 74 |
# Evaluate button
|
| 75 |
if st.button("Evaluate"):
|
| 76 |
+
if translation_input and reference_input:
|
| 77 |
+
translations = [translation_input.strip()]
|
| 78 |
+
references = [reference_input.strip()]
|
| 79 |
+
|
| 80 |
+
# Handle tokenization if necessary (e.g., for Chinese)
|
| 81 |
+
if source_lang_code == "zh" or target_lang_code == "zh":
|
| 82 |
+
translations = [' '.join(jieba.cut(text)) for text in translations]
|
| 83 |
+
references = [' '.join(jieba.cut(text)) for text in references]
|
| 84 |
+
|
| 85 |
+
bleu_score = calculate_bleu(translations, references)
|
| 86 |
+
ter_score = calculate_ter(translations, references)
|
| 87 |
+
chrf_score = calculate_chrf(translations, references)
|
| 88 |
+
bertscore = calculate_bertscore(translations, references, target_lang_code)
|
| 89 |
+
|
| 90 |
+
st.write(f"**BLEU Score:** {bleu_score:.2f}")
|
| 91 |
+
st.write(f"**TER Score:** {ter_score:.2f}")
|
| 92 |
+
st.write(f"**CHRF Score:** {chrf_score:.2f}")
|
| 93 |
+
st.write(f"**BERTScore:** {bertscore:.2f}")
|
| 94 |
+
else:
|
| 95 |
+
st.error("Please provide both translated text and reference translation.")
|