This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
MicroGrowLinkService is a Gradio web application for predicting optimal growth media for microorganisms based on their traits. It's part of the KG-Microbe project under KG-Hub and uses a KOGUT (Relational Graph Transformer) model trained on the KG-Microbe knowledge graph.
Users input microbial characteristics (temperature preference, oxygen requirement, Gram stain, cell shape, motility, sporulation) and receive ranked predictions of growth media with confidence scores.
MicroGrowLinkService/
├── app.py # Main Gradio application entry point
├── config.py # Configuration (model/data paths, constants)
├── requirements.txt # Python dependencies
├── src/
│ ├── __init__.py
│ ├── feature_utils.py # Feature validation and parsing
│ ├── predict.py # Prediction wrapper for MicroGrowLink
│ └── ui_components.py # Gradio UI component definitions
└── data/ # References MicroGrowLink/data/
app.py: Main entry point that:
- Creates Gradio interface with feature dropdowns and advanced options
- Wires up prediction function to UI components
- Handles initialization and error handling
- Launches web server on port 7860
config.py: Centralized configuration:
- Model path: Points to KOGUT .pt file in MicroGrowLink/models/
- Data path: Points to preprocessed data in MicroGrowLink/data/
- Feature categories: Valid values for each trait type
- Validation thresholds and UI constants
src/feature_utils.py: Feature handling:
build_feature_string(): Converts dropdown selections to "type:value" format- Skips None and "unknown" values (treats them as not selected)
parse_feature_string(): Parses feature strings into dictionariesvalidate_features(): Checks coverage against knowledge graph entitiesformat_validation_message(): Creates user-friendly validation messages
src/predict.py: Prediction interface:
MicroGrowPredictor: Wrapper class that calls MicroGrowLink's predict_novel_taxon.py- Uses MicroGrowLink's
.venv/bin/pythondirectly (not uv) - Runs command:
.venv/bin/python -m src.learn.predict_novel_taxon - Executes from MicroGrowLink directory (cwd) for proper imports
- The
-mflag ensures proper Python package imports without PYTHONPATH issues - Formats raw predictions into display-friendly DataFrames
- Handles temporary file management
src/ui_components.py: UI definitions:
- Feature input components (6 dropdowns for traits)
- Each dropdown includes None, "unknown", and valid trait values
- Default value is "unknown" (which is skipped when building feature string)
- Advanced options (top-k, device, hidden_dim)
- Output components (validation, predictions table, log)
- Example profiles for quick testing
- User selects microbial traits from dropdowns
build_feature_string()converts selections to "temperature:mesophilic,oxygen:aerobe" formatvalidate_features()checks features against entity2id.txt from knowledge graphMicroGrowPredictor.predict()calls predict_novel_taxon.py via subprocess- Prediction script loads KOGUT model, encodes features, runs inference
- Results parsed from TSV and formatted for display
- User sees ranked media predictions with confidence scores
This service uses:
Local Data (in this repository):
- Vocabularies:
data/kogut/vocabularies.json- Entity and relation vocabularies from KG-Microbe - Graph files:
data/kogut/*.json- Train/validation/test graph structures
MicroGrowLink Repository (sibling directory):
- Model file:
../MicroGrowLink/models/kogut_large_kg_20251024_143743_RESUME_20251026_212314.pt - Prediction script:
../MicroGrowLink/src/learn/predict_novel_taxon.py
The MicroGrowLink repository must be present at ../MicroGrowLink/ relative to this service.
Features are validated before prediction:
- Minimum 3 features recommended (warns if less)
- Minimum 2 categories recommended (warns if less)
- Minimum 50% coverage required (errors if less)
- Coverage checked against entity2id.txt to ensure features exist in KG
Confidence levels:
- High: ≥80% coverage, ≥5 features, no warnings
- Medium: ≥60% coverage, ≥3 features
- Low: <60% coverage or <3 features
- uv package manager (recommended)
- Python 3.9+ (managed by uv via .python-version)
- MicroGrowLink repository cloned at
../MicroGrowLink/- MicroGrowLink must have its own environment set up with PyTorch, models, etc.
- This service uses subprocess to call MicroGrowLink's prediction script
# Install uv if not already installed
curl -LsSf https://astral.sh/uv/install.sh | sh
# Sync dependencies (creates venv automatically)
# Only installs Gradio and Pandas - minimal web service dependencies
uv sync
# Verify paths in config.py point to correct locations
uv run python -c "import config; print('\n'.join(config.validate_paths()) or 'Paths OK')"Important Architecture Decision: This service has minimal dependencies (only Gradio and Pandas) because:
- It calls MicroGrowLink's prediction script via subprocess
- The prediction script runs in MicroGrowLink's environment with all the deep learning dependencies
- This keeps the web service lightweight and avoids complex PyTorch dependency issues
- All model loading, inference, and computation happens in the subprocess
# Launch Gradio app with uv
uv run python app.py
# Or shorter
uv run app.py
# Access at http://localhost:7860# Check configuration
uv run python -c "import config; config.validate_paths()"
# Test feature validation
uv run python -c "from src.feature_utils import *; print(build_feature_string(temperature='mesophilic', oxygen='aerobe'))"
# Test prediction (requires valid model/data)
uv run python -c "from src.predict import quick_predict; print(quick_predict('temperature:mesophilic,oxygen:aerobe'))"
# Add a new dependency
uv add <package-name>
# Remove a dependency
uv remove <package-name>
# Update all dependencies
uv sync --upgradepyproject.toml: Modern Python project configuration
- Defines project metadata and dependencies
- Used by uv for package management
- Includes dev dependencies (pytest, black, ruff)
.python-version: Specifies Python version (3.11)
- Used by uv to select Python interpreter
- Ensures consistent Python version across environments
requirements.txt: Legacy dependency list (kept for compatibility)
- Can be used with pip if uv is not available
- Generated from pyproject.toml
The KOGUT model requires hidden_dim=64:
- This was verified by inspecting the model checkpoint's
relation_embedding.weightshape:[24, 64] - The default is set in
config.DEFAULT_HIDDEN_DIM = 64 - Users can override this in the UI if using a different model variant
The current implementation uses subprocess to call predict_novel_taxon.py rather than direct imports. This approach:
- Pros: Simpler, no need to copy complex prediction logic, easier to maintain
- Cons: Slower due to model reload per prediction, higher memory overhead
For production, consider refactoring to load the model once and import prediction functions directly.
The predictor is initialized globally in app.py to avoid reloading. However, the subprocess approach still reloads the model for each prediction. To optimize:
- Import prediction modules directly instead of subprocess
- Load model once at app startup
- Reuse loaded model for all predictions
- Configuration errors shown at startup but don't block app launch
- Feature validation errors displayed to user before prediction
- Prediction failures caught and displayed with helpful messages
- Temporary TSV files cleaned up after each prediction
To add new feature categories:
- Add to
FEATURE_CATEGORIESin config.py - Create dropdown in
create_feature_inputs()in ui_components.py - Add parameter to
build_feature_string()in feature_utils.py - Update examples in
create_examples()in ui_components.py
Currently hardcoded to KOGUT. To support multiple models:
- Add model_type radio button to UI
- Update config.py with paths for each model
- Pass model_type to MicroGrowPredictor
- Update predict_novel_taxon.py call with --model_type flag