# kaustubhas/biorag architecture

> Architecture diagram of the GitHub repository kaustubhas/biorag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kaustubhas/biorag
- Repository: https://github.com/kaustubhas/biorag

## Overview

#### Purpose and entry points
Biomedical Assistant is a Python RAG application for disease/symptom questions over a structured biomedical dataset. Primary launch routing is `main.py`, selecting CLI or Streamlit UI. UI launchers invoke Streamlit through `app/run_ui.py` or root `run_ui.py`; interactive CLI behavior resides in `app/cli.py`.

#### Data ingestion and preprocessing
Raw disease-to-symptom records originate in `data/dataset.csv`. The optional scalable preprocessing stage in `pipeline/spark_processor.py` cleans CSV data and writes Parquet to `data/processed/data.parquet`; Spark write failures on Windows fall back to Pandas/PyArrow. Runtime loaders prefer processed Parquet and otherwise consume CSV, creating a clear raw-versus-preprocessed data boundary.

#### Knowledge graph construction
`knowledge_graph/data_processor.py` normalizes loaded data into disease and symptom entities. `knowledge_graph/graph_builder.py` constructs the in-memory NetworkX disease–symptom graph, while `knowledge_graph/build_knowledge_graph.py` coordinates graph-building work. The graph is the retrieval substrate for entity lookup, neighborhood search, graph statistics, and visualization.

#### Retrieval and query interpretation
The RAG orchestration boundary is `rag/biomedical_rag.py`, which combines graph access, query interpretation, and answer generation. `rag/query_processor.py` performs direct, fuzzy, and optional semantic matching using sentence-transformer/PyTorch capabilities. If those dependencies fail or are unavailable, `rag/query_processor_simple.py` provides compatible text-based retrieval, preserving application operation without the ML runtime.

#### Response generation and fallbacks
`rag/response_generator.py` turns retrieved graph context into an answer. Its primary external inference dependency is a locally running Ollama service with the `llama3.2` model. When Ollama is unavailable, generation degrades to rule-based and text-matching responses rather than failing the request. This separates deterministic retrieval from optional generative enrichment.

#### User-facing application layer
`app/streamlit_app.py` provides the stateful Streamlit experience: graph exploration, search/filtering, Q&A, history, and data-source status. `app/cli.py` exposes the same system through a terminal workflow. The application layer should consume the graph/RAG interfaces rather than directly duplicating preprocessing or matching logic.

#### Optional integrations and operational dependencies
Core runtime is Python 3.8+, with Pandas, NetworkX, Plotly, and Streamlit. PySpark is optional for preprocessing; Sentence Transformers and PyTorch are optional for semantic retrieval. Neo4j integration is isolated in `knowledge_graph/neo4j_builder.py`, indicating a potential persistent graph backend distinct from the default in-memory NetworkX path. Shared configuration is centralized in `config.py`.

