Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,15 +8,19 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
8 |
from langchain.memory import ConversationBufferMemory
|
9 |
|
10 |
# Configuraci贸n del modelo de lenguaje
|
11 |
-
MODEL_NAME = "mistralai/Mistral-7B-Instruct"
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
|
14 |
print("馃攧 Cargando modelo de lenguaje...")
|
15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
16 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
memory = ConversationBufferMemory(
|
20 |
|
21 |
# Cargar modelo de la colmena
|
22 |
modelo_path = "modelo_colmena.pkl"
|
@@ -37,7 +41,7 @@ def obtener_datos_colmena():
|
|
37 |
if respuesta.status_code == 200:
|
38 |
datos = respuesta.json()
|
39 |
if "data" in datos and isinstance(datos["data"], list) and datos["data"]:
|
40 |
-
return datos["data"][-1] #
|
41 |
return {"error": "No hay datos recientes en Node-RED."}
|
42 |
else:
|
43 |
return {"error": f"Error en la API: {respuesta.status_code}"}
|
@@ -62,7 +66,7 @@ def filtrar_datos_por_pregunta(mensaje, datos):
|
|
62 |
estado = "ENCENDIDO" if int(datos['ver_ultrasonido']) == 1 else "APAGADO"
|
63 |
return f"馃攰 Ultrasonido: {estado}."
|
64 |
else:
|
65 |
-
return "馃 No entiendo la pregunta.
|
66 |
|
67 |
def conversar_con_colmena(mensaje):
|
68 |
"""Genera una respuesta combinando el modelo de lenguaje con los datos de la colmena."""
|
@@ -72,11 +76,16 @@ def conversar_con_colmena(mensaje):
|
|
72 |
|
73 |
datos_relevantes = filtrar_datos_por_pregunta(mensaje.lower(), datos)
|
74 |
|
75 |
-
contexto = f"Datos actuales de la colmena: {datos_relevantes}\nUsuario: {mensaje}\nColmena:"
|
76 |
inputs = tokenizer(contexto, return_tensors="pt").to(device)
|
77 |
|
78 |
with torch.no_grad():
|
79 |
-
output = model.generate(
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
respuesta = tokenizer.decode(output[0], skip_special_tokens=True)
|
82 |
memory.save_context({"input": mensaje}, {"output": respuesta})
|
@@ -93,3 +102,4 @@ iface = gr.Interface(
|
|
93 |
|
94 |
iface.launch()
|
95 |
|
|
|
|
8 |
from langchain.memory import ConversationBufferMemory
|
9 |
|
10 |
# Configuraci贸n del modelo de lenguaje
|
11 |
+
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.3"
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
|
14 |
print("馃攧 Cargando modelo de lenguaje...")
|
15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
17 |
+
MODEL_NAME,
|
18 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
19 |
+
device_map="auto"
|
20 |
+
).to(device)
|
21 |
|
22 |
+
# Memoria conversacional
|
23 |
+
memory = ConversationBufferMemory()
|
24 |
|
25 |
# Cargar modelo de la colmena
|
26 |
modelo_path = "modelo_colmena.pkl"
|
|
|
41 |
if respuesta.status_code == 200:
|
42 |
datos = respuesta.json()
|
43 |
if "data" in datos and isinstance(datos["data"], list) and datos["data"]:
|
44 |
+
return datos["data"][-1] # 脷ltimo registro
|
45 |
return {"error": "No hay datos recientes en Node-RED."}
|
46 |
else:
|
47 |
return {"error": f"Error en la API: {respuesta.status_code}"}
|
|
|
66 |
estado = "ENCENDIDO" if int(datos['ver_ultrasonido']) == 1 else "APAGADO"
|
67 |
return f"馃攰 Ultrasonido: {estado}."
|
68 |
else:
|
69 |
+
return "馃 No entiendo la pregunta. Pregunta sobre temperatura, humedad, CO2, ventilador, calefactor o ultrasonido."
|
70 |
|
71 |
def conversar_con_colmena(mensaje):
|
72 |
"""Genera una respuesta combinando el modelo de lenguaje con los datos de la colmena."""
|
|
|
76 |
|
77 |
datos_relevantes = filtrar_datos_por_pregunta(mensaje.lower(), datos)
|
78 |
|
79 |
+
contexto = f"Datos actuales de la colmena: {datos_relevantes}\nUsuario: {mensaje}\nColmena:"
|
80 |
inputs = tokenizer(contexto, return_tensors="pt").to(device)
|
81 |
|
82 |
with torch.no_grad():
|
83 |
+
output = model.generate(
|
84 |
+
**inputs,
|
85 |
+
max_length=200,
|
86 |
+
do_sample=True, # Permite respuestas m谩s naturales
|
87 |
+
top_p=0.9
|
88 |
+
)
|
89 |
|
90 |
respuesta = tokenizer.decode(output[0], skip_special_tokens=True)
|
91 |
memory.save_context({"input": mensaje}, {"output": respuesta})
|
|
|
102 |
|
103 |
iface.launch()
|
104 |
|
105 |
+
|