Spaces:
Sleeping
Sleeping
Hackathon Builder commited on
Commit Β·
c8d750f
1
Parent(s): d146cae
Fix BYOK key propagation and enhance agent reasoning precision
Browse files
backend/main.py
CHANGED
|
@@ -60,8 +60,10 @@ app.add_middleware(CORSMiddleware, allow_origins=["*"],
|
|
| 60 |
# ββ Gemini helper βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
import time
|
| 62 |
def _gemini(prompt: str, retries: int = 4, override_key: str = None) -> Optional[Dict]:
|
| 63 |
-
|
| 64 |
-
if
|
|
|
|
|
|
|
| 65 |
return None
|
| 66 |
payload = {
|
| 67 |
"contents": [{"parts": [{"text": prompt}]}],
|
|
@@ -69,7 +71,7 @@ def _gemini(prompt: str, retries: int = 4, override_key: str = None) -> Optional
|
|
| 69 |
}
|
| 70 |
for attempt in range(retries):
|
| 71 |
try:
|
| 72 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={
|
| 73 |
resp = _requests.post(url, json=payload, timeout=20)
|
| 74 |
if resp.status_code == 429 and attempt < retries - 1:
|
| 75 |
time.sleep(2 ** attempt)
|
|
@@ -104,9 +106,10 @@ Return ONLY raw JSON (no markdown fences, no explanation):
|
|
| 104 |
"time_critical_flags": ["finding1", "finding2"],
|
| 105 |
"do_not_delay": ["action in first 5 min"],
|
| 106 |
"triage_rationale": "2-3 sentence rationale",
|
| 107 |
-
"agent_reasoning": ["
|
| 108 |
}}
|
| 109 |
-
Severity scale: 1=IMMEDIATE 2=EMERGENT 3=URGENT 4=LESS URGENT 5=NON-URGENT
|
|
|
|
| 110 |
r = _gemini(prompt)
|
| 111 |
if r:
|
| 112 |
return r
|
|
@@ -157,9 +160,10 @@ Return ONLY raw JSON:
|
|
| 157 |
"condition_contraindications": ["condition-based risk"],
|
| 158 |
"safe_alternatives": ["alternative drug or approach"],
|
| 159 |
"monitoring_required": ["monitor SpO2 every 5 min"],
|
| 160 |
-
"agent_reasoning": ["Cross-referencing triage severity with medications
|
| 161 |
}}
|
| 162 |
-
treatment_verdict options: SAFE | CAUTION | CONTRAINDICATED | NOT_EVALUATED
|
|
|
|
| 163 |
r = _gemini(prompt, override_key=override_key)
|
| 164 |
if r:
|
| 165 |
return r
|
|
@@ -394,6 +398,8 @@ RAW DOCUMENT:
|
|
| 394 |
@app.get("/orchestrate/{patient_id}")
|
| 395 |
@app.post("/orchestrate/{patient_id}")
|
| 396 |
def orchestrate(patient_id: str, x_gemini_key: Optional[str] = Header(None)):
|
|
|
|
|
|
|
| 397 |
"""
|
| 398 |
Runs all 5 AI agents on the patient record and returns a complete
|
| 399 |
emergency intelligence brief.
|
|
|
|
| 60 |
# ββ Gemini helper βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
import time
|
| 62 |
def _gemini(prompt: str, retries: int = 4, override_key: str = None) -> Optional[Dict]:
|
| 63 |
+
key = override_key or _KEY
|
| 64 |
+
if override_key:
|
| 65 |
+
print(f"[DEBUG] Using Override Gemini Key: {override_key[:6]}...")
|
| 66 |
+
if not key:
|
| 67 |
return None
|
| 68 |
payload = {
|
| 69 |
"contents": [{"parts": [{"text": prompt}]}],
|
|
|
|
| 71 |
}
|
| 72 |
for attempt in range(retries):
|
| 73 |
try:
|
| 74 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={key}"
|
| 75 |
resp = _requests.post(url, json=payload, timeout=20)
|
| 76 |
if resp.status_code == 429 and attempt < retries - 1:
|
| 77 |
time.sleep(2 ** attempt)
|
|
|
|
| 106 |
"time_critical_flags": ["finding1", "finding2"],
|
| 107 |
"do_not_delay": ["action in first 5 min"],
|
| 108 |
"triage_rationale": "2-3 sentence rationale",
|
| 109 |
+
"agent_reasoning": ["Analyze specific vitals and chief complaint", "Evaluate severity against emergency standards"]
|
| 110 |
}}
|
| 111 |
+
Severity scale: 1=IMMEDIATE 2=EMERGENT 3=URGENT 4=LESS URGENT 5=NON-URGENT
|
| 112 |
+
BE SPECIFIC. Do not say 'Analyze vitals', say 'Analyze HR 140 and SpO2 88%' if those are in the data."""
|
| 113 |
r = _gemini(prompt)
|
| 114 |
if r:
|
| 115 |
return r
|
|
|
|
| 160 |
"condition_contraindications": ["condition-based risk"],
|
| 161 |
"safe_alternatives": ["alternative drug or approach"],
|
| 162 |
"monitoring_required": ["monitor SpO2 every 5 min"],
|
| 163 |
+
"agent_reasoning": ["Cross-referencing triage severity with medications", "Verifying allergy overlaps with proposed treatments"]
|
| 164 |
}}
|
| 165 |
+
treatment_verdict options: SAFE | CAUTION | CONTRAINDICATED | NOT_EVALUATED
|
| 166 |
+
BE CLINICALLY PRECISE. Identify specific interactions like 'Warfarin + Aspirin risk'."""
|
| 167 |
r = _gemini(prompt, override_key=override_key)
|
| 168 |
if r:
|
| 169 |
return r
|
|
|
|
| 398 |
@app.get("/orchestrate/{patient_id}")
|
| 399 |
@app.post("/orchestrate/{patient_id}")
|
| 400 |
def orchestrate(patient_id: str, x_gemini_key: Optional[str] = Header(None)):
|
| 401 |
+
if x_gemini_key:
|
| 402 |
+
print(f"[ORCHESTRATE] Received Custom API Key Header: {x_gemini_key[:8]}...")
|
| 403 |
"""
|
| 404 |
Runs all 5 AI agents on the patient record and returns a complete
|
| 405 |
emergency intelligence brief.
|
frontend/dist/assets/{index-Cp8I6i_j.js β index-BuwfU1hM.js}
RENAMED
|
The diff for this file is too large to render.
See raw diff
|
|
|
frontend/dist/index.html
CHANGED
|
@@ -7,7 +7,7 @@
|
|
| 7 |
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 8 |
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
| 9 |
<title>CodeBlue Context</title>
|
| 10 |
-
<script type="module" crossorigin src="/assets/index-
|
| 11 |
<link rel="stylesheet" crossorigin href="/assets/index-kcxpakQ_.css">
|
| 12 |
</head>
|
| 13 |
<body>
|
|
|
|
| 7 |
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 8 |
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
| 9 |
<title>CodeBlue Context</title>
|
| 10 |
+
<script type="module" crossorigin src="/assets/index-BuwfU1hM.js"></script>
|
| 11 |
<link rel="stylesheet" crossorigin href="/assets/index-kcxpakQ_.css">
|
| 12 |
</head>
|
| 13 |
<body>
|
frontend/src/App.jsx
CHANGED
|
@@ -89,8 +89,13 @@ export default function App() {
|
|
| 89 |
|
| 90 |
try {
|
| 91 |
const geminiKey = localStorage.getItem('gemini_key');
|
| 92 |
-
const headers =
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
if (!res.ok) throw new Error('API error');
|
| 95 |
const data = await res.json();
|
| 96 |
|
|
|
|
| 89 |
|
| 90 |
try {
|
| 91 |
const geminiKey = localStorage.getItem('gemini_key');
|
| 92 |
+
const headers = { 'Content-Type': 'application/json' };
|
| 93 |
+
if (geminiKey) headers['x-gemini-key'] = geminiKey;
|
| 94 |
+
|
| 95 |
+
const res = await fetch(`${API}/orchestrate/${p.id}`, {
|
| 96 |
+
method: 'POST',
|
| 97 |
+
headers
|
| 98 |
+
});
|
| 99 |
if (!res.ok) throw new Error('API error');
|
| 100 |
const data = await res.json();
|
| 101 |
|