Spaces:
Sleeping
Sleeping
Add HF_TOKEN auth + fallback model for robustness
Browse files
app.py
CHANGED
|
@@ -5,9 +5,25 @@ import os
|
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
SYSTEM_PROMPT = """You are DeepMed-R1, a medical reasoning AI trained with GRPO and multi-objective clinical rewards on AMD MI300X.
|
| 13 |
|
|
@@ -21,13 +37,13 @@ For every medical question, demonstrate systematic clinical reasoning:
|
|
| 21 |
|
| 22 |
Present reasoning inside <think></think> tags, then provide your final answer.
|
| 23 |
For MCQ, end with \\boxed{X} where X is the correct letter.
|
| 24 |
-
Be thorough but concise.
|
| 25 |
|
| 26 |
EXAMPLES = [
|
| 27 |
["A 65-year-old male with hypertension presents with sudden 'worst headache of my life,' neck stiffness, photophobia. BP 180/100. Most likely diagnosis?\nA. Migraine\nB. Subarachnoid hemorrhage\nC. Meningitis\nD. Tension headache"],
|
| 28 |
["A 28-year-old woman: fatigue, weight gain, cold intolerance. TSH 12 mIU/L, Free T4 0.5 ng/dL. Initial treatment?\nA. Levothyroxine\nB. Liothyronine\nC. Methimazole\nD. Radioactive iodine"],
|
| 29 |
["3-month-old with projectile non-bilious vomiting, olive-shaped RUQ mass, metabolic alkalosis. Diagnosis?\nA. Pyloric stenosis\nB. Intussusception\nC. Malrotation\nD. Hirschsprung disease"],
|
| 30 |
-
["55-year-old diabetic: RUQ pain, fever 39.2C, jaundice (Charcot
|
| 31 |
["22-year-old post-MVC: left chest pain, absent breath sounds left, trachea deviated right, JVD, BP 80/50. Immediate management?\nA. Chest X-ray\nB. CT chest\nC. Needle decompression left chest\nD. Intubation"],
|
| 32 |
]
|
| 33 |
|
|
@@ -46,11 +62,19 @@ def respond(message, history):
|
|
| 46 |
response += delta
|
| 47 |
yield response
|
| 48 |
except Exception as e:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
with gr.Blocks(title="DeepMed-R1", theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 53 |
-
gr.Markdown("""
|
| 54 |
# π₯ DeepMed-R1: Medical Reasoning AI
|
| 55 |
|
| 56 |
**Systematic clinical reasoning powered by GRPO + Multi-Objective Clinical Rewards**
|
|
@@ -77,9 +101,9 @@ Built for **AMD Developer Hackathon 2026** β Track 2: Fine-Tuning on AMD GPUs
|
|
| 77 |
| Training | GRPO + DAPO loss + CRPO rewards |
|
| 78 |
| Innovations | iGRPO (Feb 2026) + AERO (Feb 2026) + Curriculum (Mar 2026) |
|
| 79 |
| Hardware | AMD MI300X (192GB HBM3) |
|
| 80 |
-
| Reward
|
| 81 |
|
| 82 |
-
π [Code & Training
|
| 83 |
π [Gazal-R1](https://arxiv.org/abs/2506.21594) |
|
| 84 |
π [Clinical-R1](https://arxiv.org/abs/2512.00601) |
|
| 85 |
π [iGRPO](https://arxiv.org/abs/2602.09000)
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
+
|
| 10 |
+
# Try multiple providers in order of preference
|
| 11 |
+
def get_client():
|
| 12 |
+
models = [
|
| 13 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
| 14 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 15 |
+
"meta-llama/Llama-3.1-8B-Instruct",
|
| 16 |
+
]
|
| 17 |
+
for model in models:
|
| 18 |
+
try:
|
| 19 |
+
c = InferenceClient(model, token=HF_TOKEN)
|
| 20 |
+
# Quick test
|
| 21 |
+
return c, model
|
| 22 |
+
except Exception:
|
| 23 |
+
continue
|
| 24 |
+
return InferenceClient(models[0], token=HF_TOKEN), models[0]
|
| 25 |
+
|
| 26 |
+
client, MODEL_USED = get_client()
|
| 27 |
|
| 28 |
SYSTEM_PROMPT = """You are DeepMed-R1, a medical reasoning AI trained with GRPO and multi-objective clinical rewards on AMD MI300X.
|
| 29 |
|
|
|
|
| 37 |
|
| 38 |
Present reasoning inside <think></think> tags, then provide your final answer.
|
| 39 |
For MCQ, end with \\boxed{X} where X is the correct letter.
|
| 40 |
+
Be thorough but concise. Ground reasoning in pathophysiology and evidence."""
|
| 41 |
|
| 42 |
EXAMPLES = [
|
| 43 |
["A 65-year-old male with hypertension presents with sudden 'worst headache of my life,' neck stiffness, photophobia. BP 180/100. Most likely diagnosis?\nA. Migraine\nB. Subarachnoid hemorrhage\nC. Meningitis\nD. Tension headache"],
|
| 44 |
["A 28-year-old woman: fatigue, weight gain, cold intolerance. TSH 12 mIU/L, Free T4 0.5 ng/dL. Initial treatment?\nA. Levothyroxine\nB. Liothyronine\nC. Methimazole\nD. Radioactive iodine"],
|
| 45 |
["3-month-old with projectile non-bilious vomiting, olive-shaped RUQ mass, metabolic alkalosis. Diagnosis?\nA. Pyloric stenosis\nB. Intussusception\nC. Malrotation\nD. Hirschsprung disease"],
|
| 46 |
+
["55-year-old diabetic: RUQ pain, fever 39.2C, jaundice (Charcot triad). WBC 18K, bilirubin 5.2, CBD stone. Next step?\nA. Cholecystectomy\nB. ERCP with sphincterotomy\nC. MRCP\nD. PTC"],
|
| 47 |
["22-year-old post-MVC: left chest pain, absent breath sounds left, trachea deviated right, JVD, BP 80/50. Immediate management?\nA. Chest X-ray\nB. CT chest\nC. Needle decompression left chest\nD. Intubation"],
|
| 48 |
]
|
| 49 |
|
|
|
|
| 62 |
response += delta
|
| 63 |
yield response
|
| 64 |
except Exception as e:
|
| 65 |
+
error_msg = str(e)
|
| 66 |
+
if "api_key" in error_msg.lower() or "token" in error_msg.lower() or "401" in error_msg:
|
| 67 |
+
yield ("β οΈ **Authentication Required**\n\n"
|
| 68 |
+
"Please add your HF_TOKEN as a Space secret:\n"
|
| 69 |
+
"1. Go to Space Settings β Repository secrets\n"
|
| 70 |
+
"2. Add secret: Name=`HF_TOKEN`, Value=your token\n\n"
|
| 71 |
+
f"Error: {error_msg}")
|
| 72 |
+
else:
|
| 73 |
+
yield f"Error: {error_msg}\n\nPlease try again."
|
| 74 |
|
| 75 |
|
| 76 |
with gr.Blocks(title="DeepMed-R1", theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 77 |
+
gr.Markdown(f"""
|
| 78 |
# π₯ DeepMed-R1: Medical Reasoning AI
|
| 79 |
|
| 80 |
**Systematic clinical reasoning powered by GRPO + Multi-Objective Clinical Rewards**
|
|
|
|
| 101 |
| Training | GRPO + DAPO loss + CRPO rewards |
|
| 102 |
| Innovations | iGRPO (Feb 2026) + AERO (Feb 2026) + Curriculum (Mar 2026) |
|
| 103 |
| Hardware | AMD MI300X (192GB HBM3) |
|
| 104 |
+
| Reward | Accuracy (2.0) + Reasoning (1.0) + Consistency (0.5) + Length (0.3) |
|
| 105 |
|
| 106 |
+
π [Code & Training](https://huggingface.co/asusf15/DeepMed-R1) |
|
| 107 |
π [Gazal-R1](https://arxiv.org/abs/2506.21594) |
|
| 108 |
π [Clinical-R1](https://arxiv.org/abs/2512.00601) |
|
| 109 |
π [iGRPO](https://arxiv.org/abs/2602.09000)
|