Update app.py
Browse files
app.py
CHANGED
@@ -2,74 +2,94 @@ import streamlit as st
|
|
2 |
import sacrebleu
|
3 |
from bert_score import score as bert_score
|
4 |
import jieba
|
5 |
-
import torch
|
6 |
-
|
7 |
-
# Ensure CUDA is not used to avoid potential issues on Hugging Face Spaces
|
8 |
-
torch.cuda.is_available = lambda : False
|
9 |
|
|
|
10 |
def calculate_bleu(translations, references):
|
11 |
return sacrebleu.corpus_bleu(translations, [references]).score
|
12 |
|
|
|
13 |
def calculate_ter(translations, references):
|
14 |
return sacrebleu.corpus_ter(translations, [references]).score
|
15 |
|
|
|
16 |
def calculate_chrf(translations, references):
|
17 |
return sacrebleu.corpus_chrf(translations, [references]).score
|
18 |
|
|
|
19 |
def calculate_bertscore(translations, references, lang):
|
20 |
-
P, R, F1 = bert_score(translations, references, lang=lang
|
21 |
return F1.mean().item()
|
22 |
|
23 |
-
|
24 |
-
return ' '.join(jieba.cut(text))
|
25 |
-
|
26 |
st.title("Machine Translation Quality Evaluation")
|
27 |
st.write("Input the translated text and the reference translation to compute BLEU, TER, CHRF, and BERTScore metrics.")
|
28 |
|
|
|
29 |
languages = {
|
30 |
-
"English": "en",
|
31 |
-
"
|
32 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
}
|
34 |
|
|
|
35 |
source_lang = st.selectbox("Select Source Language", list(languages.keys()))
|
36 |
target_lang = st.selectbox("Select Target Language", list(languages.keys()))
|
37 |
|
38 |
-
|
39 |
-
|
|
|
40 |
|
|
|
41 |
translation_input = st.text_area("Translated Text", height=200)
|
42 |
reference_input = st.text_area("Reference Translation", height=200)
|
43 |
|
|
|
44 |
if st.button("Evaluate"):
|
45 |
if translation_input and reference_input:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
chrf_score = calculate_chrf(translations, references)
|
64 |
-
bertscore = calculate_bertscore(translations, references, target_lang_code)
|
65 |
-
|
66 |
-
st.write("Evaluation Scores:")
|
67 |
-
st.write(f"**BLEU Score:** {bleu_score:.2f}")
|
68 |
-
st.write(f"**TER Score:** {ter_score:.2f}")
|
69 |
-
st.write(f"**CHRF Score:** {chrf_score:.2f}")
|
70 |
-
st.write(f"**BERTScore:** {bertscore:.2f}")
|
71 |
-
|
72 |
-
except Exception as e:
|
73 |
-
st.error(f"An error occurred: {str(e)}")
|
74 |
else:
|
75 |
st.error("Please provide both translated text and reference translation.")
|
|
|
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):
|
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
|
|
|
|
|
24 |
st.title("Machine Translation Quality Evaluation")
|
25 |
st.write("Input the translated text and the reference translation to compute BLEU, TER, CHRF, and BERTScore metrics.")
|
26 |
|
27 |
+
# List of supported languages
|
28 |
languages = {
|
29 |
+
"English": "en",
|
30 |
+
"Chinese": "zh",
|
31 |
+
"French": "fr",
|
32 |
+
"German": "de",
|
33 |
+
"Spanish": "es",
|
34 |
+
"Russian": "ru",
|
35 |
+
"Japanese": "ja",
|
36 |
+
"Korean": "ko",
|
37 |
+
"Arabic": "ar",
|
38 |
+
"Italian": "it",
|
39 |
+
"Dutch": "nl",
|
40 |
+
"Portuguese": "pt",
|
41 |
+
"Turkish": "tr",
|
42 |
+
"Polish": "pl",
|
43 |
+
"Czech": "cs",
|
44 |
+
"Swedish": "sv",
|
45 |
+
"Danish": "da",
|
46 |
+
"Finnish": "fi",
|
47 |
+
"Greek": "el",
|
48 |
+
"Hungarian": "hu",
|
49 |
+
"Indonesian": "id",
|
50 |
+
"Norwegian": "no",
|
51 |
+
"Romanian": "ro",
|
52 |
+
"Thai": "th",
|
53 |
+
"Vietnamese": "vi",
|
54 |
+
"Hebrew": "he",
|
55 |
+
"Hindi": "hi",
|
56 |
+
"Bengali": "bn",
|
57 |
+
"Tamil": "ta",
|
58 |
+
"Urdu": "ur",
|
59 |
+
"Other": "other"
|
60 |
}
|
61 |
|
62 |
+
# Language selection
|
63 |
source_lang = st.selectbox("Select Source Language", list(languages.keys()))
|
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)
|
72 |
reference_input = st.text_area("Reference Translation", 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.")
|