Spaces:
Sleeping
Sleeping
Upload backend.py with huggingface_hub
Browse files- backend.py +69 -42
backend.py
CHANGED
|
@@ -6,8 +6,8 @@ Flow:
|
|
| 6 |
1. Receive Croatian discharge letter from doctor
|
| 7 |
2. Translate to English (Gemini 3.1 Flash Lite)
|
| 8 |
3. Run concurrent error-detection analysis:
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
4. Parse structured output and return errors + suggestions
|
| 12 |
"""
|
| 13 |
|
|
@@ -19,6 +19,7 @@ from dataclasses import dataclass, field
|
|
| 19 |
from typing import Optional
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
from google import genai
|
|
|
|
| 22 |
from groq import Groq
|
| 23 |
|
| 24 |
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), ".env"))
|
|
@@ -32,6 +33,13 @@ def get_gemini_client() -> genai.Client:
|
|
| 32 |
return genai.Client(api_key=key)
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def get_groq_client() -> Groq:
|
| 36 |
return Groq(api_key=os.environ.get("GROQ_API_KEY_OSS"))
|
| 37 |
|
|
@@ -176,55 +184,53 @@ def parse_model_json(raw: str) -> dict:
|
|
| 176 |
# Model calls
|
| 177 |
# ---------------------------------------------------------------------------
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
-
def
|
|
|
|
| 184 |
start = time.time()
|
| 185 |
try:
|
| 186 |
-
client =
|
| 187 |
response = client.chat.completions.create(
|
| 188 |
-
model=
|
| 189 |
messages=[
|
| 190 |
{"role": "system", "content": ERROR_CHECK_SYSTEM_PROMPT},
|
| 191 |
{"role": "user", "content": ERROR_CHECK_USER_PROMPT.format(clinical_text=clinical_text)},
|
| 192 |
],
|
| 193 |
-
temperature=0.2,
|
| 194 |
max_tokens=4096,
|
| 195 |
)
|
| 196 |
raw = response.choices[0].message.content
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
parsed = parse_model_json(raw)
|
| 200 |
-
errors = [
|
| 201 |
-
ParsedError(
|
| 202 |
-
description=e.get("description", ""),
|
| 203 |
-
category=e.get("category", "other"),
|
| 204 |
-
severity=e.get("severity", "medium"),
|
| 205 |
-
quote=e.get("quote", ""),
|
| 206 |
-
)
|
| 207 |
-
for e in parsed.get("errors", [])
|
| 208 |
-
]
|
| 209 |
-
suggestions = [
|
| 210 |
-
ParsedSuggestion(
|
| 211 |
-
description=s.get("description", ""),
|
| 212 |
-
category=s.get("category", "other"),
|
| 213 |
-
)
|
| 214 |
-
for s in parsed.get("suggestions", [])
|
| 215 |
-
]
|
| 216 |
-
return ModelResult(
|
| 217 |
-
model_name=model_label,
|
| 218 |
-
raw_response=raw,
|
| 219 |
-
errors=errors,
|
| 220 |
-
suggestions=suggestions,
|
| 221 |
-
summary=parsed.get("summary", ""),
|
| 222 |
-
success=True,
|
| 223 |
-
latency_seconds=round(latency, 2),
|
| 224 |
-
)
|
| 225 |
except Exception as exc:
|
| 226 |
return ModelResult(
|
| 227 |
-
model_name=
|
| 228 |
raw_response="",
|
| 229 |
success=False,
|
| 230 |
error_message=str(exc),
|
|
@@ -232,12 +238,30 @@ def _call_groq_model(model_id: str, model_label: str, clinical_text: str) -> Mod
|
|
| 232 |
)
|
| 233 |
|
| 234 |
|
| 235 |
-
def call_model_a(clinical_text: str) -> ModelResult:
|
| 236 |
-
return _call_groq_model(GROQ_MODEL_A, "Qwen 3 32B", clinical_text)
|
| 237 |
-
|
| 238 |
-
|
| 239 |
def call_model_b(clinical_text: str) -> ModelResult:
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
|
| 243 |
# ---------------------------------------------------------------------------
|
|
@@ -270,6 +294,9 @@ def run_error_check(croatian_text: str) -> AnalysisResponse:
|
|
| 270 |
# ---------------------------------------------------------------------------
|
| 271 |
|
| 272 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 273 |
sample = """Bolesnik 68 godina, dolazi zbog bolova u prsištu.
|
| 274 |
Dijagnoza: STEMI prednje stijenke.
|
| 275 |
Terapija: Aspirin 100mg, Klopidogrel 75mg, Ramipril 5mg, Atorvastatin 40mg.
|
|
|
|
| 6 |
1. Receive Croatian discharge letter from doctor
|
| 7 |
2. Translate to English (Gemini 3.1 Flash Lite)
|
| 8 |
3. Run concurrent error-detection analysis:
|
| 9 |
+
- DeepSeek Reasoner (via DeepSeek API)
|
| 10 |
+
- GPT-OSS-120B (via Groq)
|
| 11 |
4. Parse structured output and return errors + suggestions
|
| 12 |
"""
|
| 13 |
|
|
|
|
| 19 |
from typing import Optional
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
from google import genai
|
| 22 |
+
from openai import OpenAI
|
| 23 |
from groq import Groq
|
| 24 |
|
| 25 |
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), ".env"))
|
|
|
|
| 33 |
return genai.Client(api_key=key)
|
| 34 |
|
| 35 |
|
| 36 |
+
def get_deepseek_client() -> OpenAI:
|
| 37 |
+
return OpenAI(
|
| 38 |
+
api_key=os.environ.get("DEEPSEEK_API_KEY"),
|
| 39 |
+
base_url="https://api.deepseek.com",
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
def get_groq_client() -> Groq:
|
| 44 |
return Groq(api_key=os.environ.get("GROQ_API_KEY_OSS"))
|
| 45 |
|
|
|
|
| 184 |
# Model calls
|
| 185 |
# ---------------------------------------------------------------------------
|
| 186 |
|
| 187 |
+
def _parse_to_result(model_label: str, raw: str, latency: float) -> ModelResult:
|
| 188 |
+
parsed = parse_model_json(raw)
|
| 189 |
+
errors = [
|
| 190 |
+
ParsedError(
|
| 191 |
+
description=e.get("description", ""),
|
| 192 |
+
category=e.get("category", "other"),
|
| 193 |
+
severity=e.get("severity", "medium"),
|
| 194 |
+
quote=e.get("quote", ""),
|
| 195 |
+
)
|
| 196 |
+
for e in parsed.get("errors", [])
|
| 197 |
+
]
|
| 198 |
+
suggestions = [
|
| 199 |
+
ParsedSuggestion(
|
| 200 |
+
description=s.get("description", ""),
|
| 201 |
+
category=s.get("category", "other"),
|
| 202 |
+
)
|
| 203 |
+
for s in parsed.get("suggestions", [])
|
| 204 |
+
]
|
| 205 |
+
return ModelResult(
|
| 206 |
+
model_name=model_label,
|
| 207 |
+
raw_response=raw,
|
| 208 |
+
errors=errors,
|
| 209 |
+
suggestions=suggestions,
|
| 210 |
+
summary=parsed.get("summary", ""),
|
| 211 |
+
success=True,
|
| 212 |
+
latency_seconds=round(latency, 2),
|
| 213 |
+
)
|
| 214 |
|
| 215 |
|
| 216 |
+
def call_model_a(clinical_text: str) -> ModelResult:
|
| 217 |
+
"""DeepSeek Reasoner via DeepSeek API."""
|
| 218 |
start = time.time()
|
| 219 |
try:
|
| 220 |
+
client = get_deepseek_client()
|
| 221 |
response = client.chat.completions.create(
|
| 222 |
+
model="deepseek-reasoner",
|
| 223 |
messages=[
|
| 224 |
{"role": "system", "content": ERROR_CHECK_SYSTEM_PROMPT},
|
| 225 |
{"role": "user", "content": ERROR_CHECK_USER_PROMPT.format(clinical_text=clinical_text)},
|
| 226 |
],
|
|
|
|
| 227 |
max_tokens=4096,
|
| 228 |
)
|
| 229 |
raw = response.choices[0].message.content
|
| 230 |
+
return _parse_to_result("DeepSeek Reasoner", raw, time.time() - start)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
except Exception as exc:
|
| 232 |
return ModelResult(
|
| 233 |
+
model_name="DeepSeek Reasoner",
|
| 234 |
raw_response="",
|
| 235 |
success=False,
|
| 236 |
error_message=str(exc),
|
|
|
|
| 238 |
)
|
| 239 |
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
def call_model_b(clinical_text: str) -> ModelResult:
|
| 242 |
+
"""GPT-OSS-120B via Groq."""
|
| 243 |
+
start = time.time()
|
| 244 |
+
try:
|
| 245 |
+
client = get_groq_client()
|
| 246 |
+
response = client.chat.completions.create(
|
| 247 |
+
model="openai/gpt-oss-120b",
|
| 248 |
+
messages=[
|
| 249 |
+
{"role": "system", "content": ERROR_CHECK_SYSTEM_PROMPT},
|
| 250 |
+
{"role": "user", "content": ERROR_CHECK_USER_PROMPT.format(clinical_text=clinical_text)},
|
| 251 |
+
],
|
| 252 |
+
temperature=0.2,
|
| 253 |
+
max_tokens=4096,
|
| 254 |
+
)
|
| 255 |
+
raw = response.choices[0].message.content
|
| 256 |
+
return _parse_to_result("GPT-OSS-120B", raw, time.time() - start)
|
| 257 |
+
except Exception as exc:
|
| 258 |
+
return ModelResult(
|
| 259 |
+
model_name="GPT-OSS-120B",
|
| 260 |
+
raw_response="",
|
| 261 |
+
success=False,
|
| 262 |
+
error_message=str(exc),
|
| 263 |
+
latency_seconds=round(time.time() - start, 2),
|
| 264 |
+
)
|
| 265 |
|
| 266 |
|
| 267 |
# ---------------------------------------------------------------------------
|
|
|
|
| 294 |
# ---------------------------------------------------------------------------
|
| 295 |
|
| 296 |
if __name__ == "__main__":
|
| 297 |
+
import sys, io
|
| 298 |
+
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
|
| 299 |
+
|
| 300 |
sample = """Bolesnik 68 godina, dolazi zbog bolova u prsištu.
|
| 301 |
Dijagnoza: STEMI prednje stijenke.
|
| 302 |
Terapija: Aspirin 100mg, Klopidogrel 75mg, Ramipril 5mg, Atorvastatin 40mg.
|