Spaces:
Running
on
Zero
Running
on
Zero
alex16052G
commited on
Commit
•
86e0b44
1
Parent(s):
a0b5579
app.py
Browse files
app.py
CHANGED
@@ -1,172 +1,192 @@
|
|
1 |
-
import spaces
|
2 |
-
import os
|
3 |
-
import json
|
4 |
-
import subprocess
|
5 |
-
from llama_cpp import Llama
|
6 |
-
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
7 |
-
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
8 |
-
from llama_cpp_agent.chat_history import BasicChatHistory
|
9 |
-
from llama_cpp_agent.chat_history.messages import Roles
|
10 |
import gradio as gr
|
11 |
-
from huggingface_hub import hf_hub_download
|
12 |
|
|
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
repo_id="bartowski/gemma-2-9b-it-GGUF",
|
18 |
-
filename="gemma-2-9b-it-Q5_K_M.gguf",
|
19 |
-
local_dir="./models"
|
20 |
-
)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
local_dir="./models",
|
32 |
-
token=huggingface_token
|
33 |
-
)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
36 |
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
history: list[tuple[str, str]],
|
44 |
-
model,
|
45 |
-
system_message,
|
46 |
-
max_tokens,
|
47 |
-
temperature,
|
48 |
top_p,
|
49 |
top_k,
|
50 |
-
|
51 |
):
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
)
|
65 |
-
llm_model = model
|
66 |
-
|
67 |
-
provider = LlamaCppPythonProvider(llm)
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
)
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
'content': msn[1]
|
94 |
-
}
|
95 |
-
messages.add_message(user)
|
96 |
-
messages.add_message(assistant)
|
97 |
-
|
98 |
-
stream = agent.get_chat_response(
|
99 |
-
message,
|
100 |
-
llm_sampling_settings=settings,
|
101 |
-
chat_history=messages,
|
102 |
-
returns_streaming_generator=True,
|
103 |
-
print_output=False
|
104 |
)
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
<a href="https://huggingface.co/google/gemma-2-27b-it" target="_blank">[27B it Model]</a>
|
114 |
-
<a href="https://huggingface.co/google/gemma-2-9b-it" target="_blank">[9B it Model]</a>
|
115 |
-
<a href="https://huggingface.co/google/gemma-2-2b-it" target="_blank">[2B it Model]</a>
|
116 |
-
<a href="https://huggingface.co/bartowski/gemma-2-27b-it-GGUF" target="_blank">[27B it Model GGUF]</a>
|
117 |
-
<a href="https://huggingface.co/bartowski/gemma-2-9b-it-GGUF" target="_blank">[9B it Model GGUF]</a>
|
118 |
-
<a href="https://huggingface.co/google/gemma-2-2b-it-GGUF" target="_blank">[2B it Model GGUF]</a>
|
119 |
-
</center></p>
|
120 |
-
"""
|
121 |
-
|
122 |
-
demo = gr.ChatInterface(
|
123 |
-
respond,
|
124 |
-
additional_inputs=[
|
125 |
-
gr.Dropdown([
|
126 |
-
'gemma-2-9b-it-Q5_K_M.gguf',
|
127 |
-
'gemma-2-27b-it-Q5_K_M.gguf',
|
128 |
-
'2b_it_v2.gguf'
|
129 |
-
],
|
130 |
-
value="2b_it_v2.gguf",
|
131 |
-
label="Model"
|
132 |
-
),
|
133 |
-
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
134 |
-
gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
|
135 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
136 |
-
gr.Slider(
|
137 |
-
minimum=0.1,
|
138 |
-
maximum=1.0,
|
139 |
-
value=0.95,
|
140 |
-
step=0.05,
|
141 |
-
label="Top-p",
|
142 |
-
),
|
143 |
-
gr.Slider(
|
144 |
-
minimum=0,
|
145 |
-
maximum=100,
|
146 |
-
value=40,
|
147 |
-
step=1,
|
148 |
-
label="Top-k",
|
149 |
-
),
|
150 |
-
gr.Slider(
|
151 |
-
minimum=0.0,
|
152 |
-
maximum=2.0,
|
153 |
-
value=1.1,
|
154 |
-
step=0.1,
|
155 |
-
label="Repetition penalty",
|
156 |
-
),
|
157 |
-
],
|
158 |
-
retry_btn="Retry",
|
159 |
-
undo_btn="Undo",
|
160 |
-
clear_btn="Clear",
|
161 |
-
submit_btn="Send",
|
162 |
-
title="Chat with Gemma 2 using llama.cpp",
|
163 |
-
description=description,
|
164 |
-
chatbot=gr.Chatbot(
|
165 |
-
scale=1,
|
166 |
-
likeable=False,
|
167 |
-
show_copy_button=True
|
168 |
)
|
169 |
-
)
|
170 |
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
+
import os
|
4 |
|
5 |
+
from huggingface_hub.file_download import http_get
|
6 |
+
from llama_cpp import Llama
|
7 |
|
8 |
+
SYSTEM_PROMPT = "Tú eres ABI, un asistente automático de habla española. Hablas con las personas y las ayudas."
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
def obtener_tokens_mensaje(modelo, rol, contenido):
|
11 |
+
contenido = f"{rol}\n{contenido}\n</s>"
|
12 |
+
contenido = contenido.encode("utf-8")
|
13 |
+
return modelo.tokenize(contenido, special=True)
|
|
|
14 |
|
15 |
+
def obtener_tokens_sistema(modelo):
|
16 |
+
mensaje_sistema = {"role": "system", "content": SYSTEM_PROMPT}
|
17 |
+
return obtener_tokens_mensaje(modelo, **mensaje_sistema)
|
|
|
|
|
|
|
18 |
|
19 |
+
def cargar_modelo(
|
20 |
+
directorio: str = ".",
|
21 |
+
nombre_modelo: str = "ecastera/eva-mistral-7b-spanish-GGUF",
|
22 |
+
url_modelo: str = "https://huggingface.co/ecastera/eva-mistral-7b-spanish-GGUF/resolve/main/Turdus-trained-20-int4.gguf"
|
23 |
+
):
|
24 |
+
ruta_modelo_final = os.path.join(directorio, nombre_modelo)
|
25 |
+
|
26 |
+
print("Descargando todos los archivos...")
|
27 |
+
if not os.path.exists(ruta_modelo_final):
|
28 |
+
with open(ruta_modelo_final, "wb") as f:
|
29 |
+
http_get(url_modelo, f)
|
30 |
+
os.chmod(ruta_modelo_final, 0o777)
|
31 |
+
print("¡Archivos descargados!")
|
32 |
+
|
33 |
+
modelo = Llama(
|
34 |
+
model_path=ruta_modelo_final,
|
35 |
+
n_ctx=2048
|
36 |
+
)
|
37 |
+
|
38 |
+
print("¡Modelo cargado!")
|
39 |
+
return modelo
|
40 |
|
41 |
+
MODELO = cargar_modelo()
|
42 |
|
43 |
+
def usuario(mensaje, historial):
|
44 |
+
nuevo_historial = historial + [[mensaje, None]]
|
45 |
+
return "", nuevo_historial
|
46 |
|
47 |
+
def bot(
|
48 |
+
historial,
|
49 |
+
prompt_sistema,
|
|
|
|
|
|
|
|
|
|
|
50 |
top_p,
|
51 |
top_k,
|
52 |
+
temp
|
53 |
):
|
54 |
+
modelo = MODELO
|
55 |
+
tokens = obtener_tokens_sistema(modelo)[:]
|
56 |
+
|
57 |
+
for mensaje_usuario, mensaje_bot in historial[:-1]:
|
58 |
+
tokens_mensaje = obtener_tokens_mensaje(modelo=modelo, rol="usuario", contenido=mensaje_usuario)
|
59 |
+
tokens.extend(tokens_mensaje)
|
60 |
+
if mensaje_bot:
|
61 |
+
tokens_mensaje = obtener_tokens_mensaje(modelo=modelo, rol="bot", contenido=mensaje_bot)
|
62 |
+
tokens.extend(tokens_mensaje)
|
63 |
+
|
64 |
+
ultimo_mensaje_usuario = historial[-1][0]
|
65 |
+
tokens_mensaje = obtener_tokens_mensaje(modelo=modelo, rol="usuario", contenido=ultimo_mensaje_usuario)
|
66 |
+
tokens.extend(tokens_mensaje)
|
67 |
+
|
68 |
+
tokens_rol = modelo.tokenize("bot\n".encode("utf-8"), special=True)
|
69 |
+
tokens.extend(tokens_rol)
|
70 |
+
generador = modelo.generate(
|
71 |
+
tokens,
|
72 |
+
top_k=top_k,
|
73 |
+
top_p=top_p,
|
74 |
+
temp=temp
|
75 |
+
)
|
76 |
|
77 |
+
texto_parcial = ""
|
78 |
+
for i, token in enumerate(generador):
|
79 |
+
if token == modelo.token_eos():
|
80 |
+
break
|
81 |
+
texto_parcial += modelo.detokenize([token]).decode("utf-8", "ignore")
|
82 |
+
historial[-1][1] = texto_parcial
|
83 |
+
yield historial
|
84 |
+
|
85 |
+
with gr.Blocks(
|
86 |
+
theme=gr.themes.Soft()
|
87 |
+
) as demo:
|
88 |
+
favicon = '<img src="" width="48px" style="display: inline">'
|
89 |
+
gr.Markdown(
|
90 |
+
f"""<h1><center>{favicon}Saiga Mistral 7B GGUF Q4_K</center></h1>
|
91 |
+
Esta es una demo de un modelo basado en Mistral que habla español
|
92 |
+
"""
|
93 |
+
)
|
94 |
+
with gr.Row():
|
95 |
+
with gr.Column(scale=5):
|
96 |
+
prompt_sistema = gr.Textbox(label="Prompt del sistema", placeholder="", value=SYSTEM_PROMPT, interactive=False)
|
97 |
+
chatbot = gr.Chatbot(label="Diálogo", height=400)
|
98 |
+
with gr.Column(min_width=80, scale=1):
|
99 |
+
with gr.Tab(label="Parámetros de generación"):
|
100 |
+
top_p = gr.Slider(
|
101 |
+
minimum=0.0,
|
102 |
+
maximum=1.0,
|
103 |
+
value=0.9,
|
104 |
+
step=0.05,
|
105 |
+
interactive=True,
|
106 |
+
label="Top-p",
|
107 |
+
)
|
108 |
+
top_k = gr.Slider(
|
109 |
+
minimum=10,
|
110 |
+
maximum=100,
|
111 |
+
value=30,
|
112 |
+
step=5,
|
113 |
+
interactive=True,
|
114 |
+
label="Top-k",
|
115 |
+
)
|
116 |
+
temp = gr.Slider(
|
117 |
+
minimum=0.0,
|
118 |
+
maximum=2.0,
|
119 |
+
value=0.01,
|
120 |
+
step=0.01,
|
121 |
+
interactive=True,
|
122 |
+
label="Temperatura"
|
123 |
+
)
|
124 |
+
with gr.Row():
|
125 |
+
with gr.Column():
|
126 |
+
msg = gr.Textbox(
|
127 |
+
label="Enviar mensaje",
|
128 |
+
placeholder="Enviar mensaje",
|
129 |
+
show_label=False,
|
130 |
+
)
|
131 |
+
with gr.Column():
|
132 |
+
with gr.Row():
|
133 |
+
submit = gr.Button("Enviar")
|
134 |
+
stop = gr.Button("Detener")
|
135 |
+
clear = gr.Button("Limpiar")
|
136 |
+
with gr.Row():
|
137 |
+
gr.Markdown(
|
138 |
+
"""ADVERTENCIA: El modelo puede generar textos que sean incorrectos fácticamente o inapropiados éticamente. No nos hacemos responsables de esto."""
|
139 |
)
|
|
|
|
|
|
|
140 |
|
141 |
+
# Presionando Enter
|
142 |
+
evento_enviar = msg.submit(
|
143 |
+
fn=usuario,
|
144 |
+
inputs=[msg, chatbot],
|
145 |
+
outputs=[msg, chatbot],
|
146 |
+
queue=False,
|
147 |
+
).success(
|
148 |
+
fn=bot,
|
149 |
+
inputs=[
|
150 |
+
chatbot,
|
151 |
+
prompt_sistema,
|
152 |
+
top_p,
|
153 |
+
top_k,
|
154 |
+
temp
|
155 |
+
],
|
156 |
+
outputs=chatbot,
|
157 |
+
queue=True,
|
158 |
)
|
159 |
+
|
160 |
+
# Presionando el botón
|
161 |
+
evento_click_enviar = submit.click(
|
162 |
+
fn=usuario,
|
163 |
+
inputs=[msg, chatbot],
|
164 |
+
outputs=[msg, chatbot],
|
165 |
+
queue=False,
|
166 |
+
).success(
|
167 |
+
fn=bot,
|
168 |
+
inputs=[
|
169 |
+
chatbot,
|
170 |
+
prompt_sistema,
|
171 |
+
top_p,
|
172 |
+
top_k,
|
173 |
+
temp
|
174 |
+
],
|
175 |
+
outputs=chatbot,
|
176 |
+
queue=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
)
|
178 |
+
|
179 |
+
# Detener generación
|
180 |
+
stop.click(
|
181 |
+
fn=None,
|
182 |
+
inputs=None,
|
183 |
+
outputs=None,
|
184 |
+
cancels=[evento_enviar, evento_click_enviar],
|
185 |
+
queue=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
)
|
|
|
187 |
|
188 |
+
# Limpiar historial
|
189 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
190 |
+
|
191 |
+
demo.queue(max_size=128)
|
192 |
+
demo.launch()
|