YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

Image-to-Image RAG Pipeline

A production-style AI image generation system with RAG (Retrieval-Augmented Generation) memory. Built with FastAPI, Hugging Face APIs, ChromaDB, and SQLite.

The system learns from stored user data and retrieves similar designs before generating new images, creating a context-aware image generation pipeline.

πŸ—οΈ Architecture

User Request (image/text)
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FastAPI Endpoints  β”‚  /upload-image, /generate-image, /search-similar, /history
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Vision Service     β”‚  Qwen2-VL via HF Inference API
β”‚  (Image β†’ Text)     β”‚  Converts uploaded image to detailed description
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Embedding Service  β”‚  sentence-transformers/all-MiniLM-L6-v2
β”‚  (Text β†’ Vector)    β”‚  Generates 384-dim embeddings from descriptions
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Vector DB (ChromaDB)β”‚  Store & retrieve similar image embeddings
β”‚  RAG Retrieval       β”‚  Top-K cosine similarity search
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Prompt Engineering β”‚  Combines user input + vision output + RAG results
β”‚  Service            β”‚  into an optimized generation prompt
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Image Generation   β”‚  FLUX.1-schnell via HF Inference API
β”‚  Service            β”‚  Generates new image from final prompt
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Storage Service    β”‚  SQLite DB (metadata) + Local filesystem (images)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

image-rag-pipeline/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py                  # FastAPI app entry point
β”‚   β”œβ”€β”€ config.py                # Environment variables & settings
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── routes.py            # All API endpoints
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ vision.py            # Qwen2-VL image understanding
β”‚   β”‚   β”œβ”€β”€ embedding.py         # Sentence-transformers embeddings
β”‚   β”‚   β”œβ”€β”€ generation.py        # FLUX.1 / SDXL image generation
β”‚   β”‚   └── prompt_engineer.py   # RAG-enhanced prompt building
β”‚   β”œβ”€β”€ rag/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── vector_store.py      # ChromaDB vector operations
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ database.py          # SQLAlchemy ORM models
β”‚   β”‚   └── schemas.py           # Pydantic request/response schemas
β”‚   └── utils/
β”‚       β”œβ”€β”€ __init__.py
β”‚       └── storage.py           # Local file storage
β”œβ”€β”€ storage/
β”‚   β”œβ”€β”€ images/                  # Uploaded & generated images
β”‚   β”‚   β”œβ”€β”€ uploaded/
β”‚   β”‚   └── generated/
β”‚   β”œβ”€β”€ chroma/                  # ChromaDB persistence
β”‚   └── app.db                   # SQLite database
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env                         # Environment variables (create from .env.example)
β”œβ”€β”€ .env.example                 # Template for env vars
└── README.md

πŸš€ Quick Start

1. Install Dependencies

cd image-rag-pipeline
pip install -r requirements.txt

2. Configure Environment

# Copy the example env file
cp .env.example .env

# Edit .env and add your Hugging Face API token
# Get your token from: https://huggingface.co/settings/tokens

Set your HF_API_TOKEN in the .env file:

HF_API_TOKEN=hf_your_actual_token_here

3. Run the Server

uvicorn app.main:app --reload --port 8000

The server will start at http://localhost:8000. Visit http://localhost:8000/docs for the interactive API documentation (Swagger UI).

πŸ“‘ API Endpoints

POST /upload-image β€” Upload & Analyze Image

Upload an image to the RAG pipeline. The system will:

  1. Analyze it with a vision model (Qwen2-VL)
  2. Generate embeddings
  3. Store in the vector database
  4. Find similar images

Request:

curl -X POST http://localhost:8000/upload-image \
  -F "file=@my_photo.jpg"

Response:

{
  "image_id": "a1b2c3d4-...",
  "description": "A vibrant sunset over a mountain landscape with warm orange and purple tones...",
  "image_url": "/images/uploaded/a1b2c3d4_my_photo.jpg",
  "similar_images": [
    {
      "image_id": "e5f6g7h8-...",
      "description": "A mountain scene at dusk...",
      "image_url": "/images/uploaded/e5f6g7h8_sunset.png",
      "similarity_score": 0.8723,
      "source": "uploaded"
    }
  ],
  "message": "Image uploaded and indexed successfully. Found 1 similar images."
}

