Spaces:
Sleeping
Sleeping
Download app.py from Midya-Music/Kla: direct link, hf CLI and curl.
- Browser
- Download file 2.46 kB
-
https://huggingface.co/spaces/Midya-Music/Kla/resolve/c06be9eff40306eb069e235d86764bb2244d09e2/app.py
- Command line
-
hf download hf://spaces/Midya-Music/Kla@c06be9eff40306eb069e235d86764bb2244d09e2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Midya-Music/Kla/resolve/c06be9eff40306eb069e235d86764bb2244d09e2/app.py
2.46 kB
| import os | |
| import sqlite3 | |
| import torch | |
| import uvicorn | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from fastapi_poe import PoeBot, make_app | |
| from fastapi_poe.types import QueryRequest | |
| from sse_starlette.sse import ServerSentEvent | |
| # --- CONFIGURATION DU MODÈLE --- | |
| MODEL_ID = "Finisha-F-scratch/Charlotte-amity" | |
| print("--> Chargement du tokenizer...") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | |
| print("--> Chargement du modèle (cette étape peut prendre du temps)...") | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, | |
| device_map="auto" | |
| ) | |
| # --- BASE DE DONNÉES --- | |
| DB_NAME = os.path.join(os.path.expanduser("~"), "charlotte_api.db") | |
| def init_db(): | |
| conn = sqlite3.connect(DB_NAME) | |
| c = conn.cursor() | |
| c.execute('''CREATE TABLE IF NOT EXISTS api_keys (key TEXT PRIMARY KEY, requests_count INTEGER)''') | |
| conn.commit() | |
| conn.close() | |
| init_db() | |
| # --- CLASSE DU BOT POE --- | |
| class CharlottePoeBot(PoeBot): | |
| async def get_response(self, request: QueryRequest): | |
| # Récupérer le dernier message envoyé par l'utilisateur | |
| last_message = request.query[-1].content | |
| # Préparation des tokens | |
| inputs = tokenizer(last_message, return_tensors="pt", truncation=True, max_length=128).to(model.device) | |
| # Génération du texte | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=100, | |
| do_sample=True, | |
| temperature=0.6, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| full_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| response_text = full_text[len(last_message):].strip() | |
| # Envoi de la réponse sous forme de flux (streaming) | |
| yield ServerSentEvent(event="text", data=response_text) | |
| # --- CREATION DE L'APPLICATION --- | |
| bot = CharlottePoeBot() | |
| # Correction ici : on enlève l'argument obsolète | |
| app = make_app(bot) | |
| # --- LANCEMENT DU SERVEUR --- | |
| if __name__ == "__main__": | |
| # Hugging Face impose le port 7860 | |
| port_to_use = int(os.environ.get("PORT", 7860)) | |
| print(f"--> Démarrage d'Uvicorn sur le port {port_to_use}...") | |
| # On lance le serveur web sur l'adresse 0.0.0.0 pour qu'il soit accessible de l'extérieur | |
| uvicorn.run(app, host="0.0.0.0", port=port_to_use) | |