File size: 7,045 Bytes
85a2ba5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import gradio as gr
import pandas as pd
import random
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

CSV_FILE_PATH = "qa_pairs.csv"

cred = credentials.Certificate("unlpboard_f.json")
firebase_admin.initialize_app(cred)

def list_models():
    df = pd.read_csv(CSV_FILE_PATH)
    return df['model'].unique().tolist()


def list_questions():
    df = pd.read_csv(CSV_FILE_PATH)
    return df['question'].unique().tolist()

def fetch_questions():
    questions_ref = db.collection('questions')
    docs = questions_ref.stream()
    questions_list = []
    for doc in docs:
        question = doc.to_dict()
        questions_list.append(question)
    return questions_list


def display_answers(question, model1, model2):
    df = pd.read_csv(CSV_FILE_PATH)
    answers = {
        model1: "No answer available for Model 1",
        model2: "No answer available for Model 2",
    }
    for model in [model1, model2]:
        filtered_df = df[(df['question'] == question) & (df['model'] == model)]
        if not filtered_df.empty:
            answers[model] = f"**{model} Answer:**\n{filtered_df['answer'].iloc[0]}"
    return answers[model1], answers[model2]


def update_b(q,m1,a1,m2,a2):
    print('Model1: ', random_model2)
    print('Model2: ', random_model2)
    q, m1, a1, m2, a2 = update_symbols(q, m1, a1, m2, a2)
    b1 = gr.Button("Vote for Model 1",interactive=True)
    b2 = gr.Button("It’s a tie!",interactive=True)
    b3 = gr.Button("Vote for Model 2",interactive=True)
    b4 = gr.Button("START!", visible = False)
    return q, m1, a1, m2, a2, b1, b2, b3, b4


def update_symbols1(q,m1,a1,m2,a2):
    print("Voted for Model 1")
    log_vote(
        model1=m1,
        model2=m2,
        question=q,
        output1=a1,
        output2=a2,
        outcome=m1
    )
    votes_ref = db.collection('votes')
    vote_doc = votes_ref.document(m1).get()
    if vote_doc.exists:
        votes_ref.document(m1).update({'count': firestore.Increment(1)})
    else:
        votes_ref.document(m1).set({'count': 1})
    update_total_votes()
    return update_symbols(q, m1, a1, m2, a2)


def update_symbols2(q, m1, a1, m2, a2):
    print("Voted for Spare")
    log_vote(
        model1=m1,
        model2=m2,
        question=q,
        output1=a1,
        output2=a2,
        outcome='tie'
    )
    update_total_votes()
    return update_symbols(q, m1, a1, m2, a2)

def update_symbols3(q, m1, a1, m2, a2):
    print("Voted for Model 2")
    log_vote(
        model1=m1,
        model2=m2,
        question=q,
        output1=a1,
        output2=a2,
        outcome=m2
    )
    votes_ref = db.collection('votes')
    vote_doc = votes_ref.document(m2).get()
    if vote_doc.exists:
        votes_ref.document(m2).update({'count': firestore.Increment(1)})
    else:
        votes_ref.document(m2).set({'count': 1})
    update_total_votes()
    return update_symbols(q, m1, a1, m2, a2)

def update_symbols(q,m1,a1,m2,a2):
    random_question = random.choice(questions)
    random_model1, random_model2 = random.sample(models, 2)
    answer1, answer2 = display_answers(random_question, random_model1, random_model2)
    m1 = gr.Markdown(f"{random_model1}", visible=False)
    a1 = gr.Markdown(answer1)
    q = gr.Markdown(f"{random_question}")
    m2 = gr.Markdown(f"{random_model2}", visible=False)
    a2 = gr.Markdown(answer2)
    return q,m1,a1,m2,a2

def update_total_votes():
    votes_ref = db.collection('votes')
    vote_doc = votes_ref.document('total').get()
    if vote_doc.exists:
        votes_ref.document('total').update({'count': firestore.Increment(1)})
    else:
        votes_ref.document('total').set({'count': 1})

def log_vote(model1, model2, question, output1, output2, outcome):
    # Reference to the Firestore collection where votes will be logged
    votes_log_ref = db.collection('votes_log')

    # Create a new document for this vote
    vote_data = {
        'model1': model1,
        'model2': model2,
        'question': question,
        'output1': output1,
        'output2': output2,
        'outcome': outcome,
        'timestamp': firestore.SERVER_TIMESTAMP
    }

    # Add the vote document to Firestore
    votes_log_ref.add(vote_data)


def fetch_and_format_leaderboard():
    vote_counts_ref = db.collection('votes')
    # Ensure you're using FieldPath.document_id() correctly
    docs = vote_counts_ref.stream()

    leaderboard = []
    for doc in docs:
        model_name = doc.id
        vote_count = doc.to_dict().get('count', 0)
        leaderboard.append(f"{model_name}: {vote_count} votes")

    # Optional: Sort the leaderboard by vote count in descending order
    leaderboard.sort(key=lambda x: int(x.split(': ')[1].split(' ')[0]), reverse=True)

    return "\n".join(leaderboard)

#questions = list_questions()
models = list_models()
random_question = 'Click any button to start!'
random_model1, random_model2 = '1', '2'
answer1, answer2 = display_answers(random_question, random_model1, random_model2)


db = firestore.client()

questions = []
questions_ = fetch_questions()
for question in questions_:
    questions.append(question['question_text'])

votes_ref = db.collection('votes')

def create_app():

    print('-----------------------')
    print(random_question)
    print(random_model1)
    print('-----!!!!!!!!!!!!!')

    with gr.Blocks() as app:
        q = gr.Markdown(f"### Question: {random_question}")

        with gr.Row():
            with gr.Column():
                m1 = gr.Markdown(f"{random_model1}", visible=False)
                a1 = gr.Markdown(answer1)

            with gr.Column():
                m2 = gr.Markdown(f"{random_model2}", visible=False)
                a2 = gr.Markdown(answer2)

        with gr.Row():
            b1 = gr.Button("Vote for Model 1",interactive=False)
            b2 = gr.Button("It’s a tie!",interactive=False)
            b3 = gr.Button("Vote for Model 2",interactive=False)
        with gr.Row():
            b4 = gr.Button("START!", interactive=True)
        #with gr.Row():
        #    b5 = gr.Button("Show Leaderboard")

        initial_leaderboard_data = fetch_and_format_leaderboard()
        leaderboard_display = gr.Textbox(value=initial_leaderboard_data,label="Leaderboard", placeholder="Leaderboard will be displayed here.",
                                              lines=30, visible=True)
        #b5.click(fn=fetch_and_format_leaderboard, inputs=[], outputs=leaderboard_display)

        b4.click(update_b, inputs=[q,m1,a1,m2,a2], outputs=[q,m1,a1,m2,a2,b1,b2,b3, b4])
        b1.click(update_symbols1, inputs=[q,m1,a1,m2,a2], outputs=[q,m1,a1,m2,a2])
        b2.click(update_symbols2, inputs=[q, m1, a1, m2, a2], outputs=[q, m1, a1, m2, a2])
        b3.click(update_symbols3, inputs=[q, m1, a1, m2, a2], outputs=[q, m1, a1, m2, a2])
        leaderboard_button = gr.Button("Refresh Leaderboard")
        leaderboard_button.click(fn=fetch_and_format_leaderboard, inputs=[], outputs=leaderboard_display)

    return app

app = create_app()
app.launch()