Spaces:
Sleeping
Sleeping
File size: 1,525 Bytes
a332b8e ea9aa73 a332b8e 848a585 61b7072 c1f418f a332b8e bf507fb ea9aa73 a332b8e ea9aa73 a332b8e bf507fb a332b8e 61b7072 a332b8e 61b7072 bf507fb ea9aa73 9b42cfb ea9aa73 61b7072 ea9aa73 a332b8e bf507fb a332b8e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | from fastapi import FastAPI
from pydantic import BaseModel
import requests, base64, numpy as np, io, os
from PIL import Image
app = FastAPI(title="MedGemma ICD-10 Remote API")
HF_TOKEN = os.getenv("HF_TOKEN", "YOUR_HF_API_TOKEN")
MODEL_ID = "google/medgemma-4b-it"
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
class TextInput(BaseModel):
text: str
@app.get("/")
def root():
return {"status": "running", "model": MODEL_ID, "api": API_URL}
@app.post("/predict")
def predict_icd10(input: TextInput):
try:
# dummy image
dummy = Image.fromarray(np.zeros((224, 224, 3), dtype=np.uint8))
buf = io.BytesIO()
dummy.save(buf, format="PNG")
image_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
prompt = f"<image>\nConvert this medical note into ICD-10 code only:\n{input.text}\nICD-10 code:"
payload = {"inputs": {"image": image_b64, "prompt": prompt}}
r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=90)
if r.status_code == 200:
data = r.json()
if isinstance(data, list) and "generated_text" in data[0]:
return {"icd10_code": data[0]["generated_text"].strip()}
return {"raw_response": data}
else:
return {"error": f"Hugging Face API error {r.status_code}", "details": r.text}
except Exception as e:
return {"error": str(e)}
|