Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,68 +1,71 @@
|
|
| 1 |
import os
|
| 2 |
import uvicorn
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from openai import OpenAI
|
| 6 |
-
from pydantic import BaseModel
|
| 7 |
-
from typing import List
|
| 8 |
|
| 9 |
-
# 1.
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
ALBERT_BASE_URL = "https://albert.api.etalab.gouv.fr/v1"
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# Initialisation
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
api_key=ALBERT_API_KEY,
|
| 19 |
-
base_url=ALBERT_BASE_URL
|
| 20 |
-
)
|
| 21 |
|
| 22 |
app = FastAPI()
|
| 23 |
|
| 24 |
# 2. Configuration CORS
|
| 25 |
app.add_middleware(
|
| 26 |
CORSMiddleware,
|
| 27 |
-
allow_origins=["*"],
|
| 28 |
allow_credentials=True,
|
| 29 |
allow_methods=["*"],
|
| 30 |
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
class Message(BaseModel):
|
| 35 |
-
role: str
|
| 36 |
-
content: str
|
| 37 |
-
|
| 38 |
-
class ChatRequest(BaseModel):
|
| 39 |
-
messages: List[Message]
|
| 40 |
-
|
| 41 |
@app.post("/api/chat")
|
| 42 |
-
async def
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# Affichage dans les logs de Hugging Face
|
| 57 |
-
print(f"RÉPONSE IA : {reponse_texte}")
|
| 58 |
|
| 59 |
-
return {"content": reponse_texte}
|
| 60 |
-
|
| 61 |
except Exception as e:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return {"error": "Une erreur est survenue lors de la communication avec Albert.", "details": str(e)}
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
# Port 7860 standard pour Hugging Face Spaces
|
| 68 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import uvicorn
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from openai import OpenAI
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# 1. Récupération des deux secrets depuis Hugging Face
|
| 8 |
+
ALBERT_KEY_PRINCIPALE = os.getenv("ALBERT_API_KEY")
|
| 9 |
+
ALBERT_KEY_RISQUE = os.getenv("ALBERT_API_RISQUE")
|
| 10 |
+
|
| 11 |
ALBERT_BASE_URL = "https://albert.api.etalab.gouv.fr/v1"
|
| 12 |
+
# Le modèle spécifique demandé
|
| 13 |
+
MODEL_ID = "mistralai/Mistral-Small-3.2-24B-Instruct-2506"
|
| 14 |
|
| 15 |
+
# Initialisation des deux clients OpenAI compatibles Albert
|
| 16 |
+
client_antibio = OpenAI(api_key=ALBERT_KEY_PRINCIPALE, base_url=ALBERT_BASE_URL)
|
| 17 |
+
client_sismo = OpenAI(api_key=ALBERT_KEY_RISQUE, base_url=ALBERT_BASE_URL)
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
# 2. Configuration CORS
|
| 22 |
app.add_middleware(
|
| 23 |
CORSMiddleware,
|
| 24 |
+
allow_origins=["*"],
|
| 25 |
allow_credentials=True,
|
| 26 |
allow_methods=["*"],
|
| 27 |
allow_headers=["*"],
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# --- ROUTE PROJET 1 : ANTIBIODRAME ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@app.post("/api/chat")
|
| 32 |
+
async def chat_antibio(request: Request):
|
| 33 |
+
data = await request.json()
|
| 34 |
+
messages = data.get("messages", [])
|
| 35 |
try:
|
| 36 |
+
response = client_antibio.chat.completions.create(
|
| 37 |
+
model=MODEL_ID,
|
| 38 |
+
messages=messages,
|
| 39 |
+
temperature=0.7
|
| 40 |
+
)
|
| 41 |
+
return {"choices": [{"message": {"content": response.choices[0].message.content}}]}
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Erreur Antibio : {e}")
|
| 44 |
+
return {"choices": [{"message": {"content": "Erreur technique Albert (Antibio)."}}]}
|
| 45 |
+
|
| 46 |
+
# --- ROUTE PROJET 2 : SISMO ÉDUC ---
|
| 47 |
+
@app.post("/verify")
|
| 48 |
+
async def verify_seisme(request: Request):
|
| 49 |
+
data = await request.json()
|
| 50 |
+
# On récupère le texte brut ou les messages envoyés par l'app
|
| 51 |
+
user_input = data.get("userLatex", "")
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
# Ici on utilise la clé ALBERT_API_RISQUE sans prompt système imposé par le Python
|
| 55 |
+
response = client_sismo.chat.completions.create(
|
| 56 |
+
model=MODEL_ID,
|
| 57 |
+
messages=[
|
| 58 |
+
{"role": "user", "content": user_input}
|
| 59 |
+
],
|
| 60 |
+
temperature=0.1
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# On renvoie la réponse brute du modèle
|
| 64 |
+
return {"content": response.choices[0].message.content}
|
|
|
|
|
|
|
| 65 |
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
+
print(f"Erreur Sismo : {e}")
|
| 68 |
+
return {"error": "Albert (Sismo) ne répond pas."}
|
|
|
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 71 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|