Free to choose a model
Browse files
app.py
CHANGED
@@ -146,75 +146,50 @@ def format_prompt(messages):
|
|
146 |
def add_user_message(user_input, history):
|
147 |
return "", history + [{"role": "user", "content": user_input}]
|
148 |
|
149 |
-
#
|
150 |
model_choices = [
|
151 |
"meta-llama/Llama-3.2-3B-Instruct",
|
152 |
"deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
|
153 |
-
"google/gemma-7b"
|
|
|
154 |
]
|
155 |
|
156 |
-
# UI
|
157 |
with gr.Blocks() as demo:
|
158 |
-
gr.Markdown("## Clinical Chatbot (Streaming)
|
159 |
-
|
160 |
-
default_model = gr.State(
|
161 |
-
|
162 |
-
# @spaces.GPU
|
163 |
-
# def chat_with_model(messages):
|
164 |
-
# global current_model, current_tokenizer
|
165 |
-
# if current_model is None or current_tokenizer is None:
|
166 |
-
# yield messages + [{"role": "assistant", "content": "⚠️ No model loaded."}]
|
167 |
-
# return
|
168 |
-
|
169 |
-
# current_model = current_model.to("cuda").half()
|
170 |
-
|
171 |
-
# prompt = format_prompt(messages)
|
172 |
-
# inputs = current_tokenizer(prompt, return_tensors="pt").to(current_model.device)
|
173 |
-
|
174 |
-
# output_ids = []
|
175 |
-
# messages = messages.copy()
|
176 |
-
# messages.append({"role": "assistant", "content": ""})
|
177 |
-
|
178 |
-
# for token_id in current_model.generate(
|
179 |
-
# **inputs,
|
180 |
-
# max_new_tokens=256,
|
181 |
-
# do_sample=True,
|
182 |
-
# return_dict_in_generate=True,
|
183 |
-
# output_scores=False
|
184 |
-
# ).sequences[0][inputs['input_ids'].shape[-1]:]: # skip input tokens
|
185 |
-
# output_ids.append(token_id.item())
|
186 |
-
# decoded = current_tokenizer.decode(output_ids, skip_special_tokens=False)
|
187 |
-
# if output_ids[-1] == current_tokenizer.eos_token_id:
|
188 |
-
# current_model.to("cpu")
|
189 |
-
# torch.cuda.empty_cache()
|
190 |
-
# return
|
191 |
-
# messages[-1]["content"] = decoded
|
192 |
-
# yield messages
|
193 |
-
|
194 |
-
# current_model.to("cpu")
|
195 |
-
# torch.cuda.empty_cache()
|
196 |
-
# return
|
197 |
|
198 |
with gr.Row():
|
199 |
-
|
200 |
-
|
|
|
201 |
|
|
|
202 |
chatbot = gr.Chatbot(label="Chat", type="messages")
|
203 |
msg = gr.Textbox(label="Your message", placeholder="Enter clinical input...", show_label=False)
|
204 |
clear = gr.Button("Clear")
|
205 |
|
206 |
-
|
|
|
|
|
|
|
207 |
demo.load(fn=load_model_on_selection, inputs=default_model, outputs=model_status)
|
208 |
|
209 |
-
# Load
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
-
# Submit message + stream model response
|
213 |
msg.submit(add_user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
214 |
chat_with_model, chatbot, chatbot
|
215 |
)
|
216 |
-
|
217 |
-
# Clear chat
|
218 |
clear.click(lambda: [], None, chatbot, queue=False)
|
219 |
|
|
|
220 |
demo.launch()
|
|
|
146 |
def add_user_message(user_input, history):
|
147 |
return "", history + [{"role": "user", "content": user_input}]
|
148 |
|
149 |
+
# Curated models
|
150 |
model_choices = [
|
151 |
"meta-llama/Llama-3.2-3B-Instruct",
|
152 |
"deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
|
153 |
+
"google/gemma-7b",
|
154 |
+
"mistralai/Mistral-Small-3.1-24B-Instruct-2503"
|
155 |
]
|
156 |
|
|
|
157 |
with gr.Blocks() as demo:
|
158 |
+
gr.Markdown("## Clinical Chatbot (Streaming)")
|
159 |
+
|
160 |
+
default_model = gr.State(model_choices[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
with gr.Row():
|
163 |
+
mode = gr.Radio(["Choose from list", "Enter custom model"], value="Choose from list", label="Model Input Mode")
|
164 |
+
model_selector = gr.Dropdown(choices=model_choices, label="Select Predefined Model")
|
165 |
+
model_textbox = gr.Textbox(label="Or Enter HF Model Name")
|
166 |
|
167 |
+
model_status = gr.Textbox(label="Model Status", interactive=False)
|
168 |
chatbot = gr.Chatbot(label="Chat", type="messages")
|
169 |
msg = gr.Textbox(label="Your message", placeholder="Enter clinical input...", show_label=False)
|
170 |
clear = gr.Button("Clear")
|
171 |
|
172 |
+
def resolve_model_choice(mode, dropdown_value, textbox_value):
|
173 |
+
return textbox_value.strip() if mode == "Enter custom model" else dropdown_value
|
174 |
+
|
175 |
+
# Load on launch
|
176 |
demo.load(fn=load_model_on_selection, inputs=default_model, outputs=model_status)
|
177 |
|
178 |
+
# Load on user selection
|
179 |
+
mode.select(fn=resolve_model_choice, inputs=[mode, model_selector, model_textbox], outputs=default_model).then(
|
180 |
+
load_model_on_selection, inputs=default_model, outputs=model_status
|
181 |
+
)
|
182 |
+
model_selector.change(fn=resolve_model_choice, inputs=[mode, model_selector, model_textbox], outputs=default_model).then(
|
183 |
+
load_model_on_selection, inputs=default_model, outputs=model_status
|
184 |
+
)
|
185 |
+
model_textbox.submit(fn=resolve_model_choice, inputs=[mode, model_selector, model_textbox], outputs=default_model).then(
|
186 |
+
load_model_on_selection, inputs=default_model, outputs=model_status
|
187 |
+
)
|
188 |
|
|
|
189 |
msg.submit(add_user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
190 |
chat_with_model, chatbot, chatbot
|
191 |
)
|
|
|
|
|
192 |
clear.click(lambda: [], None, chatbot, queue=False)
|
193 |
|
194 |
+
|
195 |
demo.launch()
|