File size: 5,415 Bytes
a33ace4
63d7feb
82fc346
a52b9c1
63d7feb
a33ace4
f578a9f
 
 
a33ace4
f578a9f
 
a33ace4
f578a9f
 
 
 
 
 
 
 
 
 
 
 
 
a788ea0
7786c4d
a52b9c1
3b02b87
f578a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a33ace4
a52b9c1
f578a9f
 
a33ace4
a52b9c1
 
 
 
 
f578a9f
 
a52b9c1
 
 
f578a9f
 
 
a52b9c1
a788ea0
f578a9f
a52b9c1
 
f578a9f
a52b9c1
 
a33ace4
a788ea0
a52b9c1
 
 
 
f578a9f
a33ace4
a52b9c1
a33ace4
 
 
a788ea0
a33ace4
 
 
 
 
 
 
 
 
 
 
a52b9c1
f578a9f
a33ace4
f578a9f
a33ace4
 
f578a9f
a33ace4
f578a9f
 
a33ace4
f578a9f
a33ace4
a52b9c1
a33ace4
 
7786c4d
c146544
7786c4d
c146544
a52b9c1
 
 
 
f578a9f
 
a33ace4
a52b9c1
7786c4d
f578a9f
 
 
 
 
a52b9c1
a33ace4
82fc346
 
 
 
 
 
a33ace4
a52b9c1
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import gradio as gr
import pandas as pd
# from gradio_huggingfacehub_search import HuggingfaceHubSearch
import requests

def update_table(category):
    # TODO: url -> env
    url = "https://leaderboard.nexa4ai.com/model/get-ranking-by-category"   
    url_with_params = f"{url}?category={category}"
    
    print("Getting model table with URL:", url_with_params) 
    response = requests.get(url_with_params)

    if response.status_code == 200:
        ranking_data = response.json()["ranking"]
        # Process the API data into a DataFrame
        api_model_data = {
            "Model": [item["model_id"] for item in ranking_data],
            "Votes": [item["votes"] for item in ranking_data],
            "Categories": [category] * len(ranking_data)
        }
        api_df = pd.DataFrame(api_model_data)
    else:
        print(f"Failed to submit request: {response.text}")
        api_df = pd.DataFrame()  
    return api_df

def get_user(profile: gr.OAuthProfile | None) -> str:
    if profile is None:
        return ""
    return profile.username

def get_vote_models(category):
    print (category)
    if not category:
        return []
    
    url = "https://leaderboard.nexa4ai.com/model/get-models-by-category" 
    url_with_params = f"{url}?category={category}"

    print("Getting models with URL:", url_with_params) 
    response = requests.get(url_with_params)

    if response.status_code == 200:
        models_data = response.json()
        models.choices = models_data  # Set choices of models
        print(f"models_data: {models_data}")
        print(f"models.choices: {models.choices}")
        return models_data
    else:
        print(f"Failed to get models: {response.text}")
        return []



def submit_vote(username, category, model_ids):
    if not category or not model_ids:
        return "All fields are required!"
    if not username:
        return "You need to log in to submit a vote."
    if username.startswith("Hello "):
        username = username[6:]

    url = "https://leaderboard.nexa4ai.com/model/vote"    
    params = {
        "category": category,
        "username": username
    }
    data = {
        "model_ids": model_ids
    }
    
    print("Submitting vote with payload:", data) 
    response = requests.post(url, params=params, json=data)
    
    if response.status_code == 200:
        return f"Vote '{model_ids}' submitted successfully!"
    else:
        return f"Failed to vote: {response.text}"


def submit_model(category, model_id):
    if not category or not model_id:
        return "All fields are required!"
    
    url = "https://leaderboard.nexa4ai.com/"   # Will have a new endpoint
    data = {
        "category": category,
        "model_id": model_id
    }
    
    print("Submitting model with payload:", data) 
    response = requests.post(url, json=data)
    
    if response.status_code == 200:
        return "Your request has been submitted successfully. We will notify you by email once processing is complete."
    else:
        return f"Failed to submit request: {response.text}"


with gr.Blocks() as app:
    with gr.Tabs():
        with gr.TabItem("Table"):
            category = gr.Dropdown(
                choices=["all", "Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"],
                label="Select Category",
                value="all"
            )
            
            initial_data = update_table("all")
            table = gr.Dataframe(
                headers=["Model", "Votes", "Categories"],
                datatype=["str", "number", "str"],
                value=initial_data,
                col_count=(3, "fixed"),
            )
            category.change(update_table, inputs=category, outputs=table)
        
        with gr.TabItem("Vote"):
            username_text = gr.State(value="")
            login_button = gr.LoginButton()
            app.load(get_user, inputs=None, outputs=username_text)

            category = gr.Dropdown(
                choices=["Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"], 
                label="Select Category"
            )
    
            models = gr.CheckboxGroup(choices=[], label="Choose your options")
            submit_button = gr.Button("Submit Vote")
            submit_result = gr.Label()

            category.change(get_vote_models, inputs=[category], outputs=[models])  # Fix: Use models.choices to update choices

            submit_button.click(fn=submit_vote, inputs=[username_text, category, models], outputs=submit_result)



        
        # with gr.TabItem("Submit Model"):
        #     category = gr.Dropdown(choices=["Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"], label="Select Category")
        #     model_id = HuggingfaceHubSearch(label="Hub Model ID", placeholder="Search for model id on Huggingface", search_type="model")
        #     submit_model_button = gr.Button("Submit Model")
        #     submit_model_result = gr.Label()
        #     submit_model_button.click(fn=submit_model, inputs=[category, model_id], outputs=submit_model_result)

app.launch()