Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import spaces
|
2 |
import os
|
3 |
import random
|
4 |
-
import time
|
5 |
from llama_cpp import Llama
|
6 |
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
7 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
@@ -57,8 +56,10 @@ def generar_descripcion_personalidad(prompt):
|
|
57 |
if llm is None:
|
58 |
llm = Llama(
|
59 |
model_path="models/gemma-2-2b-it-abliterated-Q4_K_M.gguf",
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
)
|
63 |
|
64 |
proveedor = LlamaCppPythonProvider(llm)
|
@@ -76,10 +77,10 @@ def generar_descripcion_personalidad(prompt):
|
|
76 |
respuesta = agente.get_chat_response(prompt, llm_sampling_settings=configuracion)
|
77 |
return respuesta
|
78 |
|
79 |
-
@spaces.GPU()
|
80 |
def responder(
|
81 |
mensaje,
|
82 |
-
historial,
|
83 |
mensaje_sistema,
|
84 |
max_tokens,
|
85 |
temperatura,
|
@@ -92,15 +93,19 @@ def responder(
|
|
92 |
if llm is None:
|
93 |
llm = Llama(
|
94 |
model_path="models/gemma-2-2b-it-abliterated-Q4_K_M.gguf",
|
95 |
-
|
96 |
-
|
|
|
|
|
97 |
)
|
98 |
|
|
|
|
|
99 |
proveedor = LlamaCppPythonProvider(llm)
|
100 |
agente = LlamaCppAgent(
|
101 |
proveedor,
|
102 |
system_prompt=f"{mensaje_sistema}",
|
103 |
-
predefined_messages_formatter_type=
|
104 |
debug_output=True
|
105 |
)
|
106 |
|
@@ -125,83 +130,63 @@ def responder(
|
|
125 |
mensajes.add_message(usuario)
|
126 |
mensajes.add_message(asistente)
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
yield salida
|
141 |
-
|
142 |
-
if not salida:
|
143 |
-
yield "El modelo no generó ninguna respuesta. Por favor, intenta de nuevo."
|
144 |
-
except Exception as e:
|
145 |
-
yield f"Error al generar respuesta: {str(e)}"
|
146 |
-
|
147 |
-
def actualizar_mensaje_sistema(personalidad):
|
148 |
-
return f"Eres un personaje con la siguiente personalidad: {personalidad}. Actúa y responde de acuerdo a estas características en todo momento."
|
149 |
|
150 |
-
def
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
166 |
-
top_k = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
|
167 |
-
penalizacion = gr.Slider(minimum=1.0, maximum=2.0, value=1.1, step=0.1, label="Penalización por repetición")
|
168 |
-
|
169 |
-
generar_btn = gr.Button("Generar Nueva Personalidad")
|
170 |
-
|
171 |
-
def on_generar_personalidad():
|
172 |
-
try:
|
173 |
-
personalidad = generar_personalidad()
|
174 |
-
nuevo_mensaje_sistema = actualizar_mensaje_sistema(personalidad)
|
175 |
-
return personalidad, nuevo_mensaje_sistema, [], "Personalidad generada con éxito"
|
176 |
-
except Exception as e:
|
177 |
-
return "", "Error al generar personalidad", [], f"Error: {str(e)}"
|
178 |
-
|
179 |
-
generar_btn.click(
|
180 |
-
on_generar_personalidad,
|
181 |
-
outputs=[personalidad_output, sistema_msg, chatbot, estado]
|
182 |
-
)
|
183 |
-
|
184 |
-
def on_submit(mensaje, historial, sistema_msg, max_tokens, temperatura, top_p, top_k, penalizacion):
|
185 |
-
historial = historial + [(mensaje, "")]
|
186 |
-
yield historial, "", "Generando respuesta..."
|
187 |
-
|
188 |
-
for respuesta in responder(mensaje, historial, sistema_msg, max_tokens, temperatura, top_p, top_k, penalizacion):
|
189 |
-
historial[-1] = (mensaje, respuesta)
|
190 |
-
yield historial, "", "Generando respuesta..."
|
191 |
-
|
192 |
-
yield historial, "", "Listo para chatear"
|
193 |
-
|
194 |
-
msg.submit(
|
195 |
-
on_submit,
|
196 |
-
[msg, chatbot, sistema_msg, max_tokens, temperatura, top_p, top_k, penalizacion],
|
197 |
-
[chatbot, msg, estado]
|
198 |
-
)
|
199 |
-
|
200 |
-
clear.click(lambda: ([], "", "Chat limpiado"), outputs=[chatbot, msg, estado])
|
201 |
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
if __name__ == "__main__":
|
205 |
-
demo = interfaz_chat()
|
206 |
-
demo.queue()
|
207 |
demo.launch()
|
|
|
1 |
import spaces
|
2 |
import os
|
3 |
import random
|
|
|
4 |
from llama_cpp import Llama
|
5 |
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
6 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
|
|
56 |
if llm is None:
|
57 |
llm = Llama(
|
58 |
model_path="models/gemma-2-2b-it-abliterated-Q4_K_M.gguf",
|
59 |
+
flash_attn=True,
|
60 |
+
n_gpu_layers=81,
|
61 |
+
n_batch=1024,
|
62 |
+
n_ctx=8192,
|
63 |
)
|
64 |
|
65 |
proveedor = LlamaCppPythonProvider(llm)
|
|
|
77 |
respuesta = agente.get_chat_response(prompt, llm_sampling_settings=configuracion)
|
78 |
return respuesta
|
79 |
|
80 |
+
@spaces.GPU(duration=120)
|
81 |
def responder(
|
82 |
mensaje,
|
83 |
+
historial: list[tuple[str, str]],
|
84 |
mensaje_sistema,
|
85 |
max_tokens,
|
86 |
temperatura,
|
|
|
93 |
if llm is None:
|
94 |
llm = Llama(
|
95 |
model_path="models/gemma-2-2b-it-abliterated-Q4_K_M.gguf",
|
96 |
+
flash_attn=True,
|
97 |
+
n_gpu_layers=81,
|
98 |
+
n_batch=1024,
|
99 |
+
n_ctx=8192,
|
100 |
)
|
101 |
|
102 |
+
plantilla_chat = MessagesFormatterType.GEMMA_2
|
103 |
+
|
104 |
proveedor = LlamaCppPythonProvider(llm)
|
105 |
agente = LlamaCppAgent(
|
106 |
proveedor,
|
107 |
system_prompt=f"{mensaje_sistema}",
|
108 |
+
predefined_messages_formatter_type=plantilla_chat,
|
109 |
debug_output=True
|
110 |
)
|
111 |
|
|
|
130 |
mensajes.add_message(usuario)
|
131 |
mensajes.add_message(asistente)
|
132 |
|
133 |
+
flujo = agente.get_chat_response(
|
134 |
+
mensaje,
|
135 |
+
llm_sampling_settings=configuracion,
|
136 |
+
chat_history=mensajes,
|
137 |
+
returns_streaming_generator=True,
|
138 |
+
print_output=False
|
139 |
+
)
|
140 |
+
|
141 |
+
salida = ""
|
142 |
+
for fragmento in flujo:
|
143 |
+
salida += fragmento
|
144 |
+
yield salida
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
+
def chat_with_personality(message, history):
|
147 |
+
if not history:
|
148 |
+
# Primera interacción, generamos la personalidad
|
149 |
+
personalidad = generar_personalidad()
|
150 |
+
mensaje_sistema = f"Eres un personaje con la siguiente personalidad: {personalidad}. Actúa y responde de acuerdo a estas características en todo momento."
|
151 |
+
history.append(("Sistema", f"Se ha generado una nueva personalidad: {personalidad}"))
|
152 |
+
else:
|
153 |
+
mensaje_sistema = history[0][1].split(": ", 1)[1]
|
154 |
+
|
155 |
+
# Configuración por defecto
|
156 |
+
max_tokens = 2048
|
157 |
+
temperatura = 0.7
|
158 |
+
top_p = 0.95
|
159 |
+
top_k = 40
|
160 |
+
penalizacion_repeticion = 1.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
+
for response in responder(message, history[1:], mensaje_sistema, max_tokens, temperatura, top_p, top_k, penalizacion_repeticion):
|
163 |
+
yield history + [(message, response)]
|
164 |
+
|
165 |
+
descripcion = """<p align="center">Chat de Rol Play con Gemma 2B Abliterated usando llama.cpp</p>
|
166 |
+
<p>Este chat genera automáticamente una personalidad al inicio de la conversación y luego responde de acuerdo a esa personalidad.</p>
|
167 |
+
<p><center>
|
168 |
+
<a href="https://huggingface.co/google/gemma-2-2b-it" target="_blank">[Modelo Gemma 2B it]</a>
|
169 |
+
<a href="https://huggingface.co/google/gemma-2-2b-it-GGUF" target="_blank">[Modelo Gemma 2B it GGUF]</a>
|
170 |
+
</center></p>"""
|
171 |
+
|
172 |
+
demo = gr.ChatInterface(
|
173 |
+
chat_with_personality,
|
174 |
+
chatbot=gr.Chatbot(height=500),
|
175 |
+
textbox=gr.Textbox(placeholder="Escribe tu mensaje aquí...", container=False, scale=7),
|
176 |
+
title="Chat de Rol Play con Gemma 2B",
|
177 |
+
description=descripcion,
|
178 |
+
theme="soft",
|
179 |
+
examples=[
|
180 |
+
"Hola, ¿cómo estás?",
|
181 |
+
"¿Puedes contarme sobre tu día típico?",
|
182 |
+
"¿Cuál es tu opinión sobre la tecnología actual?",
|
183 |
+
"¿Tienes algún hobby interesante?"
|
184 |
+
],
|
185 |
+
cache_examples=False,
|
186 |
+
retry_btn="Reintentar",
|
187 |
+
undo_btn="Deshacer",
|
188 |
+
clear_btn="Limpiar"
|
189 |
+
)
|
190 |
|
191 |
if __name__ == "__main__":
|
|
|
|
|
192 |
demo.launch()
|