Update app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,12 @@ from fastapi.responses import JSONResponse
|
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from huggingface_hub import InferenceClient
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
-
|
8 |
-
import torch
|
9 |
from fastapi import Request
|
10 |
import requests
|
11 |
-
from huggingface_hub import login
|
12 |
-
|
13 |
import numpy as np
|
14 |
import argparse
|
15 |
import os
|
16 |
-
from fastapi import HTTPException
|
17 |
-
|
18 |
|
19 |
HOST = os.environ.get("API_URL", "0.0.0.0")
|
20 |
PORT = os.environ.get("PORT", 7860)
|
@@ -35,36 +30,8 @@ app.add_middleware(
|
|
35 |
allow_headers=["*"],
|
36 |
)
|
37 |
|
38 |
-
HF_TOKEN = os.getenv("REACT_APP_HF_TOKEN")
|
39 |
-
if HF_TOKEN is None:
|
40 |
-
raise RuntimeError(
|
41 |
-
"Définis la variable d’environnement HF_TOKEN dans les Secrets de ton Space."
|
42 |
-
)
|
43 |
-
# équivalent de `huggingface-cli login`
|
44 |
-
login(token=HF_TOKEN)
|
45 |
-
# Charge le tokenizer et le modèle
|
46 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
47 |
-
"mistralai/Mistral-7B-Instruct-v0.3",
|
48 |
-
trust_remote_code=True,
|
49 |
-
use_auth_token=HF_TOKEN,
|
50 |
-
)
|
51 |
-
model = AutoModelForCausalLM.from_pretrained(
|
52 |
-
"mistralai/Mistral-7B-Instruct-v0.3",
|
53 |
-
trust_remote_code=True,
|
54 |
-
use_auth_token=HF_TOKEN,
|
55 |
-
torch_dtype=torch.float32,
|
56 |
-
low_cpu_mem_usage=True,
|
57 |
-
)
|
58 |
-
chat_pipeline = pipeline(
|
59 |
-
"text-generation",
|
60 |
-
model=model,
|
61 |
-
tokenizer=tokenizer,
|
62 |
-
device=-1, # -1 = CPU
|
63 |
-
max_new_tokens=512,
|
64 |
-
temperature=0.7,
|
65 |
-
do_sample=True
|
66 |
-
)
|
67 |
|
|
|
68 |
embedder = SentenceTransformer('sentence-transformers/distiluse-base-multilingual-cased-v1')
|
69 |
|
70 |
@app.post("/api/embed")
|
@@ -97,24 +64,25 @@ async def chat(request: Request):
|
|
97 |
user_message = data.get("message", "").strip()
|
98 |
if not user_message:
|
99 |
raise HTTPException(status_code=400, detail="Le champ 'message' est requis.")
|
100 |
-
|
101 |
-
# Construit le prompt
|
102 |
-
prompt = (
|
103 |
-
"Tu es un assistant médical spécialisé en schizophrénie.\n"
|
104 |
-
"Utilisateur : " + user_message + "\n"
|
105 |
-
"Assistant :"
|
106 |
-
)
|
107 |
|
108 |
try:
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
)
|
113 |
-
|
|
|
114 |
return {"response": bot_msg}
|
115 |
|
116 |
except Exception as e:
|
117 |
-
|
|
|
118 |
|
119 |
|
120 |
@app.get("/data")
|
|
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from huggingface_hub import InferenceClient
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
+
|
|
|
8 |
from fastapi import Request
|
9 |
import requests
|
|
|
|
|
10 |
import numpy as np
|
11 |
import argparse
|
12 |
import os
|
|
|
|
|
13 |
|
14 |
HOST = os.environ.get("API_URL", "0.0.0.0")
|
15 |
PORT = os.environ.get("PORT", 7860)
|
|
|
30 |
allow_headers=["*"],
|
31 |
)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
app = FastAPI()
|
35 |
embedder = SentenceTransformer('sentence-transformers/distiluse-base-multilingual-cased-v1')
|
36 |
|
37 |
@app.post("/api/embed")
|
|
|
64 |
user_message = data.get("message", "").strip()
|
65 |
if not user_message:
|
66 |
raise HTTPException(status_code=400, detail="Le champ 'message' est requis.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
try:
|
69 |
+
# Appel au modèle en mode chat
|
70 |
+
completion = hf_client.chat.completions.create(
|
71 |
+
model="mistralai/Mistral-7B-Instruct-v0.3",
|
72 |
+
messages=[
|
73 |
+
{"role": "system", "content": "Tu es un assistant médical spécialisé en schizophrénie."},
|
74 |
+
{"role": "user", "content": user_message}
|
75 |
+
],
|
76 |
+
max_tokens=512,
|
77 |
+
temperature=0.7,
|
78 |
)
|
79 |
+
|
80 |
+
bot_msg = completion.choices[0].message.content
|
81 |
return {"response": bot_msg}
|
82 |
|
83 |
except Exception as e:
|
84 |
+
# En cas d'erreur d'inférence
|
85 |
+
raise HTTPException(status_code=502, detail=f"Erreur d'inférence HF : {e}")
|
86 |
|
87 |
|
88 |
@app.get("/data")
|