Spaces:
Runtime error
Runtime error
Commit
·
6b29344
0
Parent(s):
Initial commit: runtime LFS snapshot download approach
Browse files- .gitignore +9 -0
- app.py +162 -0
- download.py +29 -0
- requirements.txt +8 -0
.gitignore
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
model
|
| 2 |
+
venv
|
| 3 |
+
checkpoint
|
| 4 |
+
|
| 5 |
+
all_diagnosing_doctor_outputs.json
|
| 6 |
+
all_patient_followups.json
|
| 7 |
+
all_questioning_doctor_outputs.json
|
| 8 |
+
all_summarizer_outputs.json
|
| 9 |
+
all_treatment_outputs.json
|
app.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from huggingface_hub import snapshot_download
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# ——— CONFIG ———
|
| 8 |
+
REPO_ID = "CodCodingCode/llama-3.1-8b-clinical"
|
| 9 |
+
SUBFOLDER = "checkpoint-45000"
|
| 10 |
+
HF_TOKEN = os.environ["HUGGINGFACE_HUB_TOKEN"] # set in Settings→Secrets
|
| 11 |
+
|
| 12 |
+
# ——— SNAPSHOT & LOAD ———
|
| 13 |
+
# This will grab all .json and .safetensors under checkpoint-45000:
|
| 14 |
+
local_dir = snapshot_download(
|
| 15 |
+
repo_id=REPO_ID,
|
| 16 |
+
subfolder=SUBFOLDER,
|
| 17 |
+
token=HF_TOKEN,
|
| 18 |
+
allow_patterns=["*.json", "*.safetensors"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Now point at that folder:
|
| 22 |
+
MODEL_DIR = local_dir # e.g. ~/.cache/huggingface/…/checkpoint-45000
|
| 23 |
+
|
| 24 |
+
# Load tokenizer & model from the real files you just pulled:
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 26 |
+
MODEL_DIR,
|
| 27 |
+
use_fast=False,
|
| 28 |
+
trust_remote_code=True,
|
| 29 |
+
)
|
| 30 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
+
MODEL_DIR,
|
| 32 |
+
device_map="auto",
|
| 33 |
+
torch_dtype=torch.float16,
|
| 34 |
+
trust_remote_code=True,
|
| 35 |
+
)
|
| 36 |
+
model.eval()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# === Role Agent with instruction/input/output format ===
|
| 41 |
+
class RoleAgent:
|
| 42 |
+
def __init__(self, role_instruction):
|
| 43 |
+
self.role_instruction = role_instruction
|
| 44 |
+
|
| 45 |
+
def act(self, input_text):
|
| 46 |
+
prompt = (
|
| 47 |
+
f"Instruction: {self.role_instruction}\n"
|
| 48 |
+
f"Input: {input_text}\n"
|
| 49 |
+
f"Output:"
|
| 50 |
+
)
|
| 51 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 52 |
+
outputs = model.generate(
|
| 53 |
+
**inputs,
|
| 54 |
+
max_new_tokens=256,
|
| 55 |
+
do_sample=True,
|
| 56 |
+
temperature=0.7,
|
| 57 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 58 |
+
)
|
| 59 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
+
|
| 61 |
+
# extract THINKING / ANSWER if present
|
| 62 |
+
thinking, answer = "", response
|
| 63 |
+
if "THINKING:" in response and "ANSWER:" in response and "END" in response:
|
| 64 |
+
block = response.split("THINKING:")[1].split("END")[0]
|
| 65 |
+
thinking = block.split("ANSWER:")[0].strip()
|
| 66 |
+
answer = block.split("ANSWER:")[1].strip()
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"instruction": f"You are {self.role_instruction}.",
|
| 70 |
+
"input": input_text,
|
| 71 |
+
"thinking": thinking,
|
| 72 |
+
"output": answer,
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# === Agents ===
|
| 77 |
+
summarizer = RoleAgent(
|
| 78 |
+
"You are a clinical summarizer trained to extract structured vignettes from doctor–patient dialogues."
|
| 79 |
+
)
|
| 80 |
+
diagnoser = RoleAgent(
|
| 81 |
+
"You are a board-certified diagnostician that diagnoses patients."
|
| 82 |
+
)
|
| 83 |
+
questioner = RoleAgent("You are a physician asking questions to diagnose a patient.")
|
| 84 |
+
|
| 85 |
+
treatment_agent = RoleAgent(
|
| 86 |
+
"You are a board-certified clinician. Based on the diagnosis and patient vignette provided below, suggest a concise treatment plan that could realistically be initiated by a primary care physician or psychiatrist."
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# === Inference State ===
|
| 91 |
+
conversation_history = []
|
| 92 |
+
summary = ""
|
| 93 |
+
diagnosis = ""
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# === Gradio Inference ===
|
| 97 |
+
def simulate_interaction(user_input, iterations=1):
|
| 98 |
+
history = [f"Doctor: What brings you in today?", f"Patient: {user_input}"]
|
| 99 |
+
summary, diagnosis = "", ""
|
| 100 |
+
|
| 101 |
+
for i in range(iterations):
|
| 102 |
+
# Summarize
|
| 103 |
+
sum_in = "\n".join(history) + f"\nPrevious Vignette: {summary}"
|
| 104 |
+
sum_out = summarizer.act(sum_in)
|
| 105 |
+
summary = sum_out["output"]
|
| 106 |
+
|
| 107 |
+
# Diagnose
|
| 108 |
+
diag_out = diagnoser.act(summary)
|
| 109 |
+
diagnosis = diag_out["output"]
|
| 110 |
+
|
| 111 |
+
# Question
|
| 112 |
+
q_in = f"Vignette: {summary}\nCurrent Estimated Diagnosis: {diag_out['thinking']} {diagnosis}"
|
| 113 |
+
q_out = questioner.act(q_in)
|
| 114 |
+
history.append(f"Doctor: {q_out['output']}")
|
| 115 |
+
history.append("Patient: (awaiting response)")
|
| 116 |
+
|
| 117 |
+
# Treatment
|
| 118 |
+
treatment_out = treatment_agent.act(
|
| 119 |
+
f"Diagnosis: {diagnosis}\nVignette: {summary}"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
return {
|
| 123 |
+
"summary": sum_out,
|
| 124 |
+
"diagnosis": diag_out,
|
| 125 |
+
"question": q_out,
|
| 126 |
+
"treatment": treatment_out,
|
| 127 |
+
"conversation": "\n".join(history),
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# === Gradio UI ===
|
| 132 |
+
def ui_fn(user_input):
|
| 133 |
+
res = simulate_interaction(user_input)
|
| 134 |
+
return f"""📋 Vignette Summary:
|
| 135 |
+
💭 THINKING: {res['summary']['thinking']}
|
| 136 |
+
ANSWER: {res['summary']['output']}
|
| 137 |
+
|
| 138 |
+
🩺 Diagnosis:
|
| 139 |
+
💭 THINKING: {res['diagnosis']['thinking']}
|
| 140 |
+
ANSWER: {res['diagnosis']['output']}
|
| 141 |
+
T
|
| 142 |
+
❓ Follow-up Question:
|
| 143 |
+
💭 THINKING: {res['question']['thinking']}
|
| 144 |
+
ANSWER: {res['question']['output']}
|
| 145 |
+
|
| 146 |
+
💊 Treatment Plan:
|
| 147 |
+
{res['treatment']['output']}
|
| 148 |
+
|
| 149 |
+
💬 Conversation:
|
| 150 |
+
{res['conversation']}
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
demo = gr.Interface(
|
| 155 |
+
fn=ui_fn,
|
| 156 |
+
inputs=gr.Textbox(label="Patient Response"),
|
| 157 |
+
outputs=gr.Textbox(label="Doctor Simulation Output"),
|
| 158 |
+
title="🧠 AI Doctor Multi-Agent Reasoning",
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
demo.launch(share=True)
|
download.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import hf_hub_download
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# ——— CONFIG ———
|
| 5 |
+
REPO_ID = "CodCodingCode/llama-3.1-8b-clinical"
|
| 6 |
+
SUBDIR = "checkpoint-45000"
|
| 7 |
+
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN") # make sure you set this in Secrets
|
| 8 |
+
|
| 9 |
+
# Ensure output directory exists
|
| 10 |
+
os.makedirs(SUBDIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# List of shards to download
|
| 13 |
+
shards = [
|
| 14 |
+
"model-00001-of-00004.safetensors",
|
| 15 |
+
"model-00002-of-00004.safetensors",
|
| 16 |
+
"model-00003-of-00004.safetensors",
|
| 17 |
+
"model-00004-of-00004.safetensors",
|
| 18 |
+
"model.safetensors.index.json", # the index file
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
for fname in shards:
|
| 22 |
+
local_path = hf_hub_download(
|
| 23 |
+
repo_id=REPO_ID,
|
| 24 |
+
filename=f"{SUBDIR}/{fname}",
|
| 25 |
+
token=HF_TOKEN,
|
| 26 |
+
local_dir=".", # download into the Space root
|
| 27 |
+
local_dir_use_symlinks=False, # ensure actual file copy
|
| 28 |
+
)
|
| 29 |
+
print(f"Downloaded {fname} to {local_path}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
transformers>=4.38.0
|
| 3 |
+
torch>=2.1.0
|
| 4 |
+
peft>=0.9.0
|
| 5 |
+
accelerate>=0.24.0
|
| 6 |
+
bitsandbytes
|
| 7 |
+
safetensors
|
| 8 |
+
gradio
|