Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a
|
| 50 |
-
gr.Slider(minimum=1, maximum=
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch\
|
| 3 |
+
import spaces
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
| 6 |
+
# Load MedScholar model and tokenizer
|
| 7 |
+
model_name = "yasserrmd/MedScholar-1.5B"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_name,
|
| 11 |
+
device_map="auto",
|
| 12 |
+
torch_dtype=torch.float16,
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Chat function (streaming style)
|
| 18 |
+
@spaces.GPU
|
| 19 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 20 |
+
# Prepare the full conversation
|
| 21 |
+
conversation = [{"role": "system", "content": system_message}]
|
| 22 |
+
for user_msg, bot_reply in history:
|
| 23 |
+
if user_msg:
|
| 24 |
+
conversation.append({"role": "user", "content": user_msg})
|
| 25 |
+
if bot_reply:
|
| 26 |
+
conversation.append({"role": "assistant", "content": bot_reply})
|
| 27 |
+
conversation.append({"role": "user", "content": message})
|
| 28 |
|
| 29 |
+
# Convert conversation into prompt string
|
| 30 |
+
prompt = ""
|
| 31 |
+
for turn in conversation:
|
| 32 |
+
if turn["role"] == "system":
|
| 33 |
+
prompt += f"<|system|>\n{turn['content']}\n"
|
| 34 |
+
elif turn["role"] == "user":
|
| 35 |
+
prompt += f"<|user|>\n{turn['content']}\n"
|
| 36 |
+
elif turn["role"] == "assistant":
|
| 37 |
+
prompt += f"<|assistant|>\n{turn['content']}\n"
|
| 38 |
+
prompt += "<|assistant|>\n"
|
| 39 |
|
| 40 |
+
# Tokenize
|
| 41 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 42 |
|
| 43 |
+
# Generate with streaming-like loop
|
| 44 |
+
output_ids = model.generate(
|
| 45 |
+
**inputs,
|
| 46 |
+
max_new_tokens=max_tokens,
|
| 47 |
temperature=temperature,
|
| 48 |
top_p=top_p,
|
| 49 |
+
do_sample=True,
|
| 50 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 51 |
+
)
|
| 52 |
|
| 53 |
+
# Decode and stream the new content
|
| 54 |
+
decoded = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 55 |
+
response = decoded.split("<|assistant|>\n")[-1].strip()
|
| 56 |
+
yield response
|
| 57 |
|
| 58 |
+
# Build Gradio interface
|
|
|
|
|
|
|
|
|
|
| 59 |
demo = gr.ChatInterface(
|
| 60 |
respond,
|
| 61 |
additional_inputs=[
|
| 62 |
+
gr.Textbox(value="You are a helpful medical assistant.", label="System message"),
|
| 63 |
+
gr.Slider(minimum=1, maximum=1024, value=512, step=1, label="Max new tokens"),
|
| 64 |
+
gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Temperature"),
|
| 65 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
],
|
| 67 |
+
title="🩺 MedScholar-1.5B: Medical Chatbot"
|
| 68 |
)
|
| 69 |
|
|
|
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch()
|