POST /generate-image β€” Generate Image with RAG

Generate a new image using the full RAG pipeline. The system retrieves similar images for context-aware generation.

Request:

curl -X POST http://localhost:8000/generate-image \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "a futuristic city at sunset with flying cars",
    "style": "cyberpunk",
    "top_k": 5
  }'

Response:

{
  "image_id": "x9y0z1a2-...",
  "image_url": "/images/generated/x9y0z1a2.png",
  "user_prompt": "a futuristic city at sunset with flying cars",
  "final_prompt": "A futuristic cyberpunk scene of a futuristic city at sunset with flying cars, inspired by: ..., neon glowing lights, synthwave aesthetic, dark atmosphere",
  "similar_context": [...],
  "message": "Image generated successfully. Used 3 similar images as RAG context."
}

Available Styles: detailed, digital_art, photorealistic, anime, cyberpunk, watercolor, fantasy, 3d_render

POST /search-similar β€” Similarity Search

Search the vector store for similar images by text or by image ID.

By text:

curl -X POST http://localhost:8000/search-similar \
  -H "Content-Type: application/json" \
  -d '{"query": "sunset over mountains", "top_k": 5}'

By image ID:

curl -X POST http://localhost:8000/search-similar \
  -H "Content-Type: application/json" \
  -d '{"image_id": "a1b2c3d4-...", "top_k": 5}'

Response:

{
  "query": "sunset over mountains",
  "results": [
    {
      "image_id": "...",
      "description": "...",
      "image_url": "/images/...",
      "similarity_score": 0.9124,
      "source": "uploaded"
    }
  ],
  "total_results": 3
}

GET /history β€” Generation History

Retrieve paginated generation history.

curl "http://localhost:8000/history?page=1&page_size=10"

Response:

{
  "items": [
    {
      "id": "...",
      "user_input": "a futuristic city at sunset",
      "input_type": "text",
      "vision_output": null,
      "final_prompt": "...",
      "generated_image_url": "/images/generated/...",
      "similarity_score": 0.85,
      "created_at": "2025-01-15T10:30:00Z"
    }
  ],
  "total": 42,
  "page": 1,
  "page_size": 10
}

GET / β€” Health Check

curl http://localhost:8000/

GET /stats β€” System Statistics

curl http://localhost:8000/stats

🧠 How the RAG Pipeline Works

  1. Upload Phase: When you upload an image, the vision model (Qwen2-VL) creates a detailed textual description. This description is embedded into a 384-dimensional vector and stored in ChromaDB alongside the image metadata.

  2. Generation Phase: When you request image generation:

    • Your text prompt is converted to an embedding
    • ChromaDB finds the most similar images from your stored collection
    • The prompt engineer combines your input with RAG-retrieved context
    • The final optimized prompt is sent to the image generation model
    • The generated image and its embedding are stored back into the RAG system
  3. Learning: Over time, as more images are uploaded and generated, the RAG system builds a richer knowledge base. Each new generation benefits from all previous interactions, making the system increasingly context-aware.

βš™οΈ Configuration

All settings are managed via environment variables (.env file):

Variable Default Description
HF_API_TOKEN (required) Hugging Face API token
DATABASE_URL sqlite+aiosqlite:///./storage/app.db Database connection string
STORAGE_DIR ./storage/images Image storage directory
CHROMA_PERSIST_DIR ./storage/chroma ChromaDB persistence directory
VISION_MODEL Qwen/Qwen2-VL-7B-Instruct Vision-language model
EMBEDDING_MODEL sentence-transformers/all-MiniLM-L6-v2 Embedding model
GENERATION_MODEL black-forest-labs/FLUX.1-schnell Image generation model

πŸ”§ Models Used

Component Model Purpose
Vision Qwen/Qwen2-VL-7B-Instruct Image β†’ Text description
Embedding sentence-transformers/all-MiniLM-L6-v2 Text β†’ 384-dim vector
Generation black-forest-labs/FLUX.1-schnell Text β†’ Image
Fallback Gen Pollinations AI Free image generation (no API key)
Fallback Vision Salesforce/blip-image-captioning-large Basic image captioning

πŸ“ License

This project is for educational and development purposes.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support