Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
# Read source sentences and translations from an Excel file | |
excel_file = "demo.xlsx" # Path to your Excel file | |
df = pd.read_excel(excel_file) | |
# Assuming the Excel file has columns: "Source", "Translation_1", "Translation_2", ..., "Translation_5" | |
source_col = "Source Sentence" | |
translation_cols = [f"Translation_{i}" for i in range(1, 6)] | |
# Store the result scores after evaluation | |
#result_columns = ['Source', 'Translation', 'Accuracy', 'Fluency', 'Overall'] | |
result_columns = ['Source', 'Translation', 'Rating'] | |
# Function to simulate the rating collection and return scores | |
def evaluate_translations(*ratings): | |
# Initialize lists to store results for all translations | |
sources = [] | |
translations = [] | |
#accuracy_ratings = [] | |
#fluency_ratings = [] | |
overall_ratings = [] | |
name = ratings[0] | |
for item in ratings: | |
if type(item)==int: | |
overall_ratings.append(item) | |
for idx, row in df.iterrows(): | |
source = row[source_col] | |
translations_list = [row[translation_col] for translation_col in translation_cols] | |
# Collect ratings for each translation | |
for i, translation in enumerate(translations_list): | |
sources.append(source) | |
translations.append(translation) | |
# Store the results in a DataFrame | |
results_df = pd.DataFrame({ | |
'Source': sources, | |
'Translation': translations, | |
#'Accuracy': accuracy_ratings, | |
#'Fluency': fluency_ratings, | |
'Overall': overall_ratings | |
}) | |
# Save the result to an Excel file | |
filename = name+"_evaluation_results.xlsx" | |
results_df.to_excel(filename, index=False) | |
return "Evaluation complete. Results have been saved to" + filename + "." | |
# Function to create the input sliders dynamically for each translation | |
def get_inputs(): | |
inputs = [] | |
inputs.append(gr.Textbox(label="User Name :", interactive=True)) | |
for idx, row in df.iterrows(): | |
source = row[source_col] | |
translations_list = [row[translation_col] for translation_col in translation_cols] | |
# Source sentence display (non-editable) | |
inputs.append(gr.Textbox(value=source, label=f"Source Sentence {idx+1}", interactive=False)) | |
# Add 3 sliders for each translation (accuracy, fluency, overall) | |
for i, translation in enumerate(translations_list): | |
with gr.Row(): | |
inputs.append(gr.Textbox(value=translation, label=f"Translation {i+1}")) | |
#inputs.append(gr.Slider(minimum=1, maximum=5, step=1, label=f"Accuracy (Translation {i+1})")) | |
#inputs.append(gr.Slider(minimum=1, maximum=5, step=1, label=f"Fluency (Translation {i+1})")) | |
inputs.append(gr.Slider(minimum=1, maximum=5, step=1, label=f"Rating (Translation {i+1})")) | |
return inputs | |
# Define the Gradio interface outputs | |
def get_outputs(): | |
return gr.Textbox(label="Evaluation Status") | |
# Create the Gradio interface | |
with gr.Blocks() as demo: | |
# Inputs | |
inputs = get_inputs() | |
# Outputs | |
output = get_outputs() | |
# Add a Submit Button | |
submit_button = gr.Button("Submit Evaluation") | |
# Bind the button to the evaluation function | |
submit_button.click(evaluate_translations, inputs=inputs, outputs=output) | |
# Launch the interface | |
demo.launch() | |