hanzla javaid commited on
Commit
ff120ef
β€’
1 Parent(s): 116a0d1
Files changed (1) hide show
  1. app.py +124 -26
app.py CHANGED
@@ -7,55 +7,153 @@ import spaces
7
  loaded_models = {}
8
 
9
  # List of available models (update with your preferred models)
10
- models = ["hanzla/gemma-2b-datascience-instruct-v5", "hanzla/gemma-2b-datascience-instruct-v4.5"]
 
 
 
 
11
 
12
  @spaces.GPU
13
- def load_model(model_name):
14
- if model_name not in loaded_models:
15
- print(f"Loading model: {model_name}")
16
- tokenizer = AutoTokenizer.from_pretrained(model_name)
17
- model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda" if torch.cuda.is_available() else "cpu")
18
- loaded_models[model_name] = (model, tokenizer)
19
- return loaded_models[model_name]
 
 
 
 
 
20
 
21
  @spaces.GPU
22
  def get_model_response(model_name, message):
23
- model, tokenizer = load_model(model_name)
 
 
 
24
  inputs = tokenizer(message, return_tensors="pt").to(model.device)
25
 
26
- outputs = model.generate(**inputs)
 
 
 
 
 
 
 
27
 
28
- response = tokenizer.decode(outputs[0])
29
  return response
30
 
31
 
32
- def chat(message, history, model1, model2):
 
 
 
 
33
  response1 = get_model_response(model1, message)
34
  response2 = get_model_response(model2, message)
35
- history = history or []
36
- history.append({"role": "user", "content": message})
37
- history.append({"role": "assistant", "content": f"{model1}: {response1}\n\n{model2}: {response2}"})
38
- return history
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  with gr.Blocks() as demo:
43
- gr.Markdown("# Hugging Face Model Comparison Chat")
44
 
 
45
  with gr.Row():
46
  model1_dropdown = gr.Dropdown(choices=models, label="Model 1", value=models[0])
47
  model2_dropdown = gr.Dropdown(choices=models, label="Model 2", value=models[1])
48
 
49
- chatbot = gr.Chatbot(label="Chat History", type="messages")
50
- msg = gr.Textbox(label="Your message")
51
- clear = gr.Button("Clear")
52
-
53
  with gr.Row():
54
- upvote = gr.Button("πŸ‘ Upvote")
55
- downvote = gr.Button("πŸ‘Ž Downvote")
 
 
 
 
56
 
