Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import secrets
|
| 3 |
import string
|
| 4 |
import torch
|
|
@@ -6,8 +7,6 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 6 |
|
| 7 |
# --- CONFIGURATION DU MODÈLE ---
|
| 8 |
MODEL_ID = "Finisha-F-scratch/Charlotte-amity"
|
| 9 |
-
|
| 10 |
-
print("Chargement du tokenizer et du modèle...")
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
MODEL_ID,
|
|
@@ -15,22 +14,60 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 15 |
device_map="auto"
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# ---
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return True
|
|
|
|
| 23 |
return False
|
| 24 |
|
| 25 |
-
# --- FONCTION API
|
| 26 |
-
def
|
| 27 |
"""
|
| 28 |
-
|
| 29 |
"""
|
| 30 |
-
if not
|
| 31 |
-
return "Erreur: Clé API invalide
|
| 32 |
|
| 33 |
-
#
|
| 34 |
inputs = tokenizer(message, return_tensors="pt", truncation=True, max_length=128).to(model.device)
|
| 35 |
|
| 36 |
with torch.no_grad():
|
|
@@ -38,31 +75,40 @@ def predict(api_key, message):
|
|
| 38 |
**inputs,
|
| 39 |
max_new_tokens=100,
|
| 40 |
do_sample=True,
|
| 41 |
-
temperature=0.
|
| 42 |
pad_token_id=tokenizer.eos_token_id
|
| 43 |
)
|
| 44 |
|
| 45 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
-
# On
|
| 47 |
response = full_text[len(message):].strip()
|
| 48 |
return response
|
| 49 |
|
| 50 |
-
# --- INTERFACE
|
| 51 |
-
with gr.Blocks() as demo:
|
| 52 |
-
gr.Markdown("# 🌸 Charlotte-Amity API
|
| 53 |
-
|
| 54 |
-
key_input = gr.Textbox(label="Clé API")
|
| 55 |
-
msg_input = gr.Textbox(label="Message")
|
| 56 |
-
output = gr.Textbox(label="Réponse")
|
| 57 |
-
|
| 58 |
-
submit_btn = gr.Button("Envoyer")
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
outputs=
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import sqlite3
|
| 3 |
import secrets
|
| 4 |
import string
|
| 5 |
import torch
|
|
|
|
| 7 |
|
| 8 |
# --- CONFIGURATION DU MODÈLE ---
|
| 9 |
MODEL_ID = "Finisha-F-scratch/Charlotte-amity"
|
|
|
|
|
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_ID,
|
|
|
|
| 14 |
device_map="auto"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# --- GESTION DE LA BASE DE DONNÉES (SQLITE) ---
|
| 18 |
+
DB_NAME = "charlotte_api.db"
|
| 19 |
+
|
| 20 |
+
def init_db():
|
| 21 |
+
conn = sqlite3.connect(DB_NAME)
|
| 22 |
+
c = conn.cursor()
|
| 23 |
+
# Table pour stocker les clés et le nombre de requêtes
|
| 24 |
+
c.execute('''CREATE TABLE IF NOT EXISTS api_keys
|
| 25 |
+
(key TEXT PRIMARY KEY, requests_count INTEGER)''')
|
| 26 |
+
conn.commit()
|
| 27 |
+
conn.close()
|
| 28 |
+
|
| 29 |
+
init_db()
|
| 30 |
+
|
| 31 |
+
def create_key():
|
| 32 |
+
"""Génère une clé tn-xxxx0000 et l'enregistre"""
|
| 33 |
+
letters = ''.join(secrets.choice(string.ascii_lowercase) for _ in range(4))
|
| 34 |
+
digits = ''.join(secrets.choice(string.digits) for _ in range(4))
|
| 35 |
+
new_key = f"tn-{letters}{digits}"
|
| 36 |
+
|
| 37 |
+
conn = sqlite3.connect(DB_NAME)
|
| 38 |
+
c = conn.cursor()
|
| 39 |
+
try:
|
| 40 |
+
c.execute("INSERT INTO api_keys (key, requests_count) VALUES (?, ?)", (new_key, 0))
|
| 41 |
+
conn.commit()
|
| 42 |
+
return new_key
|
| 43 |
+
except:
|
| 44 |
+
return "Erreur lors de la génération. Réessayez."
|
| 45 |
+
finally:
|
| 46 |
+
conn.close()
|
| 47 |
+
|
| 48 |
+
def validate_and_log(key):
|
| 49 |
+
"""Vérifie la clé et incrémente le compteur"""
|
| 50 |
+
conn = sqlite3.connect(DB_NAME)
|
| 51 |
+
c = conn.cursor()
|
| 52 |
+
c.execute("SELECT requests_count FROM api_keys WHERE key=?", (key,))
|
| 53 |
+
result = c.fetchone()
|
| 54 |
+
if result is not None:
|
| 55 |
+
c.execute("UPDATE api_keys SET requests_count = requests_count + 1 WHERE key=?", (key,))
|
| 56 |
+
conn.commit()
|
| 57 |
+
conn.close()
|
| 58 |
return True
|
| 59 |
+
conn.close()
|
| 60 |
return False
|
| 61 |
|
| 62 |
+
# --- FONCTION D'INFÉRENCE (L'API) ---
|
| 63 |
+
def charlotte_api(api_key, message):
|
| 64 |
"""
|
| 65 |
+
Point d'entrée principal pour les appels externes.
|
| 66 |
"""
|
| 67 |
+
if not validate_and_log(api_key):
|
| 68 |
+
return "❌ Erreur : Clé API 'tn-' invalide ou non trouvée."
|
| 69 |
|
| 70 |
+
# Limitation stricte au contexte de 128 tokens
|
| 71 |
inputs = tokenizer(message, return_tensors="pt", truncation=True, max_length=128).to(model.device)
|
| 72 |
|
| 73 |
with torch.no_grad():
|
|
|
|
| 75 |
**inputs,
|
| 76 |
max_new_tokens=100,
|
| 77 |
do_sample=True,
|
| 78 |
+
temperature=0.8,
|
| 79 |
pad_token_id=tokenizer.eos_token_id
|
| 80 |
)
|
| 81 |
|
| 82 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 83 |
+
# On renvoie uniquement la réponse générée
|
| 84 |
response = full_text[len(message):].strip()
|
| 85 |
return response
|
| 86 |
|
| 87 |
+
# --- INTERFACE GRADIO ---
|
| 88 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 89 |
+
gr.Markdown("# 🌸 Charlotte-Amity : Système API Personnel")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
with gr.Tab("🔑 Gérer mes clés"):
|
| 92 |
+
gr.Markdown("Cliquez ci-dessous pour générer une clé d'accès unique.")
|
| 93 |
+
key_output = gr.Textbox(label="Votre nouvelle clé API", interactive=False)
|
| 94 |
+
generate_btn = gr.Button("Générer ma clé tn-")
|
| 95 |
+
generate_btn.click(create_key, outputs=key_output)
|
| 96 |
+
gr.Info("Note: Gardez précieusement votre clé pour vos appels externes.")
|
| 97 |
+
|
| 98 |
+
with gr.Tab("💬 Test Rapide"):
|
| 99 |
+
api_input = gr.Textbox(label="Clé API (tn-...)")
|
| 100 |
+
msg_input = gr.Textbox(label="Votre message")
|
| 101 |
+
text_output = gr.Textbox(label="Réponse de Charlotte")
|
| 102 |
+
run_btn = gr.Button("Envoyer")
|
| 103 |
+
run_btn.click(charlotte_api, inputs=[api_input, msg_input], outputs=text_output)
|
| 104 |
+
|
| 105 |
+
# L'EXPOSITION DE L'API POUR L'EXTÉRIEUR
|
| 106 |
+
# Cette ligne permet d'appeler la fonction via l'URL du Space
|
| 107 |
+
api_interface = gr.Interface(
|
| 108 |
+
fn=charlotte_api,
|
| 109 |
+
inputs=[gr.Textbox(label="api_key"), gr.Textbox(label="message")],
|
| 110 |
+
outputs="text",
|
| 111 |
+
api_name="chat" # C'est le nom à utiliser dans le client Python
|
| 112 |
)
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|