Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def respond(
|
| 7 |
message,
|
| 8 |
history: list[tuple[str, str]],
|
|
@@ -11,83 +23,160 @@ def respond(
|
|
| 11 |
temperature,
|
| 12 |
top_p,
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
messages,
|
| 28 |
-
max_tokens=max_tokens,
|
| 29 |
-
stream=True,
|
| 30 |
-
temperature=temperature,
|
| 31 |
-
top_p=top_p,
|
| 32 |
-
):
|
| 33 |
-
token = message.choices[0].delta.content
|
| 34 |
-
|
| 35 |
-
response += token
|
| 36 |
-
yield response
|
| 37 |
-
|
| 38 |
-
# Define custom CSS
|
| 39 |
custom_css = """
|
| 40 |
-
/*
|
| 41 |
body {
|
| 42 |
font-family: Arial, sans-serif;
|
| 43 |
-
background-color:
|
|
|
|
|
|
|
| 44 |
}
|
|
|
|
|
|
|
| 45 |
.gradio-container {
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
border-radius: 10px;
|
|
|
|
| 48 |
padding: 20px;
|
| 49 |
-
background-color: #ffffff;
|
| 50 |
-
box-shadow:0 0 12px 12px solid black;
|
| 51 |
}
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
|
|
|
|
|
|
| 57 |
.gradio-button {
|
| 58 |
-
background
|
| 59 |
-
color: white;
|
| 60 |
-
border: none;
|
| 61 |
-
border-radius:
|
| 62 |
-
padding:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
padding: 10px;
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
}
|
| 70 |
"""
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
demo = gr.ChatInterface(
|
| 75 |
fn=respond,
|
| 76 |
additional_inputs=[
|
| 77 |
-
gr.Textbox(
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
gr.Slider(
|
| 81 |
minimum=0.1,
|
| 82 |
maximum=1.0,
|
| 83 |
-
value=0.
|
| 84 |
step=0.05,
|
| 85 |
-
label="Top-p
|
|
|
|
| 86 |
),
|
| 87 |
],
|
| 88 |
css=custom_css,
|
| 89 |
-
title="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
)
|
| 91 |
|
|
|
|
| 92 |
if __name__ == "__main__":
|
| 93 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Inicializar el cliente de Hugging Face
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 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"
|
| 14 |
+
prompt += f"<|assistant|>\n{assistant_msg}</s>\n"
|
| 15 |
+
prompt += f"<|user|>\n{message}</s>\n<|assistant|>\n"
|
| 16 |
+
return prompt
|
| 17 |
+
|
| 18 |
def respond(
|
| 19 |
message,
|
| 20 |
history: list[tuple[str, str]],
|
|
|
|
| 23 |
temperature,
|
| 24 |
top_p,
|
| 25 |
):
|
| 26 |
+
"""
|
| 27 |
+
Funci贸n principal que genera las respuestas del chatbot
|
| 28 |
+
"""
|
| 29 |
+
try:
|
| 30 |
+
# Formatear el prompt adecuadamente
|
| 31 |
+
prompt = format_prompt(message, history, system_message)
|
| 32 |
+
|
| 33 |
+
# Generar la respuesta
|
| 34 |
+
response = ""
|
| 35 |
+
for token in client.text_generation(
|
| 36 |
+
prompt,
|
| 37 |
+
max_new_tokens=max_tokens,
|
| 38 |
+
stream=True,
|
| 39 |
+
temperature=temperature,
|
| 40 |
+
top_p=top_p,
|
| 41 |
+
truncate=1000,
|
| 42 |
+
):
|
| 43 |
+
response += token
|
| 44 |
+
yield response
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Error: {e}")
|
| 48 |
+
yield "Lo siento, ocurri贸 un error al procesar tu mensaje. Por favor, int茅ntalo de nuevo."
|
| 49 |
|
| 50 |
+
# CSS personalizado para la interfaz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
custom_css = """
|
| 52 |
+
/* Estilos generales */
|
| 53 |
body {
|
| 54 |
font-family: Arial, sans-serif;
|
| 55 |
+
background-color: #f5f5f5;
|
| 56 |
+
margin: 0;
|
| 57 |
+
padding: 20px;
|
| 58 |
}
|
| 59 |
+
|
| 60 |
+
/* Contenedor principal */
|
| 61 |
.gradio-container {
|
| 62 |
+
max-width: 900px;
|
| 63 |
+
margin: 0 auto;
|
| 64 |
+
background: white;
|
| 65 |
border-radius: 10px;
|
| 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: 20px;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
/* 脕rea de chat */
|
| 77 |
+
.gradio-chatbot {
|
| 78 |
+
min-height: 400px;
|
| 79 |
+
border: 1px solid #e0e0e0;
|
| 80 |
+
border-radius: 8px;
|
| 81 |
+
padding: 15px;
|
| 82 |
+
background: white;
|
| 83 |
+
margin-bottom: 15px;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/* Input de texto */
|
| 87 |
+
.gradio-textbox textarea {
|
| 88 |
+
border-radius: 8px !important;
|
| 89 |
+
border: 1px solid #e0e0e0 !important;
|
| 90 |
+
padding: 12px !important;
|
| 91 |
+
font-size: 14px !important;
|
| 92 |
}
|
| 93 |
+
|
| 94 |
+
/* Bot贸n de enviar */
|
| 95 |
.gradio-button {
|
| 96 |
+
background: linear-gradient(90deg, #0f0653, #160979) !important;
|
| 97 |
+
color: white !important;
|
| 98 |
+
border: none !important;
|
| 99 |
+
border-radius: 8px !important;
|
| 100 |
+
padding: 12px 24px !important;
|
| 101 |
+
font-weight: bold !important;
|
| 102 |
+
transition: all 0.3s !important;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
.gradio-button:hover {
|
| 106 |
+
transform: translateY(-2px) !important;
|
| 107 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2) !important;
|
| 108 |
}
|
| 109 |
+
|
| 110 |
+
/* Sliders y controles */
|
| 111 |
+
.gradio-slider {
|
| 112 |
+
margin: 15px 0 !important;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
/* Mensajes del sistema */
|
| 116 |
+
.gradio-markdown {
|
| 117 |
padding: 10px;
|
| 118 |
+
background: #f8f9fa;
|
| 119 |
+
border-radius: 8px;
|
| 120 |
+
margin-bottom: 15px;
|
| 121 |
}
|
| 122 |
"""
|
| 123 |
|
| 124 |
+
# Crear la interfaz de chat con Gradio
|
|
|
|
| 125 |
demo = gr.ChatInterface(
|
| 126 |
fn=respond,
|
| 127 |
additional_inputs=[
|
| 128 |
+
gr.Textbox(
|
| 129 |
+
value="Eres un chatbot llamado ELISA. Eres amable, servicial y profesional. Desarrollado por Gerardo.",
|
| 130 |
+
label="Mensaje del Sistema",
|
| 131 |
+
lines=3
|
| 132 |
+
),
|
| 133 |
+
gr.Slider(
|
| 134 |
+
minimum=32,
|
| 135 |
+
maximum=2048,
|
| 136 |
+
value=512,
|
| 137 |
+
step=32,
|
| 138 |
+
label="Tokens m谩ximos",
|
| 139 |
+
info="Controla la longitud m谩xima de la respuesta"
|
| 140 |
+
),
|
| 141 |
+
gr.Slider(
|
| 142 |
+
minimum=0.1,
|
| 143 |
+
maximum=1.5,
|
| 144 |
+
value=0.7,
|
| 145 |
+
step=0.1,
|
| 146 |
+
label="Temperatura",
|
| 147 |
+
info="Valores m谩s altos = respuestas m谩s creativas pero menos precisas"
|
| 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 las respuestas"
|
| 156 |
),
|
| 157 |
],
|
| 158 |
css=custom_css,
|
| 159 |
+
title="馃馃挰 ELISA - Modelo de Inteligencia Artificial",
|
| 160 |
+
description="Chatbot desarrollado por Gerardo usando Zephyr-7b-beta de Hugging Face",
|
| 161 |
+
theme="soft",
|
| 162 |
+
examples=[
|
| 163 |
+
["Hola, 驴c贸mo est谩s?"],
|
| 164 |
+
["Expl铆came qu茅 es la inteligencia artificial"],
|
| 165 |
+
["Dame ideas para un proyecto de IA"]
|
| 166 |
+
],
|
| 167 |
+
retry_btn=None,
|
| 168 |
+
undo_btn=None,
|
| 169 |
+
clear_btn="Limpiar"
|
| 170 |
)
|
| 171 |
|
| 172 |
+
# Configuraci贸n adicional para el lanzamiento
|
| 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 |
+
)
|