Download app.py from Adarka/agent: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/Adarka/agent/resolve/main/app.py
- Command line
-
hf download hf://spaces/Adarka/agent/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Adarka/agent/resolve/main/app.py
1.31 kB
| import os | |
| import requests | |
| from fastapi import FastAPI, HTTPException | |
| app = FastAPI() | |
| HF_TOKEN = os.getenv("HF_API_TOKEN") | |
| if not HF_TOKEN: | |
| raise RuntimeError("❌ HF_API_TOKEN n'est pas défini comme secret dans ton Space !") | |
| HF_MODEL = "google/flan-t5-base" | |
| HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}" | |
| HEADERS = { | |
| "Authorization": f"Bearer {HF_TOKEN}", | |
| "Content-Type": "application/json" | |
| } | |
| from pydantic import BaseModel | |
| from typing import Dict | |
| class AgentInput(BaseModel): | |
| prompt: str | |
| data: Dict = {} | |
| async def analyze(input: AgentInput): | |
| full_prompt = f"Voici des données JSON : {input.data}\n\nQuestion : {input.prompt}\nRéponse :" | |
| resp = requests.post(HF_API_URL, headers=HEADERS, json={ | |
| "inputs": full_prompt, | |
| "parameters": {"max_new_tokens": 200} | |
| }, timeout=30) | |
| if resp.status_code == 404: | |
| # On intercepte le 404 pour te donner plus de contexte | |
| raise HTTPException(status_code=404, detail=f"Inference API Not Found. URL={HF_API_URL}") | |
| if resp.status_code != 200: | |
| raise HTTPException(status_code=500, detail=f"HF Inference error ({resp.status_code}): {resp.text}") | |
| data = resp.json() | |
| generated = data[0].get("generated_text") | |
| return {"response": generated} | |