MohamedTry commited on
Commit
bf507fb
·
verified ·
1 Parent(s): ea9aa73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -2
app.py CHANGED
@@ -7,7 +7,7 @@ app = FastAPI(title="MedGemma ICD-10 Remote API")
7
 
8
  HF_TOKEN = os.getenv("HF_TOKEN", "YOUR_HF_API_TOKEN")
9
  MODEL_ID = "google/medgemma-4b-it"
10
- API_URL = f"https://router.huggingface.co/hf-inference/v1/models/{MODEL_ID}"
11
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
12
 
13
  class TextInput(BaseModel):
@@ -20,12 +20,14 @@ def root():
20
  @app.post("/predict")
21
  def predict_icd10(input: TextInput):
22
  try:
 
23
  dummy = Image.fromarray(np.zeros((224, 224, 3), dtype=np.uint8))
24
  buf = io.BytesIO()
25
  dummy.save(buf, format="PNG")
26
  image_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
27
 
28
  prompt = f"<image>\nConvert this medical note into ICD-10 code only:\n{input.text}\nICD-10 code:"
 
29
  payload = {"inputs": {"image": image_b64, "prompt": prompt}}
30
 
31
  r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=90)
@@ -35,6 +37,6 @@ def predict_icd10(input: TextInput):
35
  return {"icd10_code": data[0]["generated_text"].strip()}
36
  return {"raw_response": data}
37
  else:
38
- return {"error": f"Hugging Face Router error {r.status_code}", "details": r.text}
39
  except Exception as e:
40
  return {"error": str(e)}
 
7
 
8
  HF_TOKEN = os.getenv("HF_TOKEN", "YOUR_HF_API_TOKEN")
9
  MODEL_ID = "google/medgemma-4b-it"
10
+ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
11
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
12
 
13
  class TextInput(BaseModel):
 
20
  @app.post("/predict")
21
  def predict_icd10(input: TextInput):
22
  try:
23
+ # dummy image
24
  dummy = Image.fromarray(np.zeros((224, 224, 3), dtype=np.uint8))
25
  buf = io.BytesIO()
26
  dummy.save(buf, format="PNG")
27
  image_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
28
 
29
  prompt = f"<image>\nConvert this medical note into ICD-10 code only:\n{input.text}\nICD-10 code:"
30
+
31
  payload = {"inputs": {"image": image_b64, "prompt": prompt}}
32
 
33
  r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=90)
 
37
  return {"icd10_code": data[0]["generated_text"].strip()}
38
  return {"raw_response": data}
39
  else:
40
+ return {"error": f"Hugging Face API error {r.status_code}", "details": r.text}
41
  except Exception as e:
42
  return {"error": str(e)}