Spaces:
Running on Zero
Running on Zero
Upload 2 files
Browse files- app.py +128 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
BASE_MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 7 |
+
ADAPTER_MODEL = "rolmaxx/MediGuide-QLoRA"
|
| 8 |
+
|
| 9 |
+
print("Loading tokenizer...")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 11 |
+
|
| 12 |
+
print("Loading base model...")
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
BASE_MODEL,
|
| 15 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 16 |
+
device_map="auto",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
print("Loading MediGuide QLoRA adapter...")
|
| 20 |
+
model = PeftModel.from_pretrained(
|
| 21 |
+
model,
|
| 22 |
+
ADAPTER_MODEL,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
model.eval()
|
| 26 |
+
|
| 27 |
+
print("MediGuide loaded successfully!")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def generate_response(message, history):
|
| 31 |
+
messages = [
|
| 32 |
+
{
|
| 33 |
+
"role": "system",
|
| 34 |
+
"content": (
|
| 35 |
+
"You are MediGuide, a medical conversational assistant. "
|
| 36 |
+
"Provide clear, helpful, and appropriately cautious responses. "
|
| 37 |
+
"Do not claim to replace a qualified healthcare professional."
|
| 38 |
+
),
|
| 39 |
+
}
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
for user_msg, assistant_msg in history:
|
| 43 |
+
messages.append({"role": "user", "content": user_msg})
|
| 44 |
+
if assistant_msg:
|
| 45 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 46 |
+
|
| 47 |
+
messages.append({"role": "user", "content": message})
|
| 48 |
+
|
| 49 |
+
prompt = tokenizer.apply_chat_template(
|
| 50 |
+
messages,
|
| 51 |
+
tokenize=False,
|
| 52 |
+
add_generation_prompt=True,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
inputs = tokenizer(
|
| 56 |
+
prompt,
|
| 57 |
+
return_tensors="pt",
|
| 58 |
+
).to(model.device)
|
| 59 |
+
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
outputs = model.generate(
|
| 62 |
+
**inputs,
|
| 63 |
+
max_new_tokens=256,
|
| 64 |
+
do_sample=True,
|
| 65 |
+
temperature=0.7,
|
| 66 |
+
top_p=0.9,
|
| 67 |
+
repetition_penalty=1.1,
|
| 68 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
generated_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
|
| 72 |
+
|
| 73 |
+
response = tokenizer.decode(
|
| 74 |
+
generated_tokens,
|
| 75 |
+
skip_special_tokens=True,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return response.strip()
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
with gr.Blocks(title="MediGuide") as demo:
|
| 82 |
+
|
| 83 |
+
gr.Markdown(
|
| 84 |
+
"""
|
| 85 |
+
# 🩺 MediGuide
|
| 86 |
+
|
| 87 |
+
**QLoRA fine-tuned medical conversational assistant**
|
| 88 |
+
|
| 89 |
+
MediGuide is built on `Qwen2.5-1.5B-Instruct` and fine-tuned
|
| 90 |
+
using QLoRA on medical dialogue data.
|
| 91 |
+
|
| 92 |
+
⚠️ **Disclaimer:** MediGuide is for research and educational
|
| 93 |
+
purposes only. It is not a substitute for professional medical advice.
|
| 94 |
+
"""
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
chatbot = gr.Chatbot(
|
| 98 |
+
label="MediGuide",
|
| 99 |
+
height=500,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
msg = gr.Textbox(
|
| 103 |
+
label="Describe your symptoms or ask a medical question",
|
| 104 |
+
placeholder="e.g. What are common symptoms of the flu?",
|
| 105 |
+
lines=2,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
clear = gr.Button("Clear")
|
| 109 |
+
|
| 110 |
+
def respond(message, history):
|
| 111 |
+
response = generate_response(message, history)
|
| 112 |
+
history.append((message, response))
|
| 113 |
+
return "", history
|
| 114 |
+
|
| 115 |
+
msg.submit(
|
| 116 |
+
respond,
|
| 117 |
+
inputs=[msg, chatbot],
|
| 118 |
+
outputs=[msg, chatbot],
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
clear.click(
|
| 122 |
+
lambda: [],
|
| 123 |
+
outputs=chatbot,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.45.0
|
| 2 |
+
peft>=0.20.0
|
| 3 |
+
torch
|
| 4 |
+
gradio
|
| 5 |
+
accelerate
|
| 6 |
+
safetensors
|