Spaces:
Build error
Build error
Create eval.py
Browse files
eval.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
# Read source sentences and translations from an Excel file
|
| 5 |
+
excel_file = "demo.xlsx" # Path to your Excel file
|
| 6 |
+
df = pd.read_excel(excel_file)
|
| 7 |
+
|
| 8 |
+
# Assuming the Excel file has columns: "Source", "Translation_1", "Translation_2", ..., "Translation_5"
|
| 9 |
+
source_col = "Source Sentence"
|
| 10 |
+
translation_cols = [f"Translation_{i}" for i in range(1, 6)]
|
| 11 |
+
|
| 12 |
+
# Store the result scores after evaluation
|
| 13 |
+
#result_columns = ['Source', 'Translation', 'Accuracy', 'Fluency', 'Overall']
|
| 14 |
+
result_columns = ['Source', 'Translation', 'Rating']
|
| 15 |
+
|
| 16 |
+
# Function to simulate the rating collection and return scores
|
| 17 |
+
def evaluate_translations(*ratings):
|
| 18 |
+
# Initialize lists to store results for all translations
|
| 19 |
+
sources = []
|
| 20 |
+
translations = []
|
| 21 |
+
#accuracy_ratings = []
|
| 22 |
+
#fluency_ratings = []
|
| 23 |
+
overall_ratings = []
|
| 24 |
+
name = ratings[0]
|
| 25 |
+
for item in ratings:
|
| 26 |
+
if type(item)==int:
|
| 27 |
+
overall_ratings.append(item)
|
| 28 |
+
|
| 29 |
+
for idx, row in df.iterrows():
|
| 30 |
+
source = row[source_col]
|
| 31 |
+
translations_list = [row[translation_col] for translation_col in translation_cols]
|
| 32 |
+
|
| 33 |
+
# Collect ratings for each translation
|
| 34 |
+
for i, translation in enumerate(translations_list):
|
| 35 |
+
|
| 36 |
+
sources.append(source)
|
| 37 |
+
translations.append(translation)
|
| 38 |
+
|
| 39 |
+
# Store the results in a DataFrame
|
| 40 |
+
results_df = pd.DataFrame({
|
| 41 |
+
'Source': sources,
|
| 42 |
+
'Translation': translations,
|
| 43 |
+
#'Accuracy': accuracy_ratings,
|
| 44 |
+
#'Fluency': fluency_ratings,
|
| 45 |
+
'Overall': overall_ratings
|
| 46 |
+
})
|
| 47 |
+
|
| 48 |
+
# Save the result to an Excel file
|
| 49 |
+
filename = name+"_evaluation_results.xlsx"
|
| 50 |
+
results_df.to_excel(filename, index=False)
|
| 51 |
+
return "Evaluation complete. Results have been saved to" + filename + "."
|
| 52 |
+
|
| 53 |
+
# Function to create the input sliders dynamically for each translation
|
| 54 |
+
def get_inputs():
|
| 55 |
+
inputs = []
|
| 56 |
+
inputs.append(gr.Textbox(label="User Name :", interactive=True))
|
| 57 |
+
for idx, row in df.iterrows():
|
| 58 |
+
source = row[source_col]
|
| 59 |
+
translations_list = [row[translation_col] for translation_col in translation_cols]
|
| 60 |
+
|
| 61 |
+
# Source sentence display (non-editable)
|
| 62 |
+
|
| 63 |
+
inputs.append(gr.Textbox(value=source, label=f"Source Sentence {idx+1}", interactive=False))
|
| 64 |
+
|
| 65 |
+
# Add 3 sliders for each translation (accuracy, fluency, overall)
|
| 66 |
+
for i, translation in enumerate(translations_list):
|
| 67 |
+
with gr.Row():
|
| 68 |
+
inputs.append(gr.Textbox(value=translation, label=f"Translation {i+1}"))
|
| 69 |
+
#inputs.append(gr.Slider(minimum=1, maximum=5, step=1, label=f"Accuracy (Translation {i+1})"))
|
| 70 |
+
#inputs.append(gr.Slider(minimum=1, maximum=5, step=1, label=f"Fluency (Translation {i+1})"))
|
| 71 |
+
inputs.append(gr.Slider(minimum=1, maximum=5, step=1, label=f"Rating (Translation {i+1})"))
|
| 72 |
+
|
| 73 |
+
return inputs
|
| 74 |
+
|
| 75 |
+
# Define the Gradio interface outputs
|
| 76 |
+
def get_outputs():
|
| 77 |
+
return gr.Textbox(label="Evaluation Status")
|
| 78 |
+
|
| 79 |
+
# Create the Gradio interface
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
# Inputs
|
| 82 |
+
inputs = get_inputs()
|
| 83 |
+
|
| 84 |
+
# Outputs
|
| 85 |
+
output = get_outputs()
|
| 86 |
+
|
| 87 |
+
# Add a Submit Button
|
| 88 |
+
submit_button = gr.Button("Submit Evaluation")
|
| 89 |
+
|
| 90 |
+
# Bind the button to the evaluation function
|
| 91 |
+
submit_button.click(evaluate_translations, inputs=inputs, outputs=output)
|
| 92 |
+
|
| 93 |
+
# Launch the interface
|
| 94 |
+
demo.launch()
|