Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,41 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
from typing import Dict
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
|
|
|
|
|
|
| 13 |
HEADERS = {
|
| 14 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
"Content-Type": "application/json"
|
| 16 |
}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
class AgentInput(BaseModel):
|
| 19 |
prompt: str
|
| 20 |
data: Dict = {}
|
| 21 |
|
| 22 |
@app.post("/analyze")
|
| 23 |
async def analyze(input: AgentInput):
|
| 24 |
-
full_prompt =
|
| 25 |
-
|
| 26 |
-
f"Question : {input.prompt}\nRéponse :"
|
| 27 |
-
)
|
| 28 |
-
payload = {
|
| 29 |
"inputs": full_prompt,
|
| 30 |
"parameters": {"max_new_tokens": 200}
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
if resp.status_code != 200:
|
| 35 |
-
raise HTTPException(status_code=500, detail=f"HF Inference error: {resp.text}")
|
|
|
|
| 36 |
data = resp.json()
|
| 37 |
-
|
| 38 |
-
generated = data[0].get("generated_text") or data[0].get("generated_text")
|
| 39 |
return {"response": generated}
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
from fastapi import FastAPI, HTTPException
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
| 8 |
+
if not HF_TOKEN:
|
| 9 |
+
raise RuntimeError("❌ HF_API_TOKEN n'est pas défini comme secret dans ton Space !")
|
| 10 |
|
| 11 |
+
HF_MODEL = "google/flan-t5-base"
|
| 12 |
+
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 13 |
HEADERS = {
|
| 14 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
"Content-Type": "application/json"
|
| 16 |
}
|
| 17 |
|
| 18 |
+
from pydantic import BaseModel
|
| 19 |
+
from typing import Dict
|
| 20 |
+
|
| 21 |
class AgentInput(BaseModel):
|
| 22 |
prompt: str
|
| 23 |
data: Dict = {}
|
| 24 |
|
| 25 |
@app.post("/analyze")
|
| 26 |
async def analyze(input: AgentInput):
|
| 27 |
+
full_prompt = f"Voici des données JSON : {input.data}\n\nQuestion : {input.prompt}\nRéponse :"
|
| 28 |
+
resp = requests.post(HF_API_URL, headers=HEADERS, json={
|
|
|
|
|
|
|
|
|
|
| 29 |
"inputs": full_prompt,
|
| 30 |
"parameters": {"max_new_tokens": 200}
|
| 31 |
+
}, timeout=30)
|
| 32 |
+
|
| 33 |
+
if resp.status_code == 404:
|
| 34 |
+
# On intercepte le 404 pour te donner plus de contexte
|
| 35 |
+
raise HTTPException(status_code=404, detail=f"Inference API Not Found. URL={HF_API_URL}")
|
| 36 |
if resp.status_code != 200:
|
| 37 |
+
raise HTTPException(status_code=500, detail=f"HF Inference error ({resp.status_code}): {resp.text}")
|
| 38 |
+
|
| 39 |
data = resp.json()
|
| 40 |
+
generated = data[0].get("generated_text")
|
|
|
|
| 41 |
return {"response": generated}
|