57
- msg.submit(chat, [msg, chatbot, model1_dropdown, model2_dropdown], chatbot)
58
- clear.click(lambda: [], None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
- demo.launch()
 
7
  loaded_models = {}
8
 
9
  # List of available models (update with your preferred models)
10
+ models = [
11
+ "hanzla/gemma-2b-datascience-instruct-v5",
12
+ "hanzla/gemma-2b-datascience-instruct-v4.5"
13
+ ]
14
+
15
 
16
  @spaces.GPU
17
+ def load_all_models():
18
+ """
19
+ Pre-loads all models and their tokenizers into memory.
20
+ """
21
+ for model_name in models:
22
+ if model_name not in loaded_models:
23
+ print(f"Loading model: {model_name}")
24
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
25
+ model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda" if torch.cuda.is_available() else "cpu")
26
+ loaded_models[model_name] = (model, tokenizer)
27
+ return "All models loaded successfully."
28
+
29
 
30
  @spaces.GPU
31
  def get_model_response(model_name, message):
32
+ """
33
+ Generates a response from the specified model given a user message.
34
+ """
35
+ model, tokenizer = loaded_models[model_name]
36
  inputs = tokenizer(message, return_tensors="pt").to(model.device)
37
 
38
+ # Generate response with appropriate parameters
39
+ outputs = model.generate(
40
+ **inputs,
41
+ max_length=512,
42
+ do_sample=True,
43
+ top_p=0.95,
44
+ top_k=50
45
+ )
46
 
47
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
  return response
49
 
50
 
51
+ def chat(message, history1, history2, model1, model2):
52
+ """
53
+ Handles the chat interaction by getting responses from both models
54
+ and updating their respective histories.
55
+ """
56
  response1 = get_model_response(model1, message)
57
  response2 = get_model_response(model2, message)
58
+
59
+ history1 = history1 or []
60
+ history2 = history2 or []
61
+
62
+ # Update history for Model 1
63
+ history1.append(("User", message))
64
+ history1.append((model1.split("/")[-1], response1))
65
+
66
+ # Update history for Model 2
67
+ history2.append(("User", message))
68
+ history2.append((model2.split("/")[-1], response2))
69
+
70
+ return history1, history2
71
+
72
+
73
+ # Initialize vote counts
74
+ vote_counts = {"model1": 0, "model2": 0}
75
 
76
 
77
+ def upvote_vote(model1, model2):
78
+ """
79
+ Increments the vote count for Model 1 and returns updated counts.
80
+ """
81
+ vote_counts["model1"] += 1
82
+ return f"Votes - {model1.split('/')[-1]}: {vote_counts['model1']}, {model2.split('/')[-1]}: {vote_counts['model2']}"
83
+
84
+
85
+ def downvote_vote(model1, model2):
86
+ """
87
+ Increments the vote count for Model 2 and returns updated counts.
88
+ """
89
+ vote_counts["model2"] += 1
90
+ return f"Votes - {model1.split('/')[-1]}: {vote_counts['model1']}, {model2.split('/')[-1]}: {vote_counts['model2']}"
91
+
92
+
93
+ def clear_chat():
94
+ """
95
+ Clears both chat histories and resets vote counts.
96
+ """
97
+ global vote_counts
98
+ vote_counts = {"model1": 0, "model2": 0}
99
+ return [], [], "Votes - 0, 0"
100
+
101
 
102
  with gr.Blocks() as demo:
103
+ gr.Markdown("# πŸ€– Hugging Face Model Comparison Chat")
104
 
105
+ # Dropdowns for selecting models
106
  with gr.Row():
107
  model1_dropdown = gr.Dropdown(choices=models, label="Model 1", value=models[0])
108
  model2_dropdown = gr.Dropdown(choices=models, label="Model 2", value=models[1])
109
 
110
+ # Separate chatboxes for each model
 
 
 
111
  with gr.Row():
112
+ with gr.Column():
113
+ gr.Markdown("### 🧠 Model 1 Chat")
114
+ chatbot1 = gr.Chatbot(label=f"{models[0].split('/')[-1]} Chat History")
115
+ with gr.Column():
116
+ gr.Markdown("### 🧠 Model 2 Chat")
117
+ chatbot2 = gr.Chatbot(label=f"{models[1].split('/')[-1]} Chat History")
118
 
119
+ # Input textbox for user message
120
+ msg = gr.Textbox(label="πŸ’¬ Your Message", placeholder="Type your message here...")
121
+
122
+ # Buttons for upvote, downvote, and clearing the chat
123
+ with gr.Row():
124
+ upvote = gr.Button("πŸ‘ Upvote Model 1")
125
+ downvote = gr.Button("πŸ‘Ž Downvote Model 2")
126
+ clear = gr.Button("🧹 Clear Chat")
127
+
128
+ # Textbox to display vote counts
129
+ vote_text = gr.Textbox(label="πŸ† Vote Counts", value="Votes - 0, 0", interactive=False)
130
+
131
+ # Define interactions
132
+ msg.submit(
133
+ chat,
134
+ inputs=[msg, chatbot1, chatbot2, model1_dropdown, model2_dropdown],
135
+ outputs=[chatbot1, chatbot2]
136
+ )
137
+
138
+ upvote.click(
139
+ upvote_vote,
140
+ inputs=[model1_dropdown, model2_dropdown],
141
+ outputs=vote_text
142
+ )
143
+
144
+ downvote.click(
145
+ downvote_vote,
146
+ inputs=[model1_dropdown, model2_dropdown],
147
+ outputs=vote_text
148
+ )
149
+
150
+ clear.click(
151
+ clear_chat,
152
+ outputs=[chatbot1, chatbot2, vote_text]
153
+ )
154
+
155
+ # Pre-load all models when the space starts
156
+ load_all_models()
157
 
158
  if __name__ == "__main__":
159
+ demo.launch()