Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def format_prompt(message, history, system_message):
|
| 8 |
-
"""
|
| 9 |
-
Formatea el prompt correctamente para el modelo Zephyr
|
| 10 |
-
"""
|
| 11 |
prompt = f"<|system|>\n{system_message}</s>\n"
|
| 12 |
for user_msg, assistant_msg in history:
|
| 13 |
prompt += f"<|user|>\n{user_msg}</s>\n"
|
|
@@ -15,168 +22,181 @@ def format_prompt(message, history, system_message):
|
|
| 15 |
prompt += f"<|user|>\n{message}</s>\n<|assistant|>\n"
|
| 16 |
return prompt
|
| 17 |
|
| 18 |
-
def respond(
|
| 19 |
-
|
| 20 |
-
history: list[tuple[str, str]],
|
| 21 |
-
system_message,
|
| 22 |
-
max_tokens,
|
| 23 |
-
temperature,
|
| 24 |
-
top_p,
|
| 25 |
-
):
|
| 26 |
-
"""
|
| 27 |
-
Funci贸n principal que genera las respuestas del chatbot
|
| 28 |
-
"""
|
| 29 |
try:
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
prompt = format_prompt(message, history, system_message)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
prompt,
|
| 37 |
-
max_new_tokens=max_tokens,
|
| 38 |
stream=True,
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
response += token
|
| 44 |
yield response
|
| 45 |
-
|
| 46 |
except Exception as e:
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
# CSS
|
| 51 |
custom_css = """
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
margin: 0;
|
| 57 |
-
padding: 20px;
|
| 58 |
}
|
| 59 |
|
| 60 |
-
/* Contenedor principal */
|
| 61 |
.gradio-container {
|
| 62 |
max-width: 900px;
|
| 63 |
-
margin:
|
|
|
|
|
|
|
| 64 |
background: white;
|
| 65 |
-
|
| 66 |
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 67 |
-
padding: 20px;
|
| 68 |
}
|
| 69 |
|
| 70 |
-
/* Cabecera */
|
| 71 |
.gradio-header {
|
| 72 |
text-align: center;
|
| 73 |
-
margin-bottom:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
|
| 76 |
-
/* 脕rea de chat */
|
| 77 |
.gradio-chatbot {
|
| 78 |
-
min-height:
|
| 79 |
border: 1px solid #e0e0e0;
|
| 80 |
-
border-radius:
|
| 81 |
padding: 15px;
|
| 82 |
-
background:
|
| 83 |
-
margin-bottom:
|
| 84 |
}
|
| 85 |
|
| 86 |
-
/* Input de texto */
|
| 87 |
.gradio-textbox textarea {
|
| 88 |
border-radius: 8px !important;
|
| 89 |
-
border: 1px solid #
|
| 90 |
-
padding: 12px !important;
|
| 91 |
-
font-size:
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
-
/* Bot贸n de enviar */
|
| 95 |
.gradio-button {
|
| 96 |
-
background: linear-gradient(
|
| 97 |
color: white !important;
|
| 98 |
border: none !important;
|
| 99 |
border-radius: 8px !important;
|
| 100 |
-
padding: 12px
|
| 101 |
-
font-weight:
|
| 102 |
transition: all 0.3s !important;
|
| 103 |
}
|
| 104 |
|
| 105 |
.gradio-button:hover {
|
| 106 |
transform: translateY(-2px) !important;
|
| 107 |
-
box-shadow: 0 4px
|
| 108 |
}
|
| 109 |
|
| 110 |
-
|
| 111 |
-
.gradio-slider {
|
| 112 |
margin: 15px 0 !important;
|
| 113 |
}
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
| 121 |
}
|
| 122 |
"""
|
| 123 |
|
| 124 |
-
#
|
| 125 |
demo = gr.ChatInterface(
|
| 126 |
fn=respond,
|
| 127 |
additional_inputs=[
|
| 128 |
gr.Textbox(
|
| 129 |
-
value="Eres un
|
| 130 |
-
label="
|
| 131 |
-
lines=3
|
|
|
|
| 132 |
),
|
| 133 |
gr.Slider(
|
| 134 |
-
minimum=
|
| 135 |
-
maximum=
|
| 136 |
-
value=
|
| 137 |
step=32,
|
| 138 |
-
label="
|
| 139 |
-
info="Controla
|
| 140 |
),
|
| 141 |
gr.Slider(
|
| 142 |
minimum=0.1,
|
| 143 |
-
maximum=1.
|
| 144 |
value=0.7,
|
| 145 |
-
step=0.
|
| 146 |
-
label="Temperatura",
|
| 147 |
-
info="Valores m谩s altos = respuestas m谩s creativas
|
| 148 |
),
|
| 149 |
gr.Slider(
|
| 150 |
minimum=0.1,
|
| 151 |
maximum=1.0,
|
| 152 |
value=0.9,
|
| 153 |
step=0.05,
|
| 154 |
-
label="Top-p",
|
| 155 |
-
info="Controla la diversidad de
|
| 156 |
),
|
| 157 |
],
|
| 158 |
css=custom_css,
|
| 159 |
-
title="
|
| 160 |
-
description="Chatbot desarrollado por Gerardo usando
|
| 161 |
-
theme="soft",
|
| 162 |
examples=[
|
| 163 |
-
["Hola, 驴
|
| 164 |
-
["Expl铆came
|
| 165 |
-
["
|
| 166 |
],
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
-
# Configuraci贸n
|
| 173 |
if __name__ == "__main__":
|
| 174 |
demo.launch(
|
| 175 |
server_name="0.0.0.0",
|
| 176 |
server_port=7860,
|
| 177 |
share=False,
|
|
|
|
| 178 |
favicon_path=None,
|
| 179 |
-
auth=None
|
| 180 |
-
auth_message=None,
|
| 181 |
-
prevent_thread_lock=False
|
| 182 |
)
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient, login
|
| 4 |
|
| 5 |
+
# Configuraci贸n inicial
|
| 6 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "tu_token_aqui") # Usa variable de entorno o reemplaza
|
| 7 |
+
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
| 8 |
+
|
| 9 |
+
# Autenticaci贸n
|
| 10 |
+
try:
|
| 11 |
+
login(token=HF_TOKEN)
|
| 12 |
+
client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN, timeout=60)
|
| 13 |
+
except Exception as auth_error:
|
| 14 |
+
print(f"Error de autenticaci贸n: {auth_error}")
|
| 15 |
|
| 16 |
def format_prompt(message, history, system_message):
|
| 17 |
+
"""Formatea el prompt seg煤n las especificaciones de Zephyr"""
|
|
|
|
|
|
|
| 18 |
prompt = f"<|system|>\n{system_message}</s>\n"
|
| 19 |
for user_msg, assistant_msg in history:
|
| 20 |
prompt += f"<|user|>\n{user_msg}</s>\n"
|
|
|
|
| 22 |
prompt += f"<|user|>\n{message}</s>\n<|assistant|>\n"
|
| 23 |
return prompt
|
| 24 |
|
| 25 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 26 |
+
"""Genera respuestas del chatbot con manejo robusto de errores"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
+
# Validaci贸n de entrada
|
| 29 |
+
if not message or len(message.strip()) == 0:
|
| 30 |
+
yield "Por favor, ingresa un mensaje v谩lido."
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
prompt = format_prompt(message, history, system_message)
|
| 34 |
|
| 35 |
+
# Configuraci贸n de generaci贸n
|
| 36 |
+
generation_params = {
|
| 37 |
+
"max_new_tokens": min(max_tokens, 1024), # Limita a 1024 tokens m谩ximo
|
| 38 |
+
"temperature": max(0.1, min(temperature, 1.0)),
|
| 39 |
+
"top_p": max(0.1, min(top_p, 1.0)),
|
| 40 |
+
"do_sample": True,
|
| 41 |
+
"truncate": 2048
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# Generaci贸n de respuesta
|
| 45 |
+
stream = client.text_generation(
|
| 46 |
prompt,
|
|
|
|
| 47 |
stream=True,
|
| 48 |
+
**generation_params
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
response = ""
|
| 52 |
+
for token in stream:
|
| 53 |
response += token
|
| 54 |
yield response
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
error_msg = f"Error en la generaci贸n: {str(e)}"
|
| 58 |
+
print(error_msg)
|
| 59 |
+
yield "馃敶 Lo siento, tuve un problema al procesar tu mensaje. Int茅ntalo de nuevo m谩s tarde."
|
| 60 |
|
| 61 |
+
# CSS mejorado
|
| 62 |
custom_css = """
|
| 63 |
+
:root {
|
| 64 |
+
--primary: #6e48aa;
|
| 65 |
+
--secondary: #9d50bb;
|
| 66 |
+
--accent: #4776E6;
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
|
|
|
|
| 69 |
.gradio-container {
|
| 70 |
max-width: 900px;
|
| 71 |
+
margin: 20px auto;
|
| 72 |
+
border-radius: 12px;
|
| 73 |
+
box-shadow: 0 6px 18px rgba(0,0,0,0.1);
|
| 74 |
background: white;
|
| 75 |
+
padding: 25px;
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
|
|
|
|
| 78 |
.gradio-header {
|
| 79 |
text-align: center;
|
| 80 |
+
margin-bottom: 25px;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
h1 {
|
| 84 |
+
background: linear-gradient(45deg, var(--primary), var(--secondary));
|
| 85 |
+
-webkit-background-clip: text;
|
| 86 |
+
background-clip: text;
|
| 87 |
+
color: transparent;
|
| 88 |
+
font-size: 2.2rem;
|
| 89 |
+
margin-bottom: 10px;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
.gradio-description {
|
| 93 |
+
color: #555;
|
| 94 |
+
font-size: 1rem;
|
| 95 |
}
|
| 96 |
|
|
|
|
| 97 |
.gradio-chatbot {
|
| 98 |
+
min-height: 450px;
|
| 99 |
border: 1px solid #e0e0e0;
|
| 100 |
+
border-radius: 10px;
|
| 101 |
padding: 15px;
|
| 102 |
+
background: #fafafa;
|
| 103 |
+
margin-bottom: 20px;
|
| 104 |
}
|
| 105 |
|
|
|
|
| 106 |
.gradio-textbox textarea {
|
| 107 |
border-radius: 8px !important;
|
| 108 |
+
border: 1px solid #ddd !important;
|
| 109 |
+
padding: 12px 15px !important;
|
| 110 |
+
font-size: 15px !important;
|
| 111 |
+
min-height: 100px !important;
|
| 112 |
}
|
| 113 |
|
|
|
|
| 114 |
.gradio-button {
|
| 115 |
+
background: linear-gradient(45deg, var(--primary), var(--secondary)) !important;
|
| 116 |
color: white !important;
|
| 117 |
border: none !important;
|
| 118 |
border-radius: 8px !important;
|
| 119 |
+
padding: 12px 28px !important;
|
| 120 |
+
font-weight: 500 !important;
|
| 121 |
transition: all 0.3s !important;
|
| 122 |
}
|
| 123 |
|
| 124 |
.gradio-button:hover {
|
| 125 |
transform: translateY(-2px) !important;
|
| 126 |
+
box-shadow: 0 4px 12px rgba(110, 72, 170, 0.3) !important;
|
| 127 |
}
|
| 128 |
|
| 129 |
+
.gradio-slider .wrap {
|
|
|
|
| 130 |
margin: 15px 0 !important;
|
| 131 |
}
|
| 132 |
|
| 133 |
+
.dark .gradio-container {
|
| 134 |
+
background: #1a1a1a;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.dark .gradio-chatbot {
|
| 138 |
+
background: #252525;
|
| 139 |
+
border-color: #444;
|
| 140 |
}
|
| 141 |
"""
|
| 142 |
|
| 143 |
+
# Configuraci贸n de la interfaz
|
| 144 |
demo = gr.ChatInterface(
|
| 145 |
fn=respond,
|
| 146 |
additional_inputs=[
|
| 147 |
gr.Textbox(
|
| 148 |
+
value="Eres ELISA, un asistente de IA 煤til, preciso y amable. Desarrollado por Gerardo.",
|
| 149 |
+
label="Configuraci贸n del Sistema",
|
| 150 |
+
lines=3,
|
| 151 |
+
max_lines=6
|
| 152 |
),
|
| 153 |
gr.Slider(
|
| 154 |
+
minimum=64,
|
| 155 |
+
maximum=1024,
|
| 156 |
+
value=256,
|
| 157 |
step=32,
|
| 158 |
+
label="Longitud de Respuesta (tokens)",
|
| 159 |
+
info="Controla cu谩n extensa ser谩 la respuesta"
|
| 160 |
),
|
| 161 |
gr.Slider(
|
| 162 |
minimum=0.1,
|
| 163 |
+
maximum=1.0,
|
| 164 |
value=0.7,
|
| 165 |
+
step=0.05,
|
| 166 |
+
label="Creatividad (Temperatura)",
|
| 167 |
+
info="Valores m谩s altos = respuestas m谩s creativas"
|
| 168 |
),
|
| 169 |
gr.Slider(
|
| 170 |
minimum=0.1,
|
| 171 |
maximum=1.0,
|
| 172 |
value=0.9,
|
| 173 |
step=0.05,
|
| 174 |
+
label="Enfoque (Top-p)",
|
| 175 |
+
info="Controla la diversidad de palabras"
|
| 176 |
),
|
| 177 |
],
|
| 178 |
css=custom_css,
|
| 179 |
+
title="馃 ELISA - Asistente de IA",
|
| 180 |
+
description="Chatbot avanzado desarrollado por Gerardo usando Hugging Face",
|
|
|
|
| 181 |
examples=[
|
| 182 |
+
["Hola, 驴qu茅 puedes hacer?"],
|
| 183 |
+
["Expl铆came el machine learning en t茅rminos simples"],
|
| 184 |
+
["Recomi茅ndame libros sobre IA"]
|
| 185 |
],
|
| 186 |
+
submit_btn="Enviar",
|
| 187 |
+
retry_btn="Reintentar",
|
| 188 |
+
undo_btn="Deshacer",
|
| 189 |
+
clear_btn="Limpiar",
|
| 190 |
+
theme="soft"
|
| 191 |
)
|
| 192 |
|
| 193 |
+
# Configuraci贸n de lanzamiento
|
| 194 |
if __name__ == "__main__":
|
| 195 |
demo.launch(
|
| 196 |
server_name="0.0.0.0",
|
| 197 |
server_port=7860,
|
| 198 |
share=False,
|
| 199 |
+
debug=True,
|
| 200 |
favicon_path=None,
|
| 201 |
+
auth=None
|
|
|
|
|
|
|
| 202 |
)
|