Adarka commited on
Commit
810b499
·
verified ·
1 Parent(s): e6d86b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -1,16 +1,18 @@
1
  import os
2
- os.environ["TRANSFORMERS_CACHE"] = "./cache"
3
-
4
  from fastapi import FastAPI
5
  from pydantic import BaseModel
6
  from typing import Dict
7
- from transformers import pipeline
8
 
9
  app = FastAPI()
10
 
 
 
 
 
11
 
12
- # Utilise un petit modèle pour que ça fonctionne même sur les GPUs limités
13
- llm = pipeline("text2text-generation", model="google/flan-t5-base", max_new_tokens=300)
14
 
15
  class AgentInput(BaseModel):
16
  prompt: str
@@ -18,6 +20,16 @@ class AgentInput(BaseModel):
18
 
19
  @app.post("/analyze")
20
  async def analyze(input: AgentInput):
21
- full_prompt = f"Voici des données JSON :\n{input.data}\n\nQuestion : {input.prompt}\nRéponse :"
22
- result = llm(full_prompt)[0]["generated_text"]
23
- return {"response": result}
 
 
 
 
 
 
 
 
 
 
 
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
+ # Ton token HF (défini dans Settings → Secrets)
10
+ HF_TOKEN = os.getenv("HF_API_TOKEN")
11
+ # Le modèle que tu veux utiliser en remote
12
+ HF_MODEL = "google/flan-t5-base"
13
 
14
+ # Client d’inférence Hugging Face
15
+ client = InferenceClient(token=HF_TOKEN)
16
 
17
  class AgentInput(BaseModel):
18
  prompt: str
 
20
 
21
  @app.post("/analyze")
22
  async def analyze(input: AgentInput):
23
+ full_prompt = (
24
+ f"Voici des données JSON :\n{input.data}\n\n"
25
+ f"Question : {input.prompt}\nRéponse :"
26
+ )
27
+ # Appel remote, pas de cache, pas de pipeline local
28
+ out = client.text_generation(
29
+ model=HF_MODEL,
30
+ inputs=full_prompt,
31
+ parameters={"max_new_tokens": 200}
32
+ )
33
+ # out est une liste de dicts
34
+ response = out[0].generated_text if hasattr(out[0], "generated_text") else out[0]["generated_text"]
35
+ return {"response": response}