Main entry points: [`rag/biomedical_rag.py`](https://github.com/kaustubhas/biorag/blob/main/rag/biomedical_rag.py) (Biomedical RAG), [`app/streamlit_app.py`](https://github.com/kaustubhas/biorag/blob/main/app/streamlit_app.py) (Streamlit application), [`pipeline/spark_processor.py`](https://github.com/kaustubhas/biorag/blob/main/pipeline/spark_processor.py) (Optional preprocessing), [`knowledge_graph/data_processor.py`](https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/data_processor.py) (Entity normalization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_main["Launch router<br/>Python entry point<br/>[main.py]"]
  node_config["Shared configuration<br/>Python config<br/>[config.py]"]
end

subgraph group_data["Data pipeline"]
  node_raw_csv[("Raw disease dataset<br/>CSV data<br/>[dataset.csv]")]
  node_spark_processor["Optional preprocessing<br/>Spark/Pandas pipeline<br/>[spark_processor.py]"]
  node_processed_parquet[("Processed dataset<br/>Parquet data<br/>[data.parquet]")]
end

subgraph group_graph["Knowledge graph"]
  node_data_processor["Entity normalization<br/>data processor<br/>[data_processor.py]"]
  node_graph_builder["NetworkX graph builder<br/>[graph_builder.py]"]
  node_graph_coordinator["Graph build coordinator<br/>build orchestration"]
  node_networkx_graph[("Disease–symptom graph<br/>in-memory retrieval substrate")]
  node_neo4j_builder[("Optional Neo4j backend<br/>persistent graph integration<br/>[neo4j_builder.py]")]
end

subgraph group_rag["RAG runtime"]
  node_rag_orchestrator["Biomedical RAG<br/>RAG orchestration<br/>[biomedical_rag.py]"]
  node_query_processor["Semantic query processor<br/>ML retrieval<br/>[query_processor.py]"]
  node_simple_query_processor["Text retrieval fallback<br/>fallback retrieval"]
  node_response_generator["Response generator<br/>answer generation"]
  node_ollama{{"Ollama llama3.2<br/>local inference service"}}
end

subgraph group_interfaces["User interfaces"]
  node_streamlit_app["Streamlit application<br/>web UI<br/>[streamlit_app.py]"]
  node_cli["Interactive CLI<br/>terminal UI<br/>[cli.py]"]
  node_ui_launcher["UI launchers<br/>Streamlit launcher<br/>[run_ui.py]"]
end

node_main -->|"CLI mode"| node_cli
node_main -->|"UI mode"| node_ui_launcher
node_ui_launcher -->|"launches"| node_streamlit_app
node_config -.->|"configures"| node_spark_processor
node_config -.->|"configures"| node_rag_orchestrator
node_raw_csv -->|"clean input"| node_spark_processor
node_spark_processor -->|"writes"| node_processed_parquet
node_processed_parquet -->|"preferred input"| node_data_processor
node_raw_csv -.->|"fallback input"| node_data_processor
node_data_processor -->|"normalized entities"| node_graph_builder
node_graph_coordinator -->|"coordinates"| node_graph_builder
node_graph_builder -->|"builds"| node_networkx_graph
node_graph_coordinator -.->|"optional backend path"| node_neo4j_builder
node_rag_orchestrator -->|"retrieves context"| node_networkx_graph
node_rag_orchestrator -->|"interprets query"| node_query_processor
node_query_processor -.->|"ML unavailable"| node_simple_query_processor
node_rag_orchestrator -->|"generates answer"| node_response_generator
node_response_generator -.->|"optional inference"| node_ollama
node_streamlit_app -->|"Q&amp;A and search"| node_rag_orchestrator
node_streamlit_app -->|"explores"| node_networkx_graph
node_cli -->|"terminal queries"| node_rag_orchestrator

click node_main "https://github.com/kaustubhas/biorag/blob/main/main.py"
click node_config "https://github.com/kaustubhas/biorag/blob/main/config.py"
click node_raw_csv "https://github.com/kaustubhas/biorag/blob/main/data/dataset.csv"
click node_spark_processor "https://github.com/kaustubhas/biorag/blob/main/pipeline/spark_processor.py"
click node_processed_parquet "https://github.com/kaustubhas/biorag/blob/main/data/processed/data.parquet"
click node_data_processor "https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/data_processor.py"
click node_graph_builder "https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/graph_builder.py"
click node_graph_coordinator "https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/build_knowledge_graph.py"
click node_neo4j_builder "https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/neo4j_builder.py"
click node_rag_orchestrator "https://github.com/kaustubhas/biorag/blob/main/rag/biomedical_rag.py"
click node_query_processor "https://github.com/kaustubhas/biorag/blob/main/rag/query_processor.py"
click node_simple_query_processor "https://github.com/kaustubhas/biorag/blob/main/rag/query_processor_simple.py"
click node_response_generator "https://github.com/kaustubhas/biorag/blob/main/rag/response_generator.py"
click node_streamlit_app "https://github.com/kaustubhas/biorag/blob/main/app/streamlit_app.py"
click node_cli "https://github.com/kaustubhas/biorag/blob/main/app/cli.py"
click node_ui_launcher "https://github.com/kaustubhas/biorag/blob/main/app/run_ui.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_config toneBlue
class node_raw_csv,node_spark_processor,node_processed_parquet toneAmber
class node_data_processor,node_graph_builder,node_graph_coordinator,node_networkx_graph,node_neo4j_builder toneMint
class node_rag_orchestrator,node_query_processor,node_simple_query_processor,node_response_generator,node_ollama toneRose
class node_streamlit_app,node_cli,node_ui_launcher toneIndigo
```

## Components

### Entry points

- [Launch router](https://github.com/kaustubhas/biorag/blob/main/main.py): Python entry point · Selects CLI or Streamlit mode. · `main.py`
- [Shared configuration](https://github.com/kaustubhas/biorag/blob/main/config.py): Python config · `config.py`

### Data pipeline

- [Raw disease dataset](https://github.com/kaustubhas/biorag/blob/main/data/dataset.csv): CSV data · `data/dataset.csv`
- [Optional preprocessing](https://github.com/kaustubhas/biorag/blob/main/pipeline/spark_processor.py): Spark/Pandas pipeline · Cleans CSV and writes Parquet, with a Windows fallback. · `pipeline/spark_processor.py`
- [Processed dataset](https://github.com/kaustubhas/biorag/blob/main/data/processed/data.parquet): Parquet data · `data/processed/data.parquet`

### Knowledge graph

- [Entity normalization](https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/data_processor.py): data processor · Loads preferred Parquet or fallback CSV into disease and symptom entities. · `knowledge_graph/data_processor.py`
- [NetworkX graph builder](https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/graph_builder.py): graph builder · Builds the in-memory disease–symptom graph. · `knowledge_graph/graph_builder.py`
- [Graph build coordinator](https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/build_knowledge_graph.py): build orchestration · `knowledge_graph/build_knowledge_graph.py`
- **Disease–symptom graph**: in-memory retrieval substrate · Supports lookup, neighborhood search, statistics, and visualization.
- [Optional Neo4j backend](https://github.com/kaustubhas/biorag/blob/main/knowledge_graph/neo4j_builder.py): persistent graph integration · Alternative persistent graph path. · `knowledge_graph/neo4j_builder.py`

### RAG runtime

- [Biomedical RAG](https://github.com/kaustubhas/biorag/blob/main/rag/biomedical_rag.py): RAG orchestration · Combines graph access, query interpretation, and answer generation. · `rag/biomedical_rag.py`
- [Semantic query processor](https://github.com/kaustubhas/biorag/blob/main/rag/query_processor.py): ML retrieval · Uses direct, fuzzy, and optional semantic matching. · `rag/query_processor.py`
- [Text retrieval fallback](https://github.com/kaustubhas/biorag/blob/main/rag/query_processor_simple.py): fallback retrieval · Compatible retrieval without the ML runtime. · `rag/query_processor_simple.py`
- [Response generator](https://github.com/kaustubhas/biorag/blob/main/rag/response_generator.py): answer generation · Falls back to rule-based responses when Ollama is unavailable. · `rag/response_generator.py`
- **Ollama llama3.2**: local inference service · Optional generative enrichment.

### User interfaces

- [Streamlit application](https://github.com/kaustubhas/biorag/blob/main/app/streamlit_app.py): web UI · Provides graph exploration, Q&A, history, and data status. · `app/streamlit_app.py`
- [Interactive CLI](https://github.com/kaustubhas/biorag/blob/main/app/cli.py): terminal UI · `app/cli.py`
- [UI launchers](https://github.com/kaustubhas/biorag/blob/main/app/run_ui.py): Streamlit launcher · `app/run_ui.py`

## Connections

- Launch router → Interactive CLI: CLI mode
- Launch router → UI launchers: UI mode
- UI launchers → Streamlit application: launches
- Shared configuration → Optional preprocessing: configures
- Shared configuration → Biomedical RAG: configures
- Raw disease dataset → Optional preprocessing: clean input
- Optional preprocessing → Processed dataset: writes
- Processed dataset → Entity normalization: preferred input
- Raw disease dataset → Entity normalization: fallback input
- Entity normalization → NetworkX graph builder: normalized entities
- Graph build coordinator → NetworkX graph builder: coordinates
- NetworkX graph builder → Disease–symptom graph: builds
- Graph build coordinator → Optional Neo4j backend: optional backend path
- Biomedical RAG → Disease–symptom graph: retrieves context
- Biomedical RAG → Semantic query processor: interprets query
- Semantic query processor → Text retrieval fallback: ML unavailable
- Biomedical RAG → Response generator: generates answer
- Response generator → Ollama llama3.2: optional inference
- Streamlit application → Biomedical RAG: Q&A and search
- Streamlit application → Disease–symptom graph: explores
- Interactive CLI → Biomedical RAG: terminal queries
