--- title: AI Agent emoji: ๐Ÿค– colorFrom: blue colorTo: green sdk: docker app_port: 7860 pinned: false --- # AI Imaging Agent (Imaging Plaza) [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/) [![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) An intelligent RAG + AI agent system that helps users discover the right imaging software for their images and tasks. Upload an image, describe what you want to do, and get ranked tool recommendations with links to runnable demos. ## โœจ Key Features - **๐Ÿค– Conversational AI Agent**: Natural language interaction with multi-turn context - **๐Ÿ” Smart Retrieval**: BGE-M3 embeddings + FAISS + CrossEncoder reranking - **๐Ÿ‘๏ธ Vision-Aware Selection**: VLM-based tool selection considering both image content and metadata - **๐Ÿฅ Medical Imaging Focus**: Specialized support for CT, MRI, DICOM, NIfTI, and other medical formats - **๐ŸŽฏ Format-Aware Matching**: IO compatibility scoring based on file formats and dimensions - **๐Ÿš€ Demo Integration**: Direct execution of Gradio Space demos on your images - **๐Ÿ“Š Rich UI**: Chat interface with image previews, file management, and execution traces

--- ## ๐Ÿš€ Quick Start ### Prerequisites - Python 3.10โ€“3.12 - OpenAI API key (or compatible API endpoint) - Internet connection for model calls ### Installation ```bash # Clone the repository git clone cd ai-agent # Create virtual environment python -m venv .venv # Activate virtual environment # On Linux/macOS: source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install the package pip install --upgrade pip pip install -e . # For development (includes test dependencies) pip install -e ".[dev]" ``` ### Configuration Create a `.env` file at the repository root: ```dotenv # Required: OpenAI API key OPENAI_API_KEY=sk-xxxx # Optional: GitHub token for repo info tool GITHUB_TOKEN=ghp_xxxx # Optional: Alternative model providers (EPFL, etc.) EPFL_API_KEY=sk-xxxx EPFL_API_KEY_EMBEDDER=sk-xxxx # Software catalog path SOFTWARE_CATALOG=dataset/catalog.jsonl # Pipeline configuration TOP_K=8 # Number of candidates to retrieve NUM_CHOICES=3 # Number of tools to recommend AGENT_OUTPUT_RETRIES=3 # Structured output validation retries EMBED_CATALOG_ON_START=1 # Pre-embed catalog if FAISS is empty # Logging configuration LOGLEVEL_CONSOLE=WARNING LOGLEVEL_FILE=INFO FILE_LOG=1 LOG_DIR=logs LOG_PROMPTS=0 # Set to 1 to save prompt snapshots for debugging # Custom config path CONFIG_PATH=config.yaml ``` ### Model Configuration The agent model can be configured via `config.yaml`: ```yaml # AI Agent Model Configuration # Default/fallback model (used for CLI and initial startup) agent_model: name: "gpt-5.1" base_url: null # null for default OpenAI endpoint api_key_env: "OPENAI_API_KEY" # Available models for UI dropdown available_models: - display_name: "gpt-4o-mini" name: "gpt-4o-mini" base_url: null provider: "OpenAI" api_key_env: "OPENAI_API_KEY" - display_name: "gpt-4o" name: "gpt-4o" base_url: null provider: "OpenAI" api_key_env: "OPENAI_API_KEY" - display_name: "gpt-5-mini" name: "gpt-5-mini" base_url: null provider: "OpenAI" api_key_env: "OPENAI_API_KEY" - display_name: "gpt-5.1" name: "gpt-5.1" base_url: null provider: "OpenAI" api_key_env: "OPENAI_API_KEY" retrieval: embedder: backend: "remote" # "remote" or "local" model_name: "Qwen/Qwen3-Embedding-8B" base_url: "https://inference-rcp.epfl.ch/v1" api_key_env: "EPFL_API_KEY_EMBEDDER" timeout_s: 20 # local example: # backend: "local" # model_name: "BAAI/bge-m3" # device: "cpu" # optional reranker: backend: "remote" # "remote" or "local" model_name: "BAAI/bge-reranker-v2-m3" base_url: "https://inference-rcp.epfl.ch/v1" api_key_env: "EPFL_API_KEY_EMBEDDER" timeout_s: 20 # local example: # backend: "local" # model_name: "BAAI/bge-reranker-v2-m3" # device: "cpu" # optional ``` ### Running the App ```bash # Start the chat interface ai_agent chat # Open your browser to: # http://127.0.0.1:7860 ``` Try uploading a cat image and asking: > "I want to segment the cat from this image" --- ## ๐Ÿ’ฌ Usage ### Chat Interface The chat interface provides a natural conversation flow: 1. **Upload Files**: Drop images (PNG, JPG, TIFF, DICOM, NIfTI, etc.) or other supported files 2. **Describe Your Task**: Use natural language like "segment the lungs" or "register brain MRI" 3. **Review Recommendations**: Get ranked tool suggestions with accuracy scores and explanations 4. **Run Demos**: Click "Run demo" to execute tools directly on your uploaded images 5. **Iterate**: Ask for alternatives, refine your query, or upload different files ### Supported File Formats **Images:** - Standard: PNG, JPG, JPEG, WEBP, BMP, GIF - Medical: DICOM (.dcm), NIfTI (.nii, .nii.gz), TIFF stacks - Scientific: Multi-page TIFF, TIFF with metadata **Other Files:** - Data: CSV, JSON, XML - Media: MP3, MP4 ### Example Queries - "Segment the lungs from this CT scan" - "Register these two brain MRI images" - "Extract text from this medical report image" - "Classify what organ is shown in this ultrasound" - "Detect tumors in this MRI scan" - "I need to analyze DICOM files, what tools are available?" ### Understanding Results Each recommendation includes: - **Rank**: Priority order (1 = best match) - **Accuracy Score**: Confidence level (0-100%) - **Explanation**: Why this tool matches your request - **Metadata**: Supported modalities, dimensions, formats, license - **Demo Link**: Direct link to runnable example --- ## ๐Ÿ—๏ธ Architecture ### Pipeline Overview The system follows a two-stage architecture: ``` User Input (Image + Text Query) โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ RETRIEVAL STAGE โ”‚ โ”‚ - BGE-M3 Embeddings โ”‚ โ”‚ - FAISS Vector Search โ”‚ โ”‚ - CrossEncoder Reranking โ”‚ โ”‚ - Format Token Matching โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ Top-K Candidates โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ AGENT SELECTION โ”‚ โ”‚ - Pydantic AI Agent โ”‚ โ”‚ - OpenAI VLM โ”‚ โ”‚ - Image + Metadata Analysis โ”‚ โ”‚ - Multi-Tool Reasoning โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ Ranked Recommendations ``` ### Retrieval Stage **No LLM calls** - purely text-based search: 1. **Query Construction**: User task + format tokens from uploaded files 2. **Embedding**: BGE-M3 model generates query embedding 3. **Vector Search**: FAISS retrieves top candidates 4. **Reranking**: CrossEncoder refines results for precision 5. **Retry Broadening**: If too few hits, retry with a shorter/broader query ### Agent Selection Stage **Single VLM call** - multimodal reasoning: 1. **Input Preparation**: - Text: User query + candidate table + file metadata - Image: PNG preview (converted from any format) - Context: Original file format, dimensions, modality 2. **Agent Tools**: - `search_tools`: Search catalog with query - `search_alternative`: Find alternatives (iterative) - `repo_info`: Fetch GitHub documentation via DeepWiki MCP 3. **Output**: Ranked tool selections with accuracy scores and explanations ### Key Components - **`api/pipeline.py`**: RAG retrieval orchestrator - **`agent/agent.py`**: Pydantic AI agent with tool definitions - **`retriever/`**: Embedding, FAISS indexing, reranking - **`generator/`**: Prompts and schema for tool selection - **`ui/`**: Gradio chat interface components - **`utils/`**: Image processing, metadata extraction, file validation - **`catalog/`**: Catalog syncing from GraphDB (optional) --- ## โš™๏ธ Configuration ### Environment Variables | Variable | Description | Default | Required | |----------|-------------|---------|----------| | `OPENAI_API_KEY` | OpenAI API key | - | โœ… | | `EPFL_API_KEY_EMBEDDER` | API key for remote embedder and reranker endpoints | - | โœ… (when `retrieval.embedder.backend: remote` and/or `retrieval.reranker.backend: remote`) | | `GITHUB_TOKEN` | GitHub token for repo info | - | โŒ | | `SOFTWARE_CATALOG` | Path to catalog JSONL | `dataset/catalog.jsonl` | โœ… | | `TOP_K` | Retrieval candidates count | `8` | โŒ | | `NUM_CHOICES` | Tools to recommend | `3` | โŒ | | `AGENT_OUTPUT_RETRIES` | Structured output validation retries | `3` | โŒ | | `EMBED_CATALOG_ON_START` | Pre-embed catalog on startup when FAISS is empty | `1` | โŒ | | `LOGLEVEL_CONSOLE` | Console log level | `WARNING` | โŒ | | `LOGLEVEL_FILE` | File log level | `INFO` | โŒ | | `FILE_LOG` | Enable file logging | `1` | โŒ | | `LOG_DIR` | Log directory | `logs` | โŒ | | `LOG_PROMPTS` | Save prompt snapshots | `0` | โŒ | | `CONFIG_PATH` | Model config file | `config.yaml` | โœ… | ### GraphDB Catalog Sync (Optional) For automatic catalog syncing from a GraphDB instance: ```dotenv GRAPHDB_URL=https://your-graphdb.example.com GRAPHDB_GRAPH=your-graph-name GRAPHDB_USER=username GRAPHDB_PASSWORD=password GRAPHDB_QUERY_FILE=/path/to/query.rq SYNC_EVERY_HOURS=24 # Auto-refresh interval (0 to disable) OUTPUT_JSONLD=dataset/catalog.jsonld OUTPUT_JSONL=dataset/catalog.jsonl ``` Run manual sync: ```bash ai_agent sync ``` --- ## ๐Ÿ“‹ Catalog Format The catalog is a JSONL file where each line is a `SoftwareDoc` following schema.org SoftwareSourceCode structure. ### Minimal Example ```json { "name": "3d-lungs-segmentation", "description": "3D lung segmentation from CT; returns a mask/overlay.", "applicationCategory": ["Medical Imaging"], "featureList": ["segmentation"], "imagingModality": ["CT"], "dims": [3], "anatomy": ["lung"], "keywords": ["mask", "overlay", "lung segmentation", "CT"], "programmingLanguage": "Python", "requiresGPU": false, "isAccessibleForFree": true, "license": "Apache-2.0", "supportingData": [ { "datasetFormat": "TIFF", "bodySite": "lung", "imagingModality": "CT", "hasDimensionality": 3 }, { "datasetFormat": "DICOM", "bodySite": "lung", "imagingModality": "CT", "hasDimensionality": 3 } ], "runnableExample": [ { "hostType": "gradio", "url": "https://huggingface.co/spaces/qchapp/3d-lungs-segmentation", "name": "HF Space" } ] } ``` ### Key Fields - **name**: Unique identifier for the tool - **description**: Clear explanation of what the tool does - **featureList**: Operations (e.g., segmentation, registration, classification) - **imagingModality**: Medical imaging types (CT, MRI, XR, US, PET) - **dims**: Supported dimensions (2D, 3D, 4D) - **anatomy**: Body parts/organs - **supportingData**: Format compatibility information (critical for matching) - **runnableExample**: Links to live demos (HuggingFace Spaces, notebooks, web apps) --- ## ๐Ÿ”ง Development ### Project Structure ``` ai-agent/ โ”œโ”€โ”€ src/ai_agent/ โ”‚ โ”œโ”€โ”€ agent/ # Pydantic AI agent and tools โ”‚ โ”‚ โ”œโ”€โ”€ agent.py # Agent definition โ”‚ โ”‚ โ”œโ”€โ”€ models.py # Agent state models โ”‚ โ”‚ โ”œโ”€โ”€ tools/ # Agent tool implementations โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ search_tool.py โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ search_alternative_tool.py โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ gradio_space_tool.py โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ repo_info_tool.py โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ deepwiki_tool.py โ”‚ โ”‚ โ””โ”€โ”€ utils.py โ”‚ โ”œโ”€โ”€ api/ # Pipeline orchestration โ”‚ โ”‚ โ””โ”€โ”€ pipeline.py # RAGImagingPipeline โ”‚ โ”œโ”€โ”€ retriever/ # Retrieval components โ”‚ โ”‚ โ”œโ”€โ”€ text_embedder.py โ”‚ โ”‚ โ”œโ”€โ”€ vector_index.py โ”‚ โ”‚ โ”œโ”€โ”€ reranker.py โ”‚ โ”‚ โ””โ”€โ”€ software_doc.py โ”‚ โ”œโ”€โ”€ generator/ # Agent prompts and schemas โ”‚ โ”‚ โ”œโ”€โ”€ prompts.py โ”‚ โ”‚ โ””โ”€โ”€ schema.py โ”‚ โ”œโ”€โ”€ ui/ # Gradio interface โ”‚ โ”‚ โ”œโ”€โ”€ app.py โ”‚ โ”‚ โ”œโ”€โ”€ handlers.py โ”‚ โ”‚ โ”œโ”€โ”€ components.py โ”‚ โ”‚ โ”œโ”€โ”€ formatters.py โ”‚ โ”‚ โ”œโ”€โ”€ state.py โ”‚ โ”‚ โ””โ”€โ”€ visualizations.py โ”‚ โ”œโ”€โ”€ utils/ # Shared utilities โ”‚ โ”‚ โ”œโ”€โ”€ config.py # Configuration management โ”‚ โ”‚ โ”œโ”€โ”€ file_validator.py โ”‚ โ”‚ โ”œโ”€โ”€ image_meta.py # Metadata extraction โ”‚ โ”‚ โ”œโ”€โ”€ image_io.py โ”‚ โ”‚ โ”œโ”€โ”€ previews.py โ”‚ โ”‚ โ””โ”€โ”€ tags.py โ”‚ โ”œโ”€โ”€ catalog/ # Catalog syncing โ”‚ โ”‚ โ””โ”€โ”€ sync.py โ”‚ โ””โ”€โ”€ cli.py # CLI entry point โ”œโ”€โ”€ tests/ # Test suite โ”‚ โ”œโ”€โ”€ test_retrieval_pipeline.py โ”‚ โ”œโ”€โ”€ test_repo_summary.py โ”‚ โ””โ”€โ”€ data/ โ”œโ”€โ”€ artifacts/ # Generated artifacts โ”‚ โ””โ”€โ”€ rag_index/ # FAISS index โ”œโ”€โ”€ dataset/ # Catalog data โ”‚ โ””โ”€โ”€ catalog.jsonl โ”œโ”€โ”€ logs/ # Application logs โ”œโ”€โ”€ config.yaml # Model configuration โ”œโ”€โ”€ pyproject.toml # Project metadata & dependencies โ”œโ”€โ”€ Dockerfile # Production Docker image โ”œโ”€โ”€ tools/image/Dockerfile # Development Docker image โ””โ”€โ”€ justfile # Task runner commands ``` ### Local Development ```bash # Install in development mode pip install -e ".[dev]" # Run tests pytest tests/ ``` ### Testing Run the test suite: ```bash # All tests pytest tests/ # Specific test file pytest tests/test_retrieval_pipeline.py # With verbose output pytest -v tests/ # With coverage pytest --cov=ai_agent tests/ ``` ### Logging & Debugging **Console Logs**: Set `LOGLEVEL_CONSOLE=DEBUG` for verbose output **File Logs**: Automatically saved to `logs/app_YYYYMMDD.log` (rotates daily) **Prompt Snapshots**: Enable `LOG_PROMPTS=1` to save: - `logs/vlm_selector_YYYYMMDD_HHMMSS.txt` - System/user prompts --- ## ๐Ÿ“š API & CLI Reference ### CLI Commands ```bash # Launch chat interface ai_agent chat # Sync catalog from GraphDB ai_agent sync ``` ## ๐Ÿ—บ๏ธ Maintainer Guide For full project documentation with detailed folder responsibilities, environment defaults, and improvement guidelines, see [docs/guide.md](docs/guide.md). --- ## ๐Ÿ“ Changelog See [CHANGELOG.md](CHANGELOG.md) for detailed version history. ### Recent Highlights **[1.0.0]** - โœจ New chat-based interface (`ai_agent chat`) with rich media and tool integration - ๐Ÿ› ๏ธ Fully agent-based architecture replacing legacy pipelines - ๐Ÿ” Smarter retrieval with automatic retry - ๐Ÿ”— DeepWiki MCP integration for fast GitHub repository documentation access - ๐Ÿ”ง YAML configuration (`config.yaml`) for flexible model and backend setup - ๐ŸŽจ Redesigned UI with Imaging Plaza branding and improved UX - โšก Performance improvements (pre-embedding, caching, faster startup) - ๐Ÿงน Major cleanup: removed deprecated code paths, legacy UI, and outdated tests **[0.1.3] - 2025-10-22** - Gradio space runner tool - Repository info tool - UI fixes and polish --- ## ๐Ÿ“„ License This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details. --- ## ๐Ÿ™ Credits & Acknowledgments **Developed by**: Imaging Plaza Team **Technologies:** - [Pydantic AI](https://github.com/pydantic/pydantic-ai) - AI agent framework - [OpenAI](https://openai.com) - GPT vision model - [FAISS](https://github.com/facebookresearch/faiss) - Vector search - [BGE-M3](https://huggingface.co/BAAI/bge-m3) - Multilingual embeddings - [Gradio](https://gradio.app) - Interactive web UI - [DeepWiki](https://deepwiki.com) - GitHub repository documentation **Medical Imaging Formats:** - [pydicom](https://github.com/pydicom/pydicom) - DICOM support - [nibabel](https://nipy.org/nibabel/) - NIfTI support --- ## ๐Ÿ“ฎ Support For issues, questions, or contributions, please contact the Imaging Plaza team. --- **๐Ÿฅ Medical Disclaimer**: This software is a tool recommendation system, not a diagnostic tool. Always consult qualified medical professionals for clinical decisions.