File size: 3,411 Bytes
cfba7ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()