Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
|
| 10 |
# ============================================================
|
| 11 |
MODEL_ID = "augtoma/qCammel-13"
|
| 12 |
|
| 13 |
-
# 4-bit quantization (
|
| 14 |
bnb_config = BitsAndBytesConfig(
|
| 15 |
load_in_4bit=True,
|
| 16 |
bnb_4bit_use_double_quant=True,
|
|
@@ -52,38 +52,33 @@ def save_memory(history):
|
|
| 52 |
json.dump(history, f, indent=2)
|
| 53 |
|
| 54 |
# ============================================================
|
| 55 |
-
# PROMPT
|
| 56 |
# ============================================================
|
| 57 |
SYSTEM_PROMPT = (
|
| 58 |
-
"You are Dr. Camel, a
|
| 59 |
-
"You
|
| 60 |
-
"
|
| 61 |
-
"Always
|
| 62 |
-
"
|
| 63 |
-
"If
|
| 64 |
-
"You have memory of the ongoing chat — remember what the patient previously said (like symptoms, medications, and feelings). "
|
| 65 |
-
"Use that information to provide thoughtful and relevant responses."
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
def build_conversation_prompt(history):
|
| 69 |
-
"""Builds a memory-aware
|
| 70 |
-
|
| 71 |
-
for turn in history[-6:]:
|
| 72 |
if turn["role"] == "user":
|
| 73 |
-
|
| 74 |
elif turn["role"] == "assistant":
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
SYSTEM_PROMPT
|
| 79 |
-
+ "\n\nConversation memory:\n"
|
| 80 |
-
+ memory_summary
|
| 81 |
-
+ "\nDoctor: "
|
| 82 |
-
)
|
| 83 |
-
return prompt
|
| 84 |
|
| 85 |
# ============================================================
|
| 86 |
-
# TEXT GENERATION (
|
| 87 |
# ============================================================
|
| 88 |
def generate_stream(history, max_new_tokens=512):
|
| 89 |
prompt = build_conversation_prompt(history)
|
|
@@ -94,9 +89,12 @@ def generate_stream(history, max_new_tokens=512):
|
|
| 94 |
input_ids=inputs["input_ids"],
|
| 95 |
attention_mask=inputs["attention_mask"],
|
| 96 |
max_new_tokens=max_new_tokens,
|
| 97 |
-
repetition_penalty=1.
|
|
|
|
|
|
|
| 98 |
do_sample=True,
|
| 99 |
-
streamer=streamer
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
|
@@ -114,6 +112,10 @@ def respond(user_message, history):
|
|
| 114 |
if not user_message.strip():
|
| 115 |
return gr.update(), history
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
history.append({"role": "user", "content": user_message})
|
| 118 |
partial = ""
|
| 119 |
|
|
@@ -137,14 +139,14 @@ with gr.Blocks(title="🩺 Dr. Camel — Medical Chatbot", css=".footer {display
|
|
| 137 |
gr.Markdown(
|
| 138 |
"""
|
| 139 |
# 🩺 Dr. Camel — AI Medical Assistant
|
| 140 |
-
|
| 141 |
-
*(
|
| 142 |
"""
|
| 143 |
)
|
| 144 |
|
| 145 |
chatbot = gr.Chatbot(type="messages", elem_id="chatbot", height=520, value=chat_memory)
|
| 146 |
with gr.Row():
|
| 147 |
-
txt = gr.Textbox(show_label=False, placeholder="Describe your symptoms or ask a
|
| 148 |
clear = gr.Button("🧹 Clear Chat")
|
| 149 |
|
| 150 |
state = gr.State(chat_memory)
|
|
|
|
| 10 |
# ============================================================
|
| 11 |
MODEL_ID = "augtoma/qCammel-13"
|
| 12 |
|
| 13 |
+
# 4-bit quantization (saves GPU memory)
|
| 14 |
bnb_config = BitsAndBytesConfig(
|
| 15 |
load_in_4bit=True,
|
| 16 |
bnb_4bit_use_double_quant=True,
|
|
|
|
| 52 |
json.dump(history, f, indent=2)
|
| 53 |
|
| 54 |
# ============================================================
|
| 55 |
+
# SYSTEM PROMPT (doctor personality)
|
| 56 |
# ============================================================
|
| 57 |
SYSTEM_PROMPT = (
|
| 58 |
+
"You are Dr. Camel, a professional, empathetic, and helpful medical doctor. "
|
| 59 |
+
"You will respond only when the patient speaks. "
|
| 60 |
+
"Never start the conversation by yourself. "
|
| 61 |
+
"Always reply as 'Doctor:' and never simulate the patient's responses. "
|
| 62 |
+
"Your tone should be calm, supportive, and medically informative. "
|
| 63 |
+
"If symptoms seem serious, politely suggest seeing a healthcare professional."
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# ============================================================
|
| 67 |
+
# CONVERSATION PROMPT BUILDER
|
| 68 |
+
# ============================================================
|
| 69 |
def build_conversation_prompt(history):
|
| 70 |
+
"""Builds a memory-aware prompt (doctor only replies after patient)."""
|
| 71 |
+
conversation = SYSTEM_PROMPT + "\n\n"
|
| 72 |
+
for turn in history[-6:]:
|
| 73 |
if turn["role"] == "user":
|
| 74 |
+
conversation += f"Patient: {turn['content'].strip()}\n"
|
| 75 |
elif turn["role"] == "assistant":
|
| 76 |
+
conversation += f"Doctor: {turn['content'].strip()}\n"
|
| 77 |
+
conversation += "Doctor:"
|
| 78 |
+
return conversation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# ============================================================
|
| 81 |
+
# TEXT GENERATION (STREAMING)
|
| 82 |
# ============================================================
|
| 83 |
def generate_stream(history, max_new_tokens=512):
|
| 84 |
prompt = build_conversation_prompt(history)
|
|
|
|
| 89 |
input_ids=inputs["input_ids"],
|
| 90 |
attention_mask=inputs["attention_mask"],
|
| 91 |
max_new_tokens=max_new_tokens,
|
| 92 |
+
repetition_penalty=1.05,
|
| 93 |
+
temperature=0.7,
|
| 94 |
+
top_p=0.9,
|
| 95 |
do_sample=True,
|
| 96 |
+
streamer=streamer,
|
| 97 |
+
pad_token_id=tokenizer.eos_token_id
|
| 98 |
)
|
| 99 |
|
| 100 |
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
|
|
|
| 112 |
if not user_message.strip():
|
| 113 |
return gr.update(), history
|
| 114 |
|
| 115 |
+
# Prevent the bot from talking first
|
| 116 |
+
if len(history) == 0 and "Doctor" in user_message:
|
| 117 |
+
return gr.update(), history
|
| 118 |
+
|
| 119 |
history.append({"role": "user", "content": user_message})
|
| 120 |
partial = ""
|
| 121 |
|
|
|
|
| 139 |
gr.Markdown(
|
| 140 |
"""
|
| 141 |
# 🩺 Dr. Camel — AI Medical Assistant
|
| 142 |
+
Ask about your symptoms or medical concerns, and Dr. Camel will respond with care and clarity.
|
| 143 |
+
*(For demo purposes only — not real medical advice.)*
|
| 144 |
"""
|
| 145 |
)
|
| 146 |
|
| 147 |
chatbot = gr.Chatbot(type="messages", elem_id="chatbot", height=520, value=chat_memory)
|
| 148 |
with gr.Row():
|
| 149 |
+
txt = gr.Textbox(show_label=False, placeholder="Describe your symptoms or ask a question...", lines=2)
|
| 150 |
clear = gr.Button("🧹 Clear Chat")
|
| 151 |
|
| 152 |
state = gr.State(chat_memory)
|