Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import Dict
|
| 5 |
-
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
| 10 |
HF_MODEL = "google/flan-t5-base"
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
class AgentInput(BaseModel):
|
| 15 |
prompt: str
|
|
@@ -21,12 +25,15 @@ async def analyze(input: AgentInput):
|
|
| 21 |
f"Voici des données JSON :\n{input.data}\n\n"
|
| 22 |
f"Question : {input.prompt}\nRéponse :"
|
| 23 |
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
HF_MODEL = "google/flan-t5-base"
|
| 11 |
+
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 12 |
|
| 13 |
+
HEADERS = {
|
| 14 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
|
| 18 |
class AgentInput(BaseModel):
|
| 19 |
prompt: str
|
|
|
|
| 25 |
f"Voici des données JSON :\n{input.data}\n\n"
|
| 26 |
f"Question : {input.prompt}\nRéponse :"
|
| 27 |
)
|
| 28 |
+
payload = {
|
| 29 |
+
"inputs": full_prompt,
|
| 30 |
+
"parameters": {"max_new_tokens": 200}
|
| 31 |
+
}
|
| 32 |
+
# Appel HTTP direct
|
| 33 |
+
resp = requests.post(HF_API_URL, headers=HEADERS, json=payload, timeout=30)
|
| 34 |
+
if resp.status_code != 200:
|
| 35 |
+
raise HTTPException(status_code=500, detail=f"HF Inference error: {resp.text}")
|
| 36 |
+
data = resp.json()
|
| 37 |
+
# Le résultat est une liste [{ "generated_text": "..." }, ...]
|
| 38 |
+
generated = data[0].get("generated_text") or data[0].get("generated_text")
|
| 39 |
+
return {"response": generated}
|