File size: 4,354 Bytes
26adeef
7181cca
3a5dac1
4e22254
 
 
 
 
a60a824
 
 
 
3a5dac1
 
 
 
 
28047c0
3a5dac1
 
 
 
 
e15b560
3a5dac1
 
 
 
 
 
 
 
 
a482d37
c006a72
b8adfc6
4e22254
 
 
 
 
3a5dac1
3b53056
4e22254
 
 
 
 
 
 
3b53056
4e22254
 
3a5dac1
 
7181cca
4e22254
3b53056
4e22254
 
3a5dac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e22254
3a5dac1
4e22254
3a5dac1
 
 
 
 
 
 
 
 
 
 
 
 
4e22254
7181cca
3a5dac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e22254
3a5dac1
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
import os
from collections import defaultdict
from database import save_response, read_responses
import gradio as gr
import pandas as pd
import random

css = """
    .rtl
    {
        text-align: right;
    }
    .usr-inst{
    text-align:center;
    border: solid 1px;
    border-radius: 5px;
    padding: 10px;
    }
    .svelte-1kzox3m{
    justify-content: end;
    }
    .svelte-sfqy0y{
    border:none; 
    }
    .svelte-90oupt{
    padding-top: 0px;
    }
    #component-4{
    border: 1px solid;
    padding: 5px;
    border-radius: 5px;
    }
    
    """

file_path = 'instructions/merged.json'
df = pd.read_json(file_path, orient='records', lines=False)

# that keeps track of how many times each question has been used
question_count = {index: 0 for index in df.index}
model_rankings = defaultdict(lambda: {'1st': 0, '2nd': 0, '3rd': 0})
curr_order = ['CIDAR', 'CHAT', 'ALPAGASUS']

def get_rank_suffix(rank):
    if 11 <= rank <= 13:
        return 'th'
    else:
        suffixes = {1: 'st', 2: 'nd', 3: 'rd'}
        return suffixes.get(rank % 10, 'th')


def process_rankings(user_rankings):
    print("Processing Rankings:", user_rankings)  # Debugging print
    save_response(user_rankings)
    print(read_responses())
    return


def get_questions_and_answers():
    available_questions = [index for index, count in question_count.items() if count < 3]
    index  = random.sample(available_questions, min(1, len(available_questions)))[0]
    question_count[index] += 1

    question = df.loc[index, 'instruction']
    answers_with_models = [
        (df.loc[index, 'cidar_output'], 'CIDAR'),
        (df.loc[index, 'chat_output'], 'CHAT'),
        (df.loc[index, 'alpagasus_output'], 'ALPAGASUS')
    ]
    random.shuffle(answers_with_models)  # Shuffle answers with their IDs
    curr_order = [model for _, model in answers_with_models]
    return (question, answers_with_models)

def reload_components():
    question, answers = get_questions_and_answers()
    user_instructions_txt = " في الصفحة التالية ستجد طلب له ثلاث إجابات مختلفة. من فضلك اختر مدي توافق كل إجابة مع الثقافة العربية."
    radios = []
    user_instructions = gr.Markdown(rtl=True, value= f'<h1 class="usr-inst">{user_instructions_txt}</h1>')

    question_md = gr.Markdown(rtl=True, value= f'<b> {question} </b>')
                
    for answer, model in answers:
        radios.append(gr.Markdown(rtl = True, value= answer))
        radios.append(gr.Radio(elem_classes = 'rtl', choices = ['متوافق', 'متوافق جزئياً', 'غير متوافق'], value = 'غير متوافق', label = ""))            
    
    return [user_instructions, question_md] + radios

def rank_interface():
    def rank_fluency(*radio_selections):
        user_rankings = {}
        for i in range(0, len(radio_selections), 3):  # Process each set of 3 dropdowns for a question
            selections = radio_selections[i:i+3]
            for j, chosen_answer in enumerate(selections):
                model_name = curr_order[j]
                if chosen_answer == 'غير متوافق':
                    user_rankings[model_name] =  3 
                elif chosen_answer == 'متوافق جزئياً':
                    user_rankings[model_name] =  2
                elif chosen_answer == 'متوافق':
                    user_rankings[model_name] =  1
        process_rankings(user_rankings)
        return "سجلنا ردك، ما قصرت =)"
        
    # Create three dropdowns for each question for 1st, 2nd, and 3rd choices
    inputs = []
    with gr.Blocks(css=css) as demo:
        with gr.Row():
            with gr.Column():
                outptus= reload_components()
                out_text = gr.Markdown("", rtl = True)

                gr.Button("Submit").click(
                    fn=rank_fluency,
                    inputs=outptus[1:],
                    outputs=out_text
                ).then(
                    fn=reload_components,
                    outputs = outptus
                )

                gr.Button("Skip").click(
                    fn=reload_components,
                    outputs=outptus
                )
           
    return demo

questions = get_questions_and_answers()
iface = rank_interface()
iface.launch(share = True)