Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 7 |
+
bleu = sacrebleu.corpus_bleu(translations, [references])
|
| 8 |
+
return bleu.score
|
| 9 |
+
|
| 10 |
+
# Function to calculate TER score
|
| 11 |
+
def calculate_ter(translations, references):
|
| 12 |
+
ter = sacrebleu.corpus_ter(translations, [references])
|
| 13 |
+
ter_score = ter.score
|
| 14 |
+
return ter_score
|
| 15 |
+
|
| 16 |
+
# Function to calculate CHRF score
|
| 17 |
+
def calculate_chrf(translations, references):
|
| 18 |
+
chrf = sacrebleu.corpus_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}")
|
| 46 |
+
st.write(f"**CHRF Score:** {chrf_score:.2f}")
|
| 47 |
+
st.write(f"**BERTScore:** {bertscore:.2f}")
|
| 48 |
+
|