import re from pyvis.network import Network def extract_key_terms(text: str): """ A naive approach to extract key terms by matching capitalized words. """ return re.findall(r"\b[A-Z][a-zA-Z]+\b", text) def create_medical_graph(query: str, docs: list) -> str: """ Builds an interactive PyVis network: - A central "QUERY" node. - One node per retrieved document. - Sub-nodes for extracted key terms. Returns the HTML content of the generated graph. """ net = Network(height="600px", width="100%", directed=False) net.add_node("QUERY", label=f"Query: {query}", color="red", shape="star") for i, doc in enumerate(docs): doc_id = f"Doc_{i}" net.add_node(doc_id, label=f"Abstract {i+1}", color="blue") net.add_edge("QUERY", doc_id) terms = extract_key_terms(doc) for term in set(terms): term_id = f"{doc_id}_{term}" net.add_node(term_id, label=term, color="green") net.add_edge(doc_id, term_id) # Generate the HTML content directly without unsupported keyword arguments. html_content = net.generate_html(notebook=False) return html_content