File size: 1,558 Bytes
c691d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
from fastapi import FastAPI
from pydantic import BaseModel
import requests
import os

app = FastAPI(title="ICD-10 Remote Inference")

# 🔑 ضع توكنك من إعدادات Hugging Face (Read access)
HF_TOKEN = os.getenv("HF_TOKEN", "YOUR_HF_API_TOKEN")

# قائمة الموديلات المراد تجربتها
MODELS = [
    "AkshatSurolia/ICD-10-Code-Prediction",
    "rjac/biobert-ICD10-L3-mimic",
    "dataphysician/ModernBERT-icd10-classifier"
]

HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}

class TextInput(BaseModel):
    text: str

@app.get("/")
def home():
    return {"status": "running", "models_to_try": MODELS}

def query_model(model_name: str, text: str):
    url = f"https://api-inference.huggingface.co/models/{model_name}"
    payload = {"inputs": text}
    response = requests.post(url, headers=HEADERS, json=payload, timeout=30)
    if response.status_code == 200:
        return response.json()
    else:
        raise RuntimeError(f"{model_name} failed: {response.status_code}")

@app.post("/predict")
def predict_icd10(input: TextInput):
    for model_name in MODELS:
        try:
            print(f"🔹 Trying remote model: {model_name}")
            result = query_model(model_name, input.text)
            return {
                "model_used": model_name,
                "input": input.text,
                "raw_response": result
            }
        except Exception as e:
            print(f"⚠️ {model_name} failed: {e}")
            continue
    return {"error": "❌ All remote models failed to respond"}