Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import sqlite3
|
| 3 |
import torch
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
-
from fastapi_poe import PoeBot,
|
| 6 |
from fastapi_poe.types import QueryRequest
|
| 7 |
from sse_starlette.sse import ServerSentEvent
|
| 8 |
|
|
@@ -18,7 +19,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 18 |
device_map="auto"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# --- BASE DE DONNÉES
|
| 22 |
DB_NAME = os.path.join(os.path.expanduser("~"), "charlotte_api.db")
|
| 23 |
|
| 24 |
def init_db():
|
|
@@ -32,13 +33,13 @@ init_db()
|
|
| 32 |
# --- CLASSE DU BOT POE ---
|
| 33 |
class CharlottePoeBot(PoeBot):
|
| 34 |
async def get_response(self, request: QueryRequest):
|
| 35 |
-
# Récupérer le dernier message envoyé par l'utilisateur
|
| 36 |
last_message = request.query[-1].content
|
| 37 |
|
| 38 |
# Préparation des tokens
|
| 39 |
inputs = tokenizer(last_message, return_tensors="pt", truncation=True, max_length=128).to(model.device)
|
| 40 |
|
| 41 |
-
# Génération du texte
|
| 42 |
with torch.no_grad():
|
| 43 |
outputs = model.generate(
|
| 44 |
**inputs,
|
|
@@ -51,17 +52,20 @@ class CharlottePoeBot(PoeBot):
|
|
| 51 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
response_text = full_text[len(last_message):].strip()
|
| 53 |
|
| 54 |
-
# Envoi de la réponse sous forme de flux (streaming)
|
| 55 |
yield ServerSentEvent(event="text", data=response_text)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# --- LANCEMENT DU SERVEUR ---
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
#
|
| 60 |
-
api_key = os.environ.get("POE_API_KEY", "")
|
| 61 |
-
|
| 62 |
-
# Hugging Face impose le port 7860, on utilise donc 7860 par défaut
|
| 63 |
port_to_use = int(os.environ.get("PORT", 7860))
|
| 64 |
-
print(f"--> Démarrage
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
|
|
|
|
| 1 |
import os
|
| 2 |
import sqlite3
|
| 3 |
import torch
|
| 4 |
+
import uvicorn
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
from fastapi_poe import PoeBot, make_app
|
| 7 |
from fastapi_poe.types import QueryRequest
|
| 8 |
from sse_starlette.sse import ServerSentEvent
|
| 9 |
|
|
|
|
| 19 |
device_map="auto"
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# --- BASE DE DONNÉES ---
|
| 23 |
DB_NAME = os.path.join(os.path.expanduser("~"), "charlotte_api.db")
|
| 24 |
|
| 25 |
def init_db():
|
|
|
|
| 33 |
# --- CLASSE DU BOT POE ---
|
| 34 |
class CharlottePoeBot(PoeBot):
|
| 35 |
async def get_response(self, request: QueryRequest):
|
| 36 |
+
# Récupérer le dernier message envoyé par l'utilisateur
|
| 37 |
last_message = request.query[-1].content
|
| 38 |
|
| 39 |
# Préparation des tokens
|
| 40 |
inputs = tokenizer(last_message, return_tensors="pt", truncation=True, max_length=128).to(model.device)
|
| 41 |
|
| 42 |
+
# Génération du texte
|
| 43 |
with torch.no_grad():
|
| 44 |
outputs = model.generate(
|
| 45 |
**inputs,
|
|
|
|
| 52 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
response_text = full_text[len(last_message):].strip()
|
| 54 |
|
| 55 |
+
# Envoi de la réponse sous forme de flux (streaming)
|
| 56 |
yield ServerSentEvent(event="text", data=response_text)
|
| 57 |
|
| 58 |
+
# --- CREATION DE L'APPLICATION ---
|
| 59 |
+
bot = CharlottePoeBot()
|
| 60 |
+
# On crée l'application FastAPI compatible Poe
|
| 61 |
+
app = make_app(bot, allow_disabling_authentication=True)
|
| 62 |
+
|
| 63 |
# --- LANCEMENT DU SERVEUR ---
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
# Hugging Face impose le port 7860
|
|
|
|
|
|
|
|
|
|
| 66 |
port_to_use = int(os.environ.get("PORT", 7860))
|
| 67 |
+
print(f"--> Démarrage d'Uvicorn sur le port {port_to_use}...")
|
| 68 |
|
| 69 |
+
# On lance le serveur web sur l'adresse 0.0.0.0 pour qu'il soit accessible de l'extérieur
|
| 70 |
+
uvicorn.run(app, host="0.0.0.0", port=port_to_use)
|
| 71 |
|