Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,35 +31,23 @@ def obtener_extractos(pregunta):
|
|
| 31 |
return [(doc.page_content, doc.metadata.get("url", "URL no disponible")) for doc in docs_relevantes]
|
| 32 |
|
| 33 |
def extract_unique_citations_paragraph(response):
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
y devuelve un párrafo de texto con formato legible, separado por puntos.
|
| 37 |
-
"""
|
| 38 |
-
seen_urls = set()
|
| 39 |
-
fragments = []
|
| 40 |
|
| 41 |
for item in getattr(response, "output", []):
|
|
|
|
| 42 |
if getattr(item, "type", None) == "message":
|
| 43 |
for block in getattr(item, "content", []) or []:
|
| 44 |
if getattr(block, "type", None) == "output_text":
|
| 45 |
for ann in getattr(block, "annotations", []) or []:
|
| 46 |
if getattr(ann, "type", None) == "url_citation":
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
else:
|
| 55 |
-
fragments.append(f"{url}")
|
| 56 |
-
|
| 57 |
-
# Unir todas las frases en un párrafo separado por puntos
|
| 58 |
-
paragraph = ". ".join(fragments)
|
| 59 |
-
if paragraph and not paragraph.endswith("."):
|
| 60 |
-
paragraph += "."
|
| 61 |
-
|
| 62 |
-
return paragraph
|
| 63 |
|
| 64 |
|
| 65 |
|
|
@@ -162,8 +150,10 @@ def respond(message, history: list[tuple[str, str]], domain_table):
|
|
| 162 |
elif event.type == "response.completed":
|
| 163 |
response_stream = stream.get_final_response()
|
| 164 |
citations = extract_unique_citations_paragraph(response_stream)
|
| 165 |
-
response += "\n\
|
| 166 |
-
|
|
|
|
|
|
|
| 167 |
break
|
| 168 |
|
| 169 |
|
|
@@ -184,10 +174,11 @@ def add_domain(domains, new_domain):
|
|
| 184 |
return domains, "" # limpia el textbox
|
| 185 |
|
| 186 |
with gr.Blocks() as demo:
|
| 187 |
-
gr.Markdown("### 🧠
|
|
|
|
| 188 |
|
| 189 |
with gr.Row():
|
| 190 |
-
new_domain = gr.Textbox(label="
|
| 191 |
add_button = gr.Button("➕ Añadir")
|
| 192 |
|
| 193 |
domain_table = gr.Dataframe(
|
|
@@ -203,8 +194,6 @@ with gr.Blocks() as demo:
|
|
| 203 |
fn=respond,
|
| 204 |
chatbot=custom_chatbot,
|
| 205 |
additional_inputs=[domain_table],
|
| 206 |
-
title="🧠 Buscador especializado en investigación médica",
|
| 207 |
-
description="Agrega tus dominios arriba para limitar la búsqueda"
|
| 208 |
)
|
| 209 |
|
| 210 |
add_button.click(add_domain, [domain_table, new_domain], [domain_table, new_domain])
|
|
|
|
| 31 |
return [(doc.page_content, doc.metadata.get("url", "URL no disponible")) for doc in docs_relevantes]
|
| 32 |
|
| 33 |
def extract_unique_citations_paragraph(response):
|
| 34 |
+
"""Extrae todas las URL y títulos de un objeto Response."""
|
| 35 |
+
citations = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
for item in getattr(response, "output", []):
|
| 38 |
+
# Solo buscamos en mensajes del asistente
|
| 39 |
if getattr(item, "type", None) == "message":
|
| 40 |
for block in getattr(item, "content", []) or []:
|
| 41 |
if getattr(block, "type", None) == "output_text":
|
| 42 |
for ann in getattr(block, "annotations", []) or []:
|
| 43 |
if getattr(ann, "type", None) == "url_citation":
|
| 44 |
+
citations.append({
|
| 45 |
+
"title": getattr(ann, "title", None),
|
| 46 |
+
"url": getattr(ann, "url", None),
|
| 47 |
+
"start": getattr(ann, "start_index", None),
|
| 48 |
+
"end": getattr(ann, "end_index", None)
|
| 49 |
+
})
|
| 50 |
+
return citations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
|
|
|
|
| 150 |
elif event.type == "response.completed":
|
| 151 |
response_stream = stream.get_final_response()
|
| 152 |
citations = extract_unique_citations_paragraph(response_stream)
|
| 153 |
+
response += "\n\n🔗 Fuentes citadas: \n"
|
| 154 |
+
for c in citations:
|
| 155 |
+
response += f"• {c['title'] or 'Sin título'} → {c['url']}")
|
| 156 |
+
yield response
|
| 157 |
break
|
| 158 |
|
| 159 |
|
|
|
|
| 174 |
return domains, "" # limpia el textbox
|
| 175 |
|
| 176 |
with gr.Blocks() as demo:
|
| 177 |
+
gr.Markdown("### 🧠 Chat Médico AI")
|
| 178 |
+
gr.Markdown("Este es un chatbot especializdo en la búsqueda de información médica con fuentes verídicas. Puedes colocar que busque sólo en dominios específicos.")
|
| 179 |
|
| 180 |
with gr.Row():
|
| 181 |
+
new_domain = gr.Textbox(label="Búscar en dominios específicos (opcional)")
|
| 182 |
add_button = gr.Button("➕ Añadir")
|
| 183 |
|
| 184 |
domain_table = gr.Dataframe(
|
|
|
|
| 194 |
fn=respond,
|
| 195 |
chatbot=custom_chatbot,
|
| 196 |
additional_inputs=[domain_table],
|
|
|
|
|
|
|
| 197 |
)
|
| 198 |
|
| 199 |
add_button.click(add_domain, [domain_table, new_domain], [domain_table, new_domain])
|