Spaces:
Sleeping
Sleeping
Fix: remove assistant prefill, use simple robust approach
Browse files
app.py
CHANGED
|
@@ -10,39 +10,29 @@ client = InferenceClient(token=HF_TOKEN)
|
|
| 10 |
|
| 11 |
MODEL = "Qwen/Qwen2.5-72B-Instruct"
|
| 12 |
|
| 13 |
-
SYSTEM_PROMPT = """You are DeepMed-R1, a medical reasoning AI trained with GRPO on AMD MI300X.
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
[Extract ALL key clinical features: age, sex, history, symptoms, vitals, labs, imaging]
|
| 20 |
|
| 21 |
-
|
| 22 |
-
[List 3-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
[Explain
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
- B. [option text] → [why correct or incorrect]
|
| 33 |
-
- C. [option text] → [why correct or incorrect]
|
| 34 |
-
- D. [option text] → [why correct or incorrect]
|
| 35 |
-
</think>
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
**Final Answer:** \\boxed{X}
|
| 40 |
-
|
| 41 |
-
Rules:
|
| 42 |
-
- The <think> section MUST appear FIRST and contain ALL 5 numbered sections
|
| 43 |
-
- Each section must have at least 2-3 sentences of clinical detail
|
| 44 |
-
- Be specific: name diseases, mechanisms, criteria by proper medical terms
|
| 45 |
-
- The reasoning must JUSTIFY the final answer logically"""
|
| 46 |
|
| 47 |
|
| 48 |
EXAMPLES = [
|
|
@@ -59,12 +49,9 @@ def respond(message, history):
|
|
| 59 |
for h in history:
|
| 60 |
if h[0]: messages.append({"role": "user", "content": h[0]})
|
| 61 |
if h[1]: messages.append({"role": "assistant", "content": h[1]})
|
| 62 |
-
|
| 63 |
messages.append({"role": "user", "content": message})
|
| 64 |
-
# Prefill the assistant response to force it to start with <think>
|
| 65 |
-
messages.append({"role": "assistant", "content": "<think>\n## 1. Information Analysis\n"})
|
| 66 |
|
| 67 |
-
response = "
|
| 68 |
try:
|
| 69 |
stream = client.chat_completion(
|
| 70 |
model=MODEL,
|
|
@@ -80,11 +67,10 @@ def respond(message, history):
|
|
| 80 |
if hasattr(delta, "content") and delta.content:
|
| 81 |
response += delta.content
|
| 82 |
yield response
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
-
|
| 85 |
-
yield f"⚠️ Error: {str(e)}"
|
| 86 |
-
else:
|
| 87 |
-
yield response
|
| 88 |
|
| 89 |
|
| 90 |
with gr.Blocks(title="DeepMed-R1", theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
|
|
| 10 |
|
| 11 |
MODEL = "Qwen/Qwen2.5-72B-Instruct"
|
| 12 |
|
| 13 |
+
SYSTEM_PROMPT = """You are DeepMed-R1, a medical reasoning AI trained with GRPO on AMD MI300X.
|
| 14 |
|
| 15 |
+
For EVERY question you MUST follow this format exactly:
|
| 16 |
|
| 17 |
+
**Step 1 - Key Findings:**
|
| 18 |
+
[List the important clinical features from the question]
|
|
|
|
| 19 |
|
| 20 |
+
**Step 2 - Differential Diagnosis:**
|
| 21 |
+
[List 3-4 possible diagnoses ranked by likelihood]
|
| 22 |
|
| 23 |
+
**Step 3 - Pathophysiology:**
|
| 24 |
+
[Explain WHY the most likely diagnosis causes these symptoms]
|
| 25 |
|
| 26 |
+
**Step 4 - Option Analysis:**
|
| 27 |
+
- A: [evaluate this option]
|
| 28 |
+
- B: [evaluate this option]
|
| 29 |
+
- C: [evaluate this option]
|
| 30 |
+
- D: [evaluate this option]
|
| 31 |
|
| 32 |
+
**Step 5 - Final Answer:**
|
| 33 |
+
The answer is **X** because [one sentence justification].
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
IMPORTANT: Always show ALL 5 steps. Never skip steps. Be detailed and specific."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
EXAMPLES = [
|
|
|
|
| 49 |
for h in history:
|
| 50 |
if h[0]: messages.append({"role": "user", "content": h[0]})
|
| 51 |
if h[1]: messages.append({"role": "assistant", "content": h[1]})
|
|
|
|
| 52 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
response = ""
|
| 55 |
try:
|
| 56 |
stream = client.chat_completion(
|
| 57 |
model=MODEL,
|
|
|
|
| 67 |
if hasattr(delta, "content") and delta.content:
|
| 68 |
response += delta.content
|
| 69 |
yield response
|
| 70 |
+
if not response:
|
| 71 |
+
yield "No response received. Please try again."
|
| 72 |
except Exception as e:
|
| 73 |
+
yield f"⚠️ Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
with gr.Blocks(title="DeepMed-R1", theme=gr.themes.Soft(primary_hue="blue")) as demo:
|