File size: 3,465 Bytes
085ecee
 
 
 
b2476bd
085ecee
 
 
 
 
 
 
 
 
35deb04
b2476bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
085ecee
 
 
 
 
b2476bd
 
 
 
 
 
085ecee
b2476bd
 
35deb04
 
b2476bd
085ecee
b2476bd
 
 
085ecee
b2476bd
 
 
 
 
085ecee
b2476bd
 
 
 
 
 
 
 
 
 
085ecee
 
 
 
 
b2476bd
 
085ecee
 
b2476bd
 
 
 
085ecee
 
b2476bd
 
 
 
 
 
 
 
 
 
 
 
 
9c881a5
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
96
97
98
99
100
101
102
import gradio as gr
import pandas as pd
import os
from evaluation import evaluate_model  # Import your evaluation function
import zipfile

# Define the path where you want to save the leaderboard data
leaderboard_file = "leaderboard.csv"

# Check if leaderboard file exists, otherwise create an empty DataFrame
if os.path.exists(leaderboard_file):
    leaderboard = pd.read_csv(leaderboard_file)
else:
    leaderboard = pd.DataFrame(columns=["Model Name", "Score"])
    print('file ok')

def extract_model(model_file, extract_dir="models"):
    """
    Extracts the uploaded model file if it's a zip archive.
    """
    os.makedirs(extract_dir, exist_ok=True)  # Ensure the directory exists
    model_path = os.path.join(extract_dir, model_file.name)

    if model_file.name.endswith(".zip"):
        with zipfile.ZipFile(model_file, 'r') as zip_ref:
            zip_ref.extractall(extract_dir)
            print(f"Extracted model to: {extract_dir}")
        return extract_dir
    else:
        # Save the file directly if it's not a zip
        model_file.save(model_path)
        return model_path


# Submit the evaluation and update the leaderboard
def submit_evaluation(model_name, model_file):
    """
    Handles the model submission, evaluates it, and updates the leaderboard.
    """
    try:
        # Extract or save the uploaded model
        model_path = extract_model(model_file)

        print(f"Model saved or extracted to: {model_path}")
        print("Starting evaluation...")

        # Example test data (replace with your actual test dataset)
        test_data = [
            ("negative", 0),  # (text, label)
            ("positive", 1),
        ]

        # Evaluate the model using your custom evaluation code
        score = evaluate_model(model_path, test_data)
        print(f"Model evaluated successfully. Score: {score}")

        # Update the leaderboard
        new_entry = {"Model Name": model_name, "Score": score}
        global leaderboard
        leaderboard = leaderboard.append(new_entry, ignore_index=True)
        leaderboard_sorted = leaderboard.sort_values(by="Score", ascending=False)

        # Save the updated leaderboard
        leaderboard_sorted.to_csv(leaderboard_file, index=False)
        print("Leaderboard updated.")

        # Return the sorted leaderboard
        return leaderboard_sorted, "Model submitted successfully!"

    except Exception as e:
        print(f"Error during evaluation: {str(e)}")
        return leaderboard, f"Error: {str(e)}"


# Create the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Model Evaluation Leaderboard")

    # User inputs for model name and file upload
    with gr.Row():
        model_name_input = gr.Textbox(label="Model Name", placeholder="Enter the model name")
        model_file_input = gr.File(
            label="Upload Model (Supported Formats: .pt, .bin, .h5, .zip)",
            file_types=[".pt", ".bin", ".h5", ".zip"]
        )

    submit_button = gr.Button("Submit Evaluation")

    # Leaderboard display and status message
    leaderboard_display = gr.Dataframe(leaderboard, label="Leaderboard")
    status_message = gr.Textbox(label="Status", interactive=False)

    # Link the submit button to the evaluation function
    submit_button.click(
        submit_evaluation,
        inputs=[model_name_input, model_file_input],
        outputs=[leaderboard_display, status_message]
    )

# Launch the Gradio app
demo.launch(share=True)