cetusian commited on
Commit
10dfcb1
·
verified ·
1 Parent(s): 297f353

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -40
app.py CHANGED
@@ -7,9 +7,17 @@ import spaces
7
  api_key = os.getenv("TOKEN")
8
  login(api_key)
9
 
10
- # Initialize clients for different models
11
- llama_client = InferenceClient("meta-llama/Llama-3.1-70B-Instruct")
12
- gpt_client = InferenceClient("openai/gpt-4")
 
 
 
 
 
 
 
 
13
 
14
  # Define the response function
15
  @spaces.GPU
@@ -27,23 +35,16 @@ def respond(
27
 
28
  responses = {}
29
 
30
- if "Llama" in selected_models:
31
- llama_response = ""
32
- for token in llama_client.chat_completion(
33
- messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
34
- ):
35
- delta = token.choices[0].delta.content
36
- llama_response += delta
37
- responses["Llama"] = llama_response
38
-
39
- if "GPT" in selected_models:
40
- gpt_response = ""
41
- for token in gpt_client.chat_completion(
42
  messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
43
  ):
44
  delta = token.choices[0].delta.content
45
- gpt_response += delta
46
- responses["GPT"] = gpt_response
47
 
48
  return responses
49
 
@@ -51,30 +52,94 @@ def respond(
51
  def create_demo():
52
  with gr.Blocks() as demo:
53
  gr.Markdown("# AI Model Comparison Tool 🌟")
54
- gr.ChatInterface(
55
- respond,
56
- type="messages",
57
- additional_inputs=[
58
- gr.Textbox(
59
- value="You are a helpful assistant providing answers for technical and customer support queries.",
60
- label="System message"
61
- ),
62
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
63
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
64
- gr.Slider(
65
- minimum=0.1,
66
- maximum=1.0,
67
- value=0.95,
68
- step=0.05,
69
- label="Top-p (nucleus sampling)"
70
- ),
71
- gr.CheckboxGroup(
72
- ["Llama", "GPT"],
73
- label="Select models to compare",
74
- value=["Llama"]
75
- ),
76
- ],
77
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  return demo
79
 
80
  if __name__ == "__main__":
 
7
  api_key = os.getenv("TOKEN")
8
  login(api_key)
9
 
10
+ # Predefined list of models to compare (can be expanded)
11
+ model_options = {
12
+ "Llama-3.1-70B": "meta-llama/Llama-3.1-70B-Instruct",
13
+ "GPT-4": "TheBloke/Open_Gpt4_8x7B-GGUF",
14
+ "Falcon-40B": "tiiuae/falcon-40b-instruct",
15
+ "Mistral-7B": "mistralai/Mistral-7B-Instruct-v0.3",
16
+ "Bloom": "bigscience/bloom",
17
+ }
18
+
19
+ # Initialize clients for models
20
+ clients = {name: InferenceClient(repo_id) for name, repo_id in model_options.items()}
21
 
22
  # Define the response function
23
  @spaces.GPU
 
35
 
36
  responses = {}
37
 
38
+ # Generate responses for each selected model
39
+ for model_name in selected_models:
40
+ client = clients[model_name]
41
+ response = ""
42
+ for token in client.chat_completion(
 
 
 
 
 
 
 
43
  messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
44
  ):
45
  delta = token.choices[0].delta.content
46
+ response += delta
47
+ responses[model_name] = response
48
 
49
  return responses
50
 
 
52
  def create_demo():
53
  with gr.Blocks() as demo:
54
  gr.Markdown("# AI Model Comparison Tool 🌟")
55
+ gr.Markdown(
56
+ """
57
+ Compare responses from multiple AI models side-by-side.
58
+ Select models, ask a question, and vote for the best response!
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.",
65
+ label="System message"
66
+ )
67
+ user_message = gr.Textbox(label="Your question", placeholder="Type your question here...")
68
+
69
+ with gr.Row():
70
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
71
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
72
+ top_p = gr.Slider(
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()),
79
+ label="Select models to compare",
80
+ value=["Llama-3.1-70B", "GPT-4"], # 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(
110
+ message, history, system_message, max_tokens, temperature, top_p, selected_models
111
+ ):
112
+ responses = respond(
113
+ message, history, system_message, max_tokens, temperature, top_p, selected_models
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, [], system_message, max_tokens, temperature, top_p, selected_models],
126
+ outputs=[response[2] for response in response_boxes],
127
+ )
128
+
129
+ for model_name, _, _, vote_button, vote_count in response_boxes:
130
+ vote_button.click(
131
+ lambda votes, name=model_name: handle_votes(votes, name),
132
+ inputs=[vote_counts],
133
+ outputs=[vote_counts],
134
+ )
135
+
136
+ # Update model visibility when the model selection changes
137
+ selected_models.change(
138
+ update_model_visibility,
139
+ inputs=[selected_models],
140
+ outputs=[response[1] for response in response_boxes],
141
+ )
142
+
143
  return demo
144
 
145
  if __name__ == "__main__":