Spaces:
Sleeping
Sleeping
Commit ·
3bdd855
1
Parent(s): 1e4c7ab
websocket api
Browse files- Dockerfile +38 -0
- main.py +39 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🌐 Base image légère avec Python 3.12
|
| 2 |
+
FROM python:3.12-slim
|
| 3 |
+
|
| 4 |
+
# 🧱 Installer les dépendances système minimales
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
build-essential \
|
| 7 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 8 |
+
|
| 9 |
+
# 👤 Créer un utilisateur non-root recommandé pour Hugging Face
|
| 10 |
+
RUN useradd -m -u 1000 appuser
|
| 11 |
+
|
| 12 |
+
# 📁 Définir le répertoire de travail
|
| 13 |
+
WORKDIR /app
|
| 14 |
+
|
| 15 |
+
# 📦 Copier les dépendances
|
| 16 |
+
COPY requirements.txt .
|
| 17 |
+
|
| 18 |
+
# 🐍 Créer un environnement virtuel dédié
|
| 19 |
+
RUN python -m venv /opt/venv
|
| 20 |
+
ENV PATH="/opt/venv/bin:$PATH"
|
| 21 |
+
|
| 22 |
+
# 📥 Installer les paquets Python dans le venv
|
| 23 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 24 |
+
|
| 25 |
+
# 📄 Copier le reste du code source
|
| 26 |
+
COPY . .
|
| 27 |
+
|
| 28 |
+
# 🔐 Définir les permissions
|
| 29 |
+
RUN chown -R appuser:appuser /app
|
| 30 |
+
|
| 31 |
+
# 👤 Passer à l'utilisateur sécurisé
|
| 32 |
+
USER appuser
|
| 33 |
+
|
| 34 |
+
# 🔊 Exposer le port attendu par Hugging Face
|
| 35 |
+
EXPOSE 7860
|
| 36 |
+
|
| 37 |
+
# 🚀 Commande de démarrage de l'application FastAPI
|
| 38 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import httpx
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Autoriser l'accès depuis ton app mobile (optionnel selon déploiement)
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"], # à restreindre en prod
|
| 12 |
+
allow_credentials=True,
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
LANGSERVE_URL = os.getenv("LANGSERVE_URL", "https://thibautmodrin-vitizen-chat.hf.space/chat/invoke")
|
| 18 |
+
|
| 19 |
+
@app.websocket("/ws/chat")
|
| 20 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 21 |
+
await websocket.accept()
|
| 22 |
+
async with httpx.AsyncClient() as client:
|
| 23 |
+
while True:
|
| 24 |
+
try:
|
| 25 |
+
data = await websocket.receive_text()
|
| 26 |
+
request_body = {
|
| 27 |
+
"input": {"question": data},
|
| 28 |
+
"stream": False
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
response = await client.post(LANGSERVE_URL, json=request_body)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
json_data = response.json()
|
| 34 |
+
|
| 35 |
+
answer = json_data.get("output", {}).get("answer", "[no response]")
|
| 36 |
+
await websocket.send_text(answer)
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
await websocket.send_text(f"[error] {str(e)}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
httpx
|