Update app.py
Browse files
app.py
CHANGED
|
@@ -5,20 +5,20 @@ from pydantic import BaseModel
|
|
| 5 |
from llama_cpp import Llama
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
MODEL_URL = "https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3
|
| 10 |
MODEL_PATH = "llama-3.1-8B-Q2_K.gguf"
|
| 11 |
|
| 12 |
-
print("--- Vérification
|
| 13 |
if not os.path.exists(MODEL_PATH):
|
| 14 |
-
print("Téléchargement
|
| 15 |
try:
|
| 16 |
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 17 |
print("Téléchargement réussi !")
|
| 18 |
except Exception as e:
|
| 19 |
-
print(f"Erreur
|
| 20 |
else:
|
| 21 |
-
print("
|
| 22 |
|
| 23 |
app = FastAPI()
|
| 24 |
|
|
@@ -30,14 +30,10 @@ app.add_middleware(
|
|
| 30 |
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
|
| 33 |
-
print("--- Chargement
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
llm = Llama(model_path=MODEL_PATH, n_ctx=1024, n_threads=2)
|
| 38 |
-
print("Modèle Llama correctement initialisé !")
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print(f"Erreur critique lors de l'initialisation de Llama : {e}")
|
| 41 |
|
| 42 |
class ChatRequest(BaseModel):
|
| 43 |
message: str
|
|
@@ -51,15 +47,11 @@ def root_check():
|
|
| 51 |
def chat_endpoint(req: ChatRequest):
|
| 52 |
global llm
|
| 53 |
if llm is None:
|
| 54 |
-
raise HTTPException(
|
| 55 |
-
status_code=500,
|
| 56 |
-
detail="Le modèle IA n'a pas pu être chargé."
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
# Prompt système adapté au format de Llama 3
|
| 60 |
prompt = (
|
| 61 |
"You are BEGA AI, a helpful AI assistant developed by BEGA Company. "
|
| 62 |
-
"Reply in the same language used in the user's message.\n\n"
|
| 63 |
)
|
| 64 |
|
| 65 |
for human_msg, assistant_msg in req.history:
|
|
@@ -68,9 +60,10 @@ def chat_endpoint(req: ChatRequest):
|
|
| 68 |
prompt += f"User: {req.message}\nAssistant:"
|
| 69 |
|
| 70 |
try:
|
|
|
|
| 71 |
output = llm(
|
| 72 |
prompt,
|
| 73 |
-
max_tokens=
|
| 74 |
stop=["User:", "\nUser:", "Assistant:", "\nAssistant:"],
|
| 75 |
temperature=0.4
|
| 76 |
)
|
|
|
|
| 5 |
from llama_cpp import Llama
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
|
| 8 |
+
# On garde votre Llama-3.1 8B ultra-léger (Q2_K ou IQ3_XS)
|
| 9 |
+
MODEL_URL = "https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct-IQ3_XS.gguf"
|
| 10 |
MODEL_PATH = "llama-3.1-8B-Q2_K.gguf"
|
| 11 |
|
| 12 |
+
print("--- Vérification / Téléchargement de Llama 3.1 8B ---")
|
| 13 |
if not os.path.exists(MODEL_PATH):
|
| 14 |
+
print("Téléchargement du modèle (veuillez patienter quelques minutes)...")
|
| 15 |
try:
|
| 16 |
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 17 |
print("Téléchargement réussi !")
|
| 18 |
except Exception as e:
|
| 19 |
+
print(f"Erreur téléchargement : {e}")
|
| 20 |
else:
|
| 21 |
+
print("Modèle Llama 3.1 8B détecté sur le disque.")
|
| 22 |
|
| 23 |
app = FastAPI()
|
| 24 |
|
|
|
|
| 30 |
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
|
| 33 |
+
print("--- Chargement en mémoire (CPU) ---")
|
| 34 |
+
# n_ctx=256 : Contexte très bas pour forcer Llama 8B à rentrer dans la petite RAM du serveur gratuit
|
| 35 |
+
llm = Llama(model_path=MODEL_PATH, n_ctx=256, n_threads=2)
|
| 36 |
+
print("Moteur Llama 3.1 8B opérationnel !")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
class ChatRequest(BaseModel):
|
| 39 |
message: str
|
|
|
|
| 47 |
def chat_endpoint(req: ChatRequest):
|
| 48 |
global llm
|
| 49 |
if llm is None:
|
| 50 |
+
raise HTTPException(status_code=500, detail="Modèle non chargé.")
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
| 52 |
prompt = (
|
| 53 |
"You are BEGA AI, a helpful AI assistant developed by BEGA Company. "
|
| 54 |
+
"Reply concisely in the same language used in the user's message.\n\n"
|
| 55 |
)
|
| 56 |
|
| 57 |
for human_msg, assistant_msg in req.history:
|
|
|
|
| 60 |
prompt += f"User: {req.message}\nAssistant:"
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# max_tokens=50 : On demande une réponse très courte pour éviter le crash/timeout du serveur
|
| 64 |
output = llm(
|
| 65 |
prompt,
|
| 66 |
+
max_tokens=50,
|
| 67 |
stop=["User:", "\nUser:", "Assistant:", "\nAssistant:"],
|
| 68 |
temperature=0.4
|
| 69 |
)
|