Zaid commited on
Commit
3a5dac1
1 Parent(s): d59fd96

Update app.py

Browse files

update new interface

Files changed (1) hide show
  1. app.py +91 -81
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
- os.system("pip install pymongo")
3
  from collections import defaultdict
4
- from database import save_response
5
  import gradio as gr
6
  import pandas as pd
7
  import random
@@ -11,17 +10,30 @@ css = """
11
  {
12
  text-align: right;
13
  }
14
- #component-19
15
- {
16
- text-align: right;
17
- flex-direction: row-reverse; /* Makes the main-axis start from the right /
18
- justify-content: flex-start; / Aligns children to the right */
 
19
  }
20
- #component-17
21
- {
22
- text-align: right;
23
- float: right;
 
24
  }
 
 
 
 
 
 
 
 
 
 
 
25
  """
26
 
27
  file_path = 'instructions/merged.json'
@@ -30,7 +42,7 @@ df = pd.read_json(file_path, orient='records', lines=False)
30
  # that keeps track of how many times each question has been used
31
  question_count = {index: 0 for index in df.index}
32
  model_rankings = defaultdict(lambda: {'1st': 0, '2nd': 0, '3rd': 0})
33
-
34
 
35
  def get_rank_suffix(rank):
36
  if 11 <= rank <= 13:
@@ -42,82 +54,80 @@ def get_rank_suffix(rank):
42
 
43
  def process_rankings(user_rankings):
44
  print("Processing Rankings:", user_rankings) # Debugging print
45
- for answer_id, rank in user_rankings:
46
- model = answer_id.split('_')[0] # Extracting the model name from the answer_id
47
- rank_suffix = get_rank_suffix(rank)
48
- model_rankings[model][f'{rank}{rank_suffix}'] += 1 # Using the correct suffix based on the rank
49
- model_rankings_dict = dict(model_rankings)
50
-
51
- save_response(model_rankings_dict)
52
- print("Updated Model Rankings:", model_rankings) # Debugging print
53
  return
54
 
55
 
56
  def get_questions_and_answers():
57
  available_questions = [index for index, count in question_count.items() if count < 3]
58
- selected_indexes = random.sample(available_questions, min(4, len(available_questions)))
59
- for index in selected_indexes:
60
- question_count[index] += 1
61
-
62
- questions_and_answers = []
63
- for index in selected_indexes:
64
- question = df.loc[index, 'instruction']
65
- answers_with_models = [
66
- (df.loc[index, 'cidar_output'], 'CIDAR'),
67
- (df.loc[index, 'chat_output'], 'CHAT'),
68
- (df.loc[index, 'alpagasus_output'], 'ALPAGASUS')
69
- ]
70
- random.shuffle(answers_with_models) # Shuffle answers with their IDs
71
- questions_and_answers.append((question, answers_with_models))
72
-
73
- return questions_and_answers
74
-
75
-
76
- def rank_interface():
77
- questions = get_questions_and_answers()
 
 
 
 
78
 
79
- # Create three dropdowns for each question for 1st, 2nd, and 3rd choices
80
- inputs = []
81
-
82
- for question, answers in questions:
83
- # Use an HTML component to display the question
84
- inputs.append(gr.Markdown(rtl=True, value= question))
85
-
86
- answers_text = [answer for answer, _ in answers]
87
-
88
- # Append three dropdowns for rankings without repeating the question
89
- inputs.append(gr.Dropdown(elem_classes="rtl", choices=["...اختر"] + answers_text, label="الاختيار الأول"))
90
- inputs.append(gr.Dropdown(elem_classes="rtl", choices=["...اختر"] + answers_text, label="الاختيار الثاني"))
91
- inputs.append(gr.Dropdown(elem_classes="rtl", choices=["...اختر"] + answers_text, label="الاختيار الثالث"))
92
-
93
- outputs = gr.Textbox(elem_id="rtl_text", label="")
94
-
95
-
96
- def rank_fluency(*dropdown_selections):
97
- user_rankings = []
98
- for i in range(0, len(dropdown_selections), 4): # Process each set of 3 dropdowns for a question
99
- selections = dropdown_selections[i+1:i+4]
100
- # Check for duplicate selections within the same question
101
- unique_selections = set(tuple(selection) for selection in selections)
102
 
103
- # Now you can safely check if all sublists were unique
104
- if len(selections) != len(unique_selections):
105
- return "تأكد من عدم تكرار الإجابة لنفس السؤال"
106
-
107
- question_index = i // 4
108
- _, model_answers = questions[question_index]
109
- for j, chosen_answer in enumerate(selections, start=1):
110
- if chosen_answer == "...اختر": # Skip unselected dropdowns
111
- continue
112
- for model_answer, model in model_answers:
113
- if model_answer == chosen_answer:
114
- user_rankings.append((model, j)) # j is the rank (1, 2, or 3)
115
- break
116
  process_rankings(user_rankings)
117
  return "سجلنا ردك، ما قصرت =)"
