Spaces:
Sleeping
Sleeping
| 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 | |
| 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}") | |
| 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"} | |