Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,34 +5,24 @@ import requests
|
|
5 |
from datetime import datetime
|
6 |
from supabase import create_client, Client
|
7 |
|
8 |
-
# --- Credenciales y Configuraci贸n ---
|
9 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
10 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
11 |
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
|
12 |
SYSTEM_MESSAGE = os.environ.get("System_Prompt")
|
13 |
|
14 |
-
# --- Constantes del Modelo ---
|
15 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
16 |
-
MODEL_NAME = "meta-llama/llama-4-
|
17 |
MAX_TOKENS = 4096
|
18 |
TEMPERATURE = 0.7
|
19 |
TOP_P = 0.95
|
20 |
|
21 |
-
|
22 |
def persist_data(session_data, user_identifier=None):
|
23 |
-
"""
|
24 |
-
Guarda el historial de la conversaci贸n en Supabase con un formato claro de 'rol' y 'contenido'.
|
25 |
-
"""
|
26 |
if not SUPABASE_URL or not SUPABASE_KEY:
|
27 |
-
print("Advertencia: Las credenciales de Supabase no est谩n configuradas.")
|
28 |
return
|
29 |
|
30 |
try:
|
31 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
32 |
-
|
33 |
-
# --- NUEVO: Transformar los datos para mayor claridad ---
|
34 |
-
# Convertimos la lista de tuplas [("user_msg", "bot_msg")]
|
35 |
-
# a una lista de diccionarios [{"role": "user", ...}, {"role": "assistant", ...}]
|
36 |
formatted_messages = []
|
37 |
for user_msg, assistant_msg in session_data:
|
38 |
if user_msg:
|
@@ -43,21 +33,16 @@ def persist_data(session_data, user_identifier=None):
|
|
43 |
data_to_insert = {
|
44 |
"timestamp": datetime.now().isoformat(),
|
45 |
"user_id": user_identifier,
|
46 |
-
"messages": formatted_messages
|
47 |
}
|
48 |
|
49 |
table_name = "conversations"
|
50 |
-
|
51 |
-
print(f"Conversaci贸n guardada en Supabase exitosamente con formato mejorado.")
|
52 |
|
53 |
except Exception as e:
|
54 |
-
|
55 |
-
|
56 |
|
57 |
def respond(message, history: list[tuple[str, str]]):
|
58 |
-
"""
|
59 |
-
Genera una respuesta del LLM y la devuelve en streaming.
|
60 |
-
"""
|
61 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
62 |
|
63 |
for user_msg, assistant_msg in history:
|
@@ -123,8 +108,6 @@ def respond(message, history: list[tuple[str, str]]):
|
|
123 |
print(f"Error en la solicitud a la API de Groq: {e}")
|
124 |
yield "Lo siento, ocurri贸 un error de conexi贸n al procesar tu solicitud."
|
125 |
|
126 |
-
|
127 |
-
# --- Interfaz de Gradio ---
|
128 |
demo = gr.ChatInterface(
|
129 |
respond,
|
130 |
examples=[["隆Bienvenido a Tu Aliado Momentum!"],
|
|
|
5 |
from datetime import datetime
|
6 |
from supabase import create_client, Client
|
7 |
|
|
|
8 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
9 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
10 |
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
|
11 |
SYSTEM_MESSAGE = os.environ.get("System_Prompt")
|
12 |
|
|
|
13 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
14 |
+
MODEL_NAME = "meta-llama/llama-4-maverick-17b-128e-instruct"
|
15 |
MAX_TOKENS = 4096
|
16 |
TEMPERATURE = 0.7
|
17 |
TOP_P = 0.95
|
18 |
|
|
|
19 |
def persist_data(session_data, user_identifier=None):
|
|
|
|
|
|
|
20 |
if not SUPABASE_URL or not SUPABASE_KEY:
|
|
|
21 |
return
|
22 |
|
23 |
try:
|
24 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
25 |
+
|
|
|
|
|
|
|
26 |
formatted_messages = []
|
27 |
for user_msg, assistant_msg in session_data:
|
28 |
if user_msg:
|
|
|
33 |
data_to_insert = {
|
34 |
"timestamp": datetime.now().isoformat(),
|
35 |
"user_id": user_identifier,
|
36 |
+
"messages": formatted_messages
|
37 |
}
|
38 |
|
39 |
table_name = "conversations"
|
40 |
+
supabase.table(table_name).insert(data_to_insert).execute()
|
|
|
41 |
|
42 |
except Exception as e:
|
43 |
+
pass
|
|
|
44 |
|
45 |
def respond(message, history: list[tuple[str, str]]):
|
|
|
|
|
|
|
46 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
47 |
|
48 |
for user_msg, assistant_msg in history:
|
|
|
108 |
print(f"Error en la solicitud a la API de Groq: {e}")
|
109 |
yield "Lo siento, ocurri贸 un error de conexi贸n al procesar tu solicitud."
|
110 |
|
|
|
|
|
111 |
demo = gr.ChatInterface(
|
112 |
respond,
|
113 |
examples=[["隆Bienvenido a Tu Aliado Momentum!"],
|