118
-
119
- return gr.Interface(fn=rank_fluency, inputs=inputs, outputs=outputs,
120
- title="رتب إجابات الأسئلة أدناه، بناء على ارتباطها بالهوية الثقافية للمجتمعات العربية", css=css)
121
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  iface = rank_interface()
123
- iface.launch()
 
1
  import os
 
2
  from collections import defaultdict
3
+ from database import save_response, read_responses
4
  import gradio as gr
5
  import pandas as pd
6
  import random
 
10
  {
11
  text-align: right;
12
  }
13
+ .usr-inst{
14
+ text-align:center;
15
+ background-color: #3e517e;
16
+ border: solid 1px;
17
+ border-radius: 5px;
18
+ padding: 10px;
19
  }
20
+ .svelte-1kzox3m{
21
+ justify-content: end;
22
+ }
23
+ .svelte-sfqy0y{
24
+ border:none;
25
  }
26
+ .svelte-90oupt{
27
+ background-color: #0b0f19;
28
+ padding-top: 0px;
29
+ }
30
+ #component-4{
31
+ border: 1px solid;
32
+ padding: 5px;
33
+ background-color: #242433;
34
+ border-radius: 5px;
35
+ }
36
+
37
  """
38
 
39
  file_path = 'instructions/merged.json'
 
42
  # that keeps track of how many times each question has been used
43
  question_count = {index: 0 for index in df.index}
44
  model_rankings = defaultdict(lambda: {'1st': 0, '2nd': 0, '3rd': 0})
45
+ curr_order = ['CIDAR', 'CHAT', 'ALPAGASUS']
46
 
47
  def get_rank_suffix(rank):
48
  if 11 <= rank <= 13:
 
54
 
55
  def process_rankings(user_rankings):
56
  print("Processing Rankings:", user_rankings) # Debugging print
57
+ save_response(user_rankings)
58
+ print(read_responses())
 
 
 
 
 
 
59
  return
60
 
61
 
62
  def get_questions_and_answers():
63
  available_questions = [index for index, count in question_count.items() if count < 3]
64
+ index = random.sample(available_questions, min(1, len(available_questions)))[0]
65
+ question_count[index] += 1
66
+
67
+ question = df.loc[index, 'instruction']
68
+ answers_with_models = [
69
+ (df.loc[index, 'cidar_output'], 'CIDAR'),
70
+ (df.loc[index, 'chat_output'], 'CHAT'),
71
+ (df.loc[index, 'alpagasus_output'], 'ALPAGASUS')
72
+ ]
73
+ random.shuffle(answers_with_models) # Shuffle answers with their IDs
74
+ curr_order = [model for _, model in answers_with_models]
75
+ return (question, answers_with_models)
76
+
77
+ def reload_components():
78
+ question, answers = get_questions_and_answers()
79
+ user_instructions_txt = " في الصفحة التالية ستجد طلب له ثلاث إجابات مختلفة. من فضلك اختر مدي توافق كل إجابة مع الثقافة العربية."
80
+ radios = []
81
+ user_instructions = gr.Markdown(rtl=True, value= f'<h1 class="usr-inst">{user_instructions_txt}</h1>')
82
+
83
+ question_md = gr.Markdown(rtl=True, value= f'<b> {question} </b>')
84
+
85
+ for answer, model in answers:
86
+ radios.append(gr.Markdown(rtl = True, value= answer))
87
+ radios.append(gr.Radio(elem_classes = 'rtl', choices = ['متوافق', 'متوافق جزئياً', 'غير متوافق'], value = 'غير متوافق', label = ""))
88
 
89
+ return [user_instructions, question_md] + radios
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ def rank_interface():
92
+ def rank_fluency(*radio_selections):
93
+ user_rankings = {}
94
+ for i in range(0, len(radio_selections), 3): # Process each set of 3 dropdowns for a question
95
+ selections = radio_selections[i:i+3]
96
+ for j, chosen_answer in enumerate(selections):
97
+ model_name = curr_order[j]
98
+ if chosen_answer == 'غير متوافق':
99
+ user_rankings[model_name] = 3
100
+ elif chosen_answer == 'متوافق جزئياً':
101
+ user_rankings[model_name] = 2
102
+ elif chosen_answer == 'متوافق':
103
+ user_rankings[model_name] = 1
104
  process_rankings(user_rankings)
105
  return "سجلنا ردك، ما قصرت =)"
106
+
107
+ # Create three dropdowns for each question for 1st, 2nd, and 3rd choices
108
+ inputs = []
109
+ with gr.Blocks(css=css) as demo:
110
+ with gr.Row():
111
+ with gr.Column():
112
+ outptus= reload_components()
113
+ out_text = gr.Markdown("", rtl = True)
114
+
115
+ gr.Button("Submit").click(
116
+ fn=rank_fluency,
117
+ inputs=outptus[1:],
118
+ outputs=out_text
119
+ ).then(
120
+ fn=reload_components,
121
+ outputs = outptus
122
+ )
123
+
124
+ gr.Button("Skip").click(
125
+ fn=reload_components,
126
+ outputs=outptus
127
+ )
128
+
129
+ return demo
130
+
131
+ questions = get_questions_and_answers()
132
  iface = rank_interface()
133
+ iface.launch(share = True)