Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,192 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
|
5 |
|
6 |
+
def load_model():
|
7 |
+
model_name = 'SantiagoMJ/Lama-3-8b-RETIE-SER-V2-30'
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
9 |
+
|
10 |
+
if torch.cuda.is_available():
|
11 |
+
bnb_config = BitsAndBytesConfig(
|
12 |
+
load_in_4bit=True,
|
13 |
+
bnb_4bit_quant_type="nf4",
|
14 |
+
bnb_4bit_compute_dtype=torch.float16,
|
15 |
+
bnb_4bit_use_double_quant=False,
|
16 |
+
)
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
18 |
+
model_name,
|
19 |
+
quantization_config=bnb_config,
|
20 |
+
device_map='auto'
|
21 |
+
)
|
22 |
+
else:
|
23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
24 |
+
model_name,
|
25 |
+
device_map='auto'
|
26 |
+
)
|
27 |
+
|
28 |
+
return model, tokenizer
|
29 |
|
30 |
+
def generate_response(message, history):
|
31 |
+
prompt = f"<s>[INST] {message} [/INST]"
|
32 |
+
result = pipe(prompt)
|
33 |
+
response = result[0]['generated_text'].replace(prompt, "").strip()
|
34 |
+
return response
|
35 |
|
36 |
+
# Definimos estilos CSS personalizados
|
37 |
+
css = """
|
38 |
+
#chat-container {
|
39 |
+
border-radius: 10px;
|
40 |
+
background-color: #ffffff;
|
41 |
+
padding: 20px;
|
42 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
43 |
+
}
|
|
|
44 |
|
45 |
+
#header {
|
46 |
+
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
|
47 |
+
padding: 20px;
|
48 |
+
border-radius: 10px;
|
49 |
+
margin-bottom: 20px;
|
50 |
+
border: 1px solid #e0e0e0;
|
51 |
+
}
|
52 |
|
53 |
+
#title {
|
54 |
+
text-align: center;
|
55 |
+
margin-bottom: 5px;
|
56 |
+
}
|
57 |
|
58 |
+
#subtitle {
|
59 |
+
text-align: center;
|
60 |
+
color: #666;
|
61 |
+
font-size: 0.9em;
|
62 |
+
}
|
63 |
|
64 |
+
#input-container {
|
65 |
+
background-color: white;
|
66 |
+
border-radius: 8px;
|
67 |
+
padding: 15px;
|
68 |
+
margin-top: 10px;
|
69 |
+
border: 1px solid #e0e0e0;
|
70 |
+
}
|
|
|
71 |
|
72 |
+
.message-box {
|
73 |
+
height: 500px !important;
|
74 |
+
overflow-y: auto;
|
75 |
+
padding: 20px;
|
76 |
+
background-color: white;
|
77 |
+
border-radius: 8px;
|
78 |
+
margin-bottom: 15px;
|
79 |
+
border: 1px solid #e0e0e0;
|
80 |
+
}
|
81 |
|
82 |
+
.bot-message {
|
83 |
+
background-color: #f8f9fa !important;
|
84 |
+
border: 1px solid #e9ecef;
|
85 |
+
}
|
86 |
|
87 |
+
.user-message {
|
88 |
+
background-color: #f8f9fa !important;
|
89 |
+
border: 1px solid #e9ecef;
|
90 |
+
}
|
91 |
+
|
92 |
+
.custom-button {
|
93 |
+
border: 1px solid #e0e0e0 !important;
|
94 |
+
background-color: white !important;
|
95 |
+
color: #666 !important;
|
96 |
+
transition: all 0.3s ease !important;
|
97 |
+
}
|
98 |
+
|
99 |
+
.custom-button:hover {
|
100 |
+
background-color: #f8f9fa !important;
|
101 |
+
border-color: #666 !important;
|
102 |
+
}
|
103 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
with gr.Blocks(css=css) as demo:
|
106 |
+
with gr.Column(elem_id="chat-container"):
|
107 |
+
# Header mejorado
|
108 |
+
with gr.Column(elem_id="header"):
|
109 |
+
gr.Markdown("""
|
110 |
+
<div id="title">
|
111 |
+
<h1 style="color: #2d3748; margin: 0; font-size: 2.2em; font-weight: 600;">NPC - RETIE</h1>
|
112 |
+
<h2 style="color: #718096; margin: 5px 0; font-size: 1.3em; font-weight: 400;">DE SERINGTEC</h2>
|
113 |
+
</div>
|
114 |
+
<div id="subtitle">
|
115 |
+
<p style="margin: 10px 0 0 0;">Asistente Virtual Especializado en Normatividad Eléctrica</p>
|
116 |
+
</div>
|
117 |
+
""")
|
118 |
+
|
119 |
+
# Chat Interface
|
120 |
+
chat_interface = gr.Chatbot(
|
121 |
+
[],
|
122 |
+
elem_id="chatbox",
|
123 |
+
height=500,
|
124 |
+
bubble_full_width=False,
|
125 |
+
avatar_images=("👤", "🤖"),
|
126 |
+
show_label=False,
|
127 |
+
container=True
|
128 |
+
)
|
129 |
+
|
130 |
+
# Status indicator
|
131 |
+
status = gr.Markdown("*Sistema listo para responder consultas*")
|
132 |
+
|
133 |
+
# Input Container
|
134 |
+
with gr.Column(elem_id="input-container"):
|
135 |
+
with gr.Row():
|
136 |
+
txt = gr.Textbox(
|
137 |
+
show_label=False,
|
138 |
+
placeholder="Escribe tu pregunta sobre normatividad eléctrica aquí...",
|
139 |
+
container=False,
|
140 |
+
scale=7
|
141 |
+
)
|
142 |
+
submit_btn = gr.Button("Enviar 📤", scale=1, variant="primary")
|
143 |
+
|
144 |
+
with gr.Row():
|
145 |
+
clear_btn = gr.Button("Limpiar Chat 🗑️", size="sm", elem_classes="custom-button")
|
146 |
+
example_btn = gr.Button("Ver Ejemplo 💡", size="sm", elem_classes="custom-button")
|
147 |
+
|
148 |
+
# Instructions
|
149 |
+
with gr.Accordion("ℹ️ Guía de Uso", open=False):
|
150 |
+
gr.Markdown("""
|
151 |
+
### Cómo usar este asistente:
|
152 |
+
1. **Escribe tu pregunta** relacionada con normatividad eléctrica
|
153 |
+
2. **Envía tu consulta** usando el botón 'Enviar' o presionando Enter
|
154 |
+
3. **Espera la respuesta** del asistente
|
155 |
+
4. **Revisa el historial** de la conversación en la ventana superior
|
156 |
+
|
157 |
+
### Tipos de consultas recomendadas:
|
158 |
+
- Preguntas sobre el RETIE
|
159 |
+
- Dudas sobre instalaciones eléctricas
|
160 |
+
- Consultas sobre normatividad
|
161 |
+
- Requerimientos técnicos
|
162 |
+
""")
|
163 |
+
|
164 |
+
def user(user_message, history):
|
165 |
+
return "", history + [[user_message, None]]
|
166 |
+
|
167 |
+
def bot(history):
|
168 |
+
status.value = "*🤔 Procesando tu consulta...*"
|
169 |
+
user_message = history[-1][0]
|
170 |
+
bot_response = generate_response(user_message, history)
|
171 |
+
history[-1][1] = bot_response
|
172 |
+
status.value = "*✅ Sistema listo para responder consultas*"
|
173 |
+
return history
|
174 |
+
|
175 |
+
def clear_history():
|
176 |
+
return None
|
177 |
+
|
178 |
+
def show_example():
|
179 |
+
return "¿Cuáles son los requisitos principales para la declaración de cumplimiento de una instalación eléctrica?"
|
180 |
+
|
181 |
+
# Event handlers
|
182 |
+
txt.submit(user, [txt, chat_interface], [txt, chat_interface], queue=False).then(
|
183 |
+
bot, chat_interface, chat_interface
|
184 |
+
)
|
185 |
+
submit_btn.click(user, [txt, chat_interface], [txt, chat_interface], queue=False).then(
|
186 |
+
bot, chat_interface, chat_interface
|
187 |
+
)
|
188 |
+
clear_btn.click(clear_history, None, chat_interface)
|
189 |
+
example_btn.click(show_example, None, txt)
|
190 |
|
191 |
+
# Lanzamos la interfaz
|
192 |
+
demo.launch(share=True)
|