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virus",
|
| 9 |
+
"genome_size": 29.9,
|
| 10 |
+
"family": "Coronaviridae",
|
| 11 |
+
"metadata": {"discovered": "2019", "origin": "Wuhan, China"}
|
| 12 |
+
},
|
| 13 |
+
"Spike Protein": {
|
| 14 |
+
"type": "Protein",
|
| 15 |
+
"function": "Viral entry",
|
| 16 |
+
"receptor": "ACE2",
|
| 17 |
+
"metadata": {"key_mutations": ["D614G", "N501Y", "E484K"]}
|
| 18 |
+
},
|
| 19 |
+
"ACE2": {
|
| 20 |
+
"type": "Receptor",
|
| 21 |
+
"location": "Cell membrane",
|
| 22 |
+
"function": "Viral entry receptor",
|
| 23 |
+
"metadata": {"tissue_expression": ["Lung", "Heart", "Kidney"]}
|
| 24 |
+
},
|
| 25 |
+
"Omicron": {
|
| 26 |
+
"type": "Variant",
|
| 27 |
+
"lineage": "BA.1",
|
| 28 |
+
"mutations": ["30+ spike mutations"],
|
| 29 |
+
"metadata": {"transmissibility": "High", "severity": "Lower"}
|
| 30 |
+
},
|
| 31 |
+
"mRNA Vaccine": {
|
| 32 |
+
"type": "Therapy",
|
| 33 |
+
"mechanism": "Induced immunity",
|
| 34 |
+
"efficacy": "~95% (original strain)",
|
| 35 |
+
"metadata": {"examples": ["Pfizer-BioNTech", "Moderna"]}
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
SAMPLE_EDGES = [
|
| 40 |
+
{"from": "SARS-CoV-2", "to": "Spike Protein", "relationship": "encodes", "confidence": 1.0},
|
| 41 |
+
{"from": "Spike Protein", "to": "ACE2", "relationship": "binds_to", "confidence": 0.95},
|
| 42 |
+
{"from": "SARS-CoV-2", "to": "Omicron", "relationship": "evolves_to", "confidence": 0.90},
|
| 43 |
+
{"from": "mRNA Vaccine", "to": "Spike Protein", "relationship": "targets", "confidence": 0.98},
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
INTENT_TYPES = ["Factual", "Causal", "Comparative", "Predictive", "Exploratory"]
|
| 47 |
+
|
| 48 |
+
# Query processing simulation
|
| 49 |
+
def process_query(query_text, intent_type, use_quantum):
|
| 50 |
+
"""Process a query and return results"""
|
| 51 |
+
results = {
|
| 52 |
+
"query": query_text,
|
| 53 |
+
"intent": intent_type,
|
| 54 |
+
"quantum_optimized": use_quantum,
|
| 55 |
+
"timestamp": datetime.now().isoformat(),
|
| 56 |
+
"nodes_searched": 0,
|
| 57 |
+
"edges_traversed": 0,
|
| 58 |
+
"results": []
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# Simple keyword matching simulation
|
| 62 |
+
query_lower = query_text.lower()
|
| 63 |
+
for node_name, node_data in SAMPLE_NODES.items():
|
| 64 |
+
if any(keyword in query_lower for keyword in node_name.lower().split()):
|
| 65 |
+
results["results"].append({
|
| 66 |
+
"node": node_name,
|
| 67 |
+
"data": node_data,
|
| 68 |
+
"relevance": 0.85 + (0.1 if use_quantum else 0)
|
| 69 |
+
})
|
| 70 |
+
results["nodes_searched"] += 1
|
| 71 |
+
|
| 72 |
+
# Add related edges
|
| 73 |
+
for edge in SAMPLE_EDGES:
|
| 74 |
+
if any(node in [r["node"] for r in results["results"]]
|
| 75 |
+
for node in [edge["from"], edge["to"]]):
|
| 76 |
+
results["edges_traversed"] += 1
|
| 77 |
+
|
| 78 |
+
# Quantum optimization bonus
|
| 79 |
+
if use_quantum and results["results"]:
|
| 80 |
+
results["optimization"] = {
|
| 81 |
+
"rate": 0.8,
|
| 82 |
+
"distortion": 0.2,
|
| 83 |
+
"method": "Quantum-inspired sampling"
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
return results
|
| 87 |
+
|
| 88 |
+
def query_interface(query_text, intent_type, use_quantum):
|
| 89 |
+
"""Main query interface"""
|
| 90 |
+
if not query_text.strip():
|
| 91 |
+
return "Please enter a query.", ""
|
| 92 |
+
|
| 93 |
+
results = process_query(query_text, intent_type, use_quantum)
|
| 94 |
+
|
| 95 |
+
# Format output
|
| 96 |
+
output = f"## Query Results\n\n"
|
| 97 |
+
output += f"**Query:** {results['query']}\n\n"
|
| 98 |
+
output += f"**Intent Type:** {results['intent']}\n\n"
|
| 99 |
+
output += f"**Quantum Optimization:** {'Enabled ⚡' if use_quantum else 'Disabled'}\n\n"
|
| 100 |
+
output += f"**Nodes Searched:** {results['nodes_searched']}\n\n"
|
| 101 |
+
output += f"**Edges Traversed:** {results['edges_traversed']}\n\n"
|
| 102 |
+
|
| 103 |
+
if results["results"]:
|
| 104 |
+
output += "### Found Nodes:\n\n"
|
| 105 |
+
for r in results["results"]:
|
| 106 |
+
output += f"**{r['node']}** (Relevance: {r['relevance']:.2f})\n"
|
| 107 |
+
output += f"- Type: {r['data']['type']}\n"
|
| 108 |
+
for key, value in r['data'].items():
|
| 109 |
+
if key not in ['type', 'metadata']:
|
| 110 |
+
output += f"- {key.replace('_', ' ').title()}: {value}\n"
|
| 111 |
+
output += "\n"
|
| 112 |
+
else:
|
| 113 |
+
output += "No nodes found matching your query.\n\n"
|
| 114 |
+
|
| 115 |
+
if use_quantum and "optimization" in results:
|
| 116 |
+
output += "### Quantum Optimization\n\n"
|
| 117 |
+
output += f"- Rate: {results['optimization']['rate']}\n"
|
| 118 |
+
output += f"- Distortion: {results['optimization']['distortion']}\n"
|
| 119 |
+
output += f"- Method: {results['optimization']['method']}\n\n"
|
| 120 |
+
|
| 121 |
+
# JSON output
|
| 122 |
+
json_output = json.dumps(results, indent=2)
|
| 123 |
+
|
| 124 |
+
return output, json_output
|
| 125 |
+
|
| 126 |
+
def browse_graph():
|
| 127 |
+
"""Browse all nodes in the graph"""
|
| 128 |
+
output = "# Knowledge Graph Nodes\n\n"
|
| 129 |
+
for node_name, node_data in SAMPLE_NODES.items():
|
| 130 |
+
output += f"## {node_name}\n\n"
|
| 131 |
+
output += f"**Type:** {node_data['type']}\n\n"
|
| 132 |
+
for key, value in node_data.items():
|
| 133 |
+
if key not in ['type', 'metadata']:
|
| 134 |
+
output += f"- **{key.replace('_', ' ').title()}:** {value}\n"
|
| 135 |
+
if 'metadata' in node_data:
|
| 136 |
+
output += f"\n**Metadata:**\n"
|
| 137 |
+
for k, v in node_data['metadata'].items():
|
| 138 |
+
output += f"- {k.replace('_', ' ').title()}: {v}\n"
|
| 139 |
+
output += "\n---\n\n"
|
| 140 |
+
|
| 141 |
+
output += "# Knowledge Graph Edges\n\n"
|
| 142 |
+
for edge in SAMPLE_EDGES:
|
| 143 |
+
output += f"- **{edge['from']}** → {edge['relationship']} → **{edge['to']}** "
|
| 144 |
+
output += f"(Confidence: {edge['confidence']})\n"
|
| 145 |
+
|
| 146 |
+
return output
|
| 147 |
+
|
| 148 |
+
def show_architecture():
|
| 149 |
+
"""Show system architecture"""
|
| 150 |
+
arch = """# SARS-CoV-2 Knowledge Graph Architecture
|
| 151 |
+
|
| 152 |
+
## System Components
|
| 153 |
+
|
| 154 |
+
### Stage 1: Biomedical Graph (limit-bio-sars)
|
| 155 |
+
- **Node Types:** Virus, Protein, Receptor, Variant, Therapy
|
| 156 |
+
- **Features:** Metadata tracking, Provenance, Confidence scoring
|
| 157 |
+
- **Operations:** O(1) node addition, O(E) edge queries
|
| 158 |
+
|
| 159 |
+
### Stage 2: Multi-Intent Harness (limit-benchmark)
|
| 160 |
+
- **Intent Types:** Factual, Causal, Comparative, Predictive, Exploratory
|
| 161 |
+
- **Performance:** ~1000 queries/second
|
| 162 |
+
- **Metrics:** Graph and query performance tracking
|
| 163 |
+
|
| 164 |
+
### Stage 3: Quantum-Inspired Retrieval (limit-quantum)
|
| 165 |
+
- **Algorithms:** Rate-Distortion optimization, Quantum sampling
|
| 166 |
+
- **Features:** Quantum annealing, Quantum walk simulation
|
| 167 |
+
- **Benefits:** Optimal retrieval strategies
|
| 168 |
+
|
| 169 |
+
### Stage 4: Open-Source Hub (limit-hub)
|
| 170 |
+
- **API:** REST endpoints with Axum framework
|
| 171 |
+
- **Governance:** Validation rules, Quality control
|
| 172 |
+
- **Latency:** <50ms per request
|
| 173 |
+
|
| 174 |
+
### Stage 5: Testing
|
| 175 |
+
- **Coverage:** Unit, Integration, Performance tests
|
| 176 |
+
- **Validation:** Automated quality checks
|
| 177 |
+
|
| 178 |
+
## Technical Stack
|
| 179 |
+
- **Language:** Rust
|
| 180 |
+
- **Dependencies:** serde, axum, tokio, uuid, rand
|
| 181 |
+
- **Performance:** ~100MB memory for 10K nodes
|
| 182 |
+
- **License:** MIT
|
| 183 |
+
|
| 184 |
+
## Data Flow
|
| 185 |
+
|
| 186 |
+
## Source Code
|
| 187 |
+
Full implementation available at:
|
| 188 |
+
https://github.com/NurcholishAdam/SARS-CoV-2-KG
|
| 189 |
+
"""
|
| 190 |
+
return arch
|
| 191 |
+
|
| 192 |
+
# Create Gradio interface
|
| 193 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="SARS-CoV-2 Knowledge Graph") as demo:
|
| 194 |
+
gr.Markdown("""
|
| 195 |
+
# 🦠 SARS-CoV-2 Extended Knowledge Graph
|
| 196 |
+
|
| 197 |
+
An interactive biomedical knowledge graph with quantum-inspired retrieval capabilities.
|
| 198 |
+
Explore viral entities, proteins, variants, and therapies with multi-intent querying.
|
| 199 |
+
|
| 200 |
+
**Version:** 2.4.1 | **Source:** [GitHub](https://github.com/NurcholishAdam/SARS-CoV-2-KG)
|
| 201 |
+
""")
|
| 202 |
+
|
| 203 |
+
with gr.Tabs():
|
| 204 |
+
# Query Tab
|
| 205 |
+
with gr.Tab("🔍 Query"):
|
| 206 |
+
gr.Markdown("### Search the Knowledge Graph")
|
| 207 |
+
with gr.Row():
|
| 208 |
+
with gr.Column(scale=2):
|
| 209 |
+
query_input = gr.Textbox(
|
| 210 |
+
label="Enter your query",
|
| 211 |
+
placeholder="e.g., What is the spike protein? How does Omicron differ?",
|
| 212 |
+
lines=2
|
| 213 |
+
)
|
| 214 |
+
with gr.Row():
|
| 215 |
+
intent_dropdown = gr.Dropdown(
|
| 216 |
+
choices=INTENT_TYPES,
|
| 217 |
+
value="Factual",
|
| 218 |
+
label="Query Intent Type"
|
| 219 |
+
)
|
| 220 |
+
quantum_checkbox = gr.Checkbox(
|
| 221 |
+
label="Enable Quantum Optimization ⚡",
|
| 222 |
+
value=False
|
| 223 |
+
)
|
| 224 |
+
submit_btn = gr.Button("Search", variant="primary")
|
| 225 |
+
|
| 226 |
+
with gr.Column(scale=2):
|
| 227 |
+
output_md = gr.Markdown(label="Results")
|
| 228 |
+
|
| 229 |
+
with gr.Accordion("View JSON Response", open=False):
|
| 230 |
+
json_output = gr.Code(language="json", label="JSON Output")
|
| 231 |
+
|
| 232 |
+
submit_btn.click(
|
| 233 |
+
fn=query_interface,
|
| 234 |
+
inputs=[query_input, intent_dropdown, quantum_checkbox],
|
| 235 |
+
outputs=[output_md, json_output]
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
gr.Examples(
|
| 239 |
+
examples=[
|
| 240 |
+
["What is the spike protein?", "Factual", False],
|
| 241 |
+
["How does the spike protein bind to ACE2?", "Causal", True],
|
| 242 |
+
["Compare Omicron to the original strain", "Comparative", True],
|
| 243 |
+
["What therapies target the spike protein?", "Exploratory", False],
|
| 244 |
+
],
|
| 245 |
+
inputs=[query_input, intent_dropdown, quantum_checkbox]
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# Browse Tab
|
| 249 |
+
with gr.Tab("📊 Browse Graph"):
|
| 250 |
+
gr.Markdown("### Explore All Nodes and Edges")
|
| 251 |
+
browse_btn = gr.Button("Load Knowledge Graph", variant="primary")
|
| 252 |
+
browse_output = gr.Markdown()
|
| 253 |
+
browse_btn.click(fn=browse_graph, outputs=browse_output)
|
| 254 |
+
|
| 255 |
+
# Architecture Tab
|
| 256 |
+
with gr.Tab("🏗️ Architecture"):
|
| 257 |
+
gr.Markdown("### System Architecture Overview")
|
| 258 |
+
arch_btn = gr.Button("Show Architecture", variant="primary")
|
| 259 |
+
arch_output = gr.Markdown()
|
| 260 |
+
arch_btn.click(fn=show_architecture, outputs=arch_output)
|
| 261 |
+
|
| 262 |
+
# About Tab
|
| 263 |
+
with gr.Tab("ℹ️ About"):
|
| 264 |
+
gr.Markdown("""
|
| 265 |
+
## About This Project
|
| 266 |
+
|
| 267 |
+
This is a demonstration interface for the SARS-CoV-2 Extended Knowledge Graph,
|
| 268 |
+
a comprehensive biomedical information system with quantum-inspired retrieval.
|
| 269 |
+
|
| 270 |
+
### Key Features
|
| 271 |
+
|
| 272 |
+
- **Enriched Biomedical Graph:** Comprehensive node types with metadata
|
| 273 |
+
- **Multi-Intent Queries:** Support for 5 query types
|
| 274 |
+
- **Quantum-Inspired Retrieval:** Advanced optimization algorithms
|
| 275 |
+
- **Open-Source:** MIT licensed, community contributions welcome
|
| 276 |
+
|
| 277 |
+
### Intent Types
|
| 278 |
+
|
| 279 |
+
- **Factual:** Direct information retrieval
|
| 280 |
+
- **Causal:** Understanding relationships and mechanisms
|
| 281 |
+
- **Comparative:** Comparing entities or concepts
|
| 282 |
+
- **Predictive:** Forward-looking analysis
|
| 283 |
+
- **Exploratory:** Open-ended discovery
|
| 284 |
+
|
| 285 |
+
### Performance
|
| 286 |
+
|
| 287 |
+
- Query Throughput: ~1000 queries/second
|
| 288 |
+
- API Latency: <50ms
|
| 289 |
+
- Memory: ~100MB for 10K nodes
|
| 290 |
+
|
| 291 |
+
### Links
|
| 292 |
+
|
| 293 |
+
- **GitHub:** [NurcholishAdam/SARS-CoV-2-KG](https://github.com/NurcholishAdam/SARS-CoV-2-KG)
|
| 294 |
+
- **License:** MIT
|
| 295 |
+
- **Version:** 2.4.1
|
| 296 |
+
|
| 297 |
+
### Citation
|
| 298 |
+
|
| 299 |
+
If you use this work, please cite:
|
| 300 |
+
@software{sarscov2_kg_2024,
|
| 301 |
+
title={SARS-CoV-2 Extended Knowledge Graph},
|
| 302 |
+
author={NurcholishAdam},
|
| 303 |
+
year={2024},
|
| 304 |
+
url={https://github.com/NurcholishAdam/SARS-CoV-2-KG}
|
| 305 |
+
}
|
| 306 |
+
""")
|
| 307 |
+
|
| 308 |
+
# Launch the app
|
| 309 |
+
if __name__ == "__main__":
|
| 310 |
+
demo.launch()
|