cetusian commited on
Commit
ae21d92
·
verified ·
1 Parent(s): 7660535

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -30
app.py CHANGED
@@ -48,6 +48,7 @@ def respond(
48
 
49
  return responses
50
 
 
51
  # Build Gradio app
52
  def create_demo():
53
  with gr.Blocks() as demo:
@@ -59,6 +60,7 @@ def create_demo():
59
  """
60
  )
61
 
 
62
  with gr.Row():
63
  system_message = gr.Textbox(
64
  value="You are a helpful assistant providing answers for technical and customer support queries.",
@@ -73,6 +75,7 @@ def create_demo():
73
  minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
74
  )
75
 
 
76
  with gr.Row():
77
  selected_models = gr.CheckboxGroup(
78
  choices=list(model_options.keys()),
@@ -80,30 +83,24 @@ def create_demo():
80
  value=["Llama-3.1-70B", "Qwen-2.5-1.5B-Instruct"], # Default models
81
  )
82
 
83
- submit_button = gr.Button("Generate Responses")
 
 
 
84
 
85
- with gr.Row():
86
- response_boxes = []
87
- vote_buttons = []
88
- vote_counts = []
89
-
90
- # Dynamically create response sections for each model
91
- for model_name in model_options.keys():
92
- with gr.Column(visible=False) as column: # Initially hide unused models
93
- response_box = gr.Textbox(label=f"Response from {model_name}")
94
  vote_button = gr.Button(f"Vote for {model_name}")
95
  vote_count = gr.Number(value=0, label=f"Votes for {model_name}")
96
- response_boxes.append((model_name, column, response_box, vote_button, vote_count))
 
97
 
98
- # Define visibility and update functions dynamically
99
- def update_model_visibility(models):
100
- for model_name, column, *_ in response_boxes:
101
- column.visible = model_name in models
102
-
103
- def handle_votes(vote_counts, model_name):
104
- index = list(model_options.keys()).index(model_name)
105
- vote_counts[index] += 1
106
- return vote_counts
107
 
108
  # Generate responses
109
  def generate_responses(
@@ -114,37 +111,40 @@ def create_demo():
114
  )
115
  outputs = []
116
  for model_name, _, response_box, *_ in response_boxes:
117
- if model_name in responses:
118
- outputs.append(responses[model_name])
119
- else:
120
- outputs.append("")
121
  return outputs
122
 
 
 
 
 
 
 
 
123
  submit_button.click(
124
  generate_responses,
125
  inputs=[user_message, gr.State([]), system_message, max_tokens, temperature, top_p, selected_models],
126
  outputs=[response[2] for response in response_boxes],
127
  )
128
 
129
- vote_state = gr.State([0] * len(model_options)) # Initialize one vote count per model
130
-
131
  for model_name, _, _, vote_button, vote_count in response_boxes:
132
  vote_button.click(
133
  lambda votes, name=model_name: handle_votes(votes, name),
134
  inputs=[vote_state],
135
- outputs=[vote_state, vote_count], # Update state and display vote count
136
  )
137
 
138
-
139
- # Update model visibility when the model selection changes
140
  selected_models.change(
141
- update_model_visibility,
142
  inputs=[selected_models],
143
  outputs=[response[1] for response in response_boxes],
144
  )
145
 
146
  return demo
147
 
 
148
  if __name__ == "__main__":
149
  demo = create_demo()
150
  demo.launch()
 
48
 
49
  return responses
50
 
51
+
52
  # Build Gradio app
53
  def create_demo():
54
  with gr.Blocks() as demo:
 
60
  """
61
  )
62
 
63
+ # Input Section
64
  with gr.Row():
65
  system_message = gr.Textbox(
66
  value="You are a helpful assistant providing answers for technical and customer support queries.",
 
75
  minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
76
  )
77
 
78
+ # Model Selection Section
79
  with gr.Row():
80
  selected_models = gr.CheckboxGroup(
81
  choices=list(model_options.keys()),
 
83
  value=["Llama-3.1-70B", "Qwen-2.5-1.5B-Instruct"], # Default models
84
  )
85
 
86
+ # Dynamic Response Section
87
+ response_boxes = []
88
+ vote_buttons = []
89
+ vote_counts = []
90
 
91
+ def generate_response_boxes(models):
92
+ response_boxes.clear()
93
+ for model_name in models:
94
+ with gr.Column() as response_column:
95
+ response_box = gr.Textbox(label=f"Response from {model_name}", interactive=False)
 
 
 
 
96
  vote_button = gr.Button(f"Vote for {model_name}")
97
  vote_count = gr.Number(value=0, label=f"Votes for {model_name}")
98
+ response_boxes.append((model_name, response_column, response_box, vote_button, vote_count))
99
+ return response_boxes
100
 
101
+ # Add a button for generating responses
102
+ submit_button = gr.Button("Generate Responses")
103
+ vote_state = gr.State([0] * len(model_options)) # Initialize votes for all models
 
 
 
 
 
 
104
 
105
  # Generate responses
106
  def generate_responses(
 
111
  )
112
  outputs = []
113
  for model_name, _, response_box, *_ in response_boxes:
114
+ outputs.append(responses.get(model_name, ""))
 
 
 
115
  return outputs
116
 
117
+ # Handle votes
118
+ def handle_votes(votes, model_name):
119
+ index = list(model_options.keys()).index(model_name)
120
+ votes[index] += 1
121
+ return votes
122
+
123
+ # Link button click to generate responses
124
  submit_button.click(
125
  generate_responses,
126
  inputs=[user_message, gr.State([]), system_message, max_tokens, temperature, top_p, selected_models],
127
  outputs=[response[2] for response in response_boxes],
128
  )
129
 
130
+ # Link voting buttons to handle votes
 
131
  for model_name, _, _, vote_button, vote_count in response_boxes:
132
  vote_button.click(
133
  lambda votes, name=model_name: handle_votes(votes, name),
134
  inputs=[vote_state],
135
+ outputs=[vote_state, vote_count],
136
  )
137
 
138
+ # Update response boxes when models are selected
 
139
  selected_models.change(
140
+ generate_response_boxes,
141
  inputs=[selected_models],
142
  outputs=[response[1] for response in response_boxes],
143
  )
144
 
145
  return demo
146
 
147
+
148
  if __name__ == "__main__":
149
  demo = create_demo()
150
  demo.launch()