Spaces:
Sleeping
Sleeping
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from typing import List, Optional | |
| from mistralai import Mistral | |
| app = FastAPI(title="ROME NAF Pro API — Mistral") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| api_key = os.environ.get("IUflaExH9FzhGEJhbp6tOH5dKJs36ZAV", "") | |
| client = Mistral(api_key=api_key) if api_key else None | |
| # Modèle utilisé — changeable via variable d'env HF | |
| # Options : mistral-small-latest | mistral-medium-latest | mistral-large-latest | |
| MODEL = os.environ.get("MISTRAL_MODEL", "mistral-small-latest") | |
| class Message(BaseModel): | |
| role: str | |
| content: str | |
| class ClaudeRequest(BaseModel): | |
| messages: List[Message] | |
| max_tokens: Optional[int] = 1200 | |
| async def call_mistral(body: ClaudeRequest): | |
| """ | |
| Endpoint conserve le nom /api/claude pour ne pas modifier le frontend. | |
| Il appelle Mistral en coulisses. | |
| """ | |
| if not client: | |
| raise HTTPException( | |
| status_code=500, | |
| detail="MISTRAL_API_KEY manquante. Configurez-la dans les Secrets du Space HF." | |
| ) | |
| try: | |
| response = client.chat.complete( | |
| model=MODEL, | |
| max_tokens=body.max_tokens, | |
| messages=[{"role": m.role, "content": m.content} for m in body.messages] | |
| ) | |
| text = response.choices[0].message.content | |
| return {"content": [{"text": text}]} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def health(): | |
| return { | |
| "status": "ok", | |
| "provider": "Mistral AI", | |
| "model": MODEL, | |
| "api_key_configured": bool(api_key) | |
| } | |
| app.mount("/", StaticFiles(directory="static", html=True), name="static") | |