Adarka commited on
Commit
fb20156
·
verified ·
1 Parent(s): 07177f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,15 +1,19 @@
1
  import os
2
- from fastapi import FastAPI
 
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
- client = InferenceClient(token=HF_TOKEN)
 
 
 
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
- # on passe max_new_tokens en argument, pas dans parameters
25
- out = client.text_generation(
26
- full_prompt,
27
- model=HF_MODEL,
28
- max_new_tokens=200
29
- )
30
- # Extraction de la réponse
31
- response = out[0]["generated_text"]
32
- return {"response": response}
 
 
 
 
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}