Update app.py
Browse files
app.py
CHANGED
@@ -148,19 +148,101 @@ def chat(message, history):
|
|
148 |
except Exception as e:
|
149 |
return f"Error: {e}"
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
demo.launch()
|
166 |
|
|
|
148 |
except Exception as e:
|
149 |
return f"Error: {e}"
|
150 |
|
151 |
+
def process_file(file):
|
152 |
+
file_path = file.name
|
153 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
154 |
+
|
155 |
+
# Leer el contenido del archivo según su extensión
|
156 |
+
if file_extension in ['.txt', '.md', '.csv', '.html', '.css', '.js', '.py', '.json']:
|
157 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
158 |
+
content = f.read()
|
159 |
+
else:
|
160 |
+
content = f"Archivo subido: {os.path.basename(file_path)} (Este tipo de archivo no puede ser procesado para mostrar su contenido)"
|
161 |
+
|
162 |
+
return content
|
163 |
+
|
164 |
+
def chat(message, history, file=None):
|
165 |
+
try:
|
166 |
+
file_content = ""
|
167 |
+
if file is not None:
|
168 |
+
file_content = f"\n\nArchivo subido: {os.path.basename(file.name)}\n"
|
169 |
+
file_content += process_file(file)
|
170 |
+
|
171 |
+
# Combinar el mensaje del usuario con el contenido del archivo
|
172 |
+
combined_message = message
|
173 |
+
if file_content:
|
174 |
+
combined_message += f"\n\nContenido del archivo:\n{file_content}"
|
175 |
+
|
176 |
+
messages = [
|
177 |
+
{"role": "user", "parts": [system_prompt]},
|
178 |
+
*[{"role": "user", "parts": [msg[0]]} for msg in history],
|
179 |
+
{"role": "user", "parts": [combined_message]}
|
180 |
+
]
|
181 |
+
response = model.generate_content(messages)
|
182 |
+
return response.text
|
183 |
+
except Exception as e:
|
184 |
+
return f"Error: {e}"
|
185 |
+
|
186 |
+
with gr.Blocks(title="🤖Chucho Bot - CopyXpert Sales Assistant") as demo:
|
187 |
+
gr.Markdown("# 🤖Chucho Bot - CopyXpert Sales Assistant")
|
188 |
+
gr.Markdown("¡Hola! Soy Chucho Bot, tu asistente personal para el curso CopyXpert. ¿Cómo puedo ayudarte hoy?")
|
189 |
+
|
190 |
+
with gr.Row():
|
191 |
+
with gr.Column(scale=4):
|
192 |
+
chatbot = gr.Chatbot()
|
193 |
+
msg = gr.Textbox(
|
194 |
+
placeholder="Escribe tu pregunta aquí...",
|
195 |
+
container=False,
|
196 |
+
scale=7
|
197 |
+
)
|
198 |
+
|
199 |
+
with gr.Row():
|
200 |
+
submit = gr.Button("Enviar", variant="primary")
|
201 |
+
clear = gr.Button("Limpiar")
|
202 |
+
|
203 |
+
with gr.Column(scale=1):
|
204 |
+
file_upload = gr.File(
|
205 |
+
label="Sube un archivo (opcional)",
|
206 |
+
file_types=["text", "pdf", "docx", "csv", "xlsx", "image"],
|
207 |
+
type="file"
|
208 |
+
)
|
209 |
+
|
210 |
+
examples = gr.Examples(
|
211 |
+
examples=[
|
212 |
+
"¿Qué incluye el curso CopyXpert?",
|
213 |
+
"¿Cuál es el precio del curso?",
|
214 |
+
"¿Cómo puedo inscribirme?",
|
215 |
+
"¿Qué beneficios obtendré?",
|
216 |
+
"¿Cuál es la metodología del curso?",
|
217 |
+
"¿Necesito experiencia previa?"
|
218 |
+
],
|
219 |
+
inputs=msg
|
220 |
+
)
|
221 |
+
|
222 |
+
submit.click(
|
223 |
+
chat,
|
224 |
+
inputs=[msg, chatbot, file_upload],
|
225 |
+
outputs=chatbot,
|
226 |
+
queue=True
|
227 |
+
).then(
|
228 |
+
lambda: "",
|
229 |
+
None,
|
230 |
+
msg,
|
231 |
+
queue=False
|
232 |
+
)
|
233 |
+
|
234 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
235 |
+
msg.submit(
|
236 |
+
chat,
|
237 |
+
inputs=[msg, chatbot, file_upload],
|
238 |
+
outputs=chatbot,
|
239 |
+
queue=True
|
240 |
+
).then(
|
241 |
+
lambda: "",
|
242 |
+
None,
|
243 |
+
msg,
|
244 |
+
queue=False
|
245 |
+
)
|
246 |
|
247 |
demo.launch()
|
248 |
|