Skip to content

Aksha4/Image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎨 Image Generative AI Using Deep Learning & Diffusion Models

A production-ready, beginner-friendly text-to-image generation system powered by Stable Diffusion, Hugging Face Diffusers, and FastAPI.

Python FastAPI Stable Diffusion License


πŸ“Œ Table of Contents


🧠 Introduction

This project implements a Text-to-Image Generative AI system using Stable Diffusion and the Hugging Face Diffusers library. Users can enter a natural language prompt (e.g., "a futuristic city at night, neon lights, cyberpunk style") and the system generates a high-quality image using the diffusion model pipeline.

The system is built with:

  • Backend: Python + FastAPI
  • Frontend: HTML5 + CSS3 + JavaScript (Vanilla)
  • AI Model: Stable Diffusion v2.1 via Hugging Face
  • Deployment: Docker, AWS EC2 / Google Cloud / Hugging Face Spaces

✨ Features

  • πŸ–ΌοΈ Text-to-Image Generation using Stable Diffusion 2.1
  • ⚑ FastAPI Backend with async support and Swagger UI
  • 🎨 Beautiful Responsive Frontend with loading animations
  • πŸ›‘οΈ Error Handling at API and UI level
  • 🐳 Docker Support for easy containerization
  • ☁️ Cloud Deployment guides for AWS, GCP, and Hugging Face Spaces
  • πŸ“– Well-Documented Code with inline comments
  • πŸ”’ Environment Variable Management via .env
  • πŸ“Š Model Architecture Explanation (CLIP + U-Net + VAE)

πŸ—οΈ Architecture

User Prompt (Text)
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         CLIP Text Encoder           β”‚  ← Converts text to embeddings
β”‚  (openai/clip-vit-large-patch14)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚  Text Embeddings
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      U-Net Diffusion Model          β”‚  ← Iterative denoising (50 steps)
β”‚   (Noise β†’ Denoised Latent Space)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚  Denoised Latents
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         VAE Decoder                 β”‚  ← Latents β†’ Pixel Space (Image)
β”‚   (Variational Autoencoder)         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
  Generated Image (PNG)

Components:

Component Role
CLIP Text Encoder Converts your text prompt into numerical embeddings
U-Net Iteratively denoises a random noise tensor guided by the text embeddings
VAE Decoder Decodes the denoised latent representation into a full-resolution image
Scheduler Controls the denoising steps (PNDM, DDIM, etc.)

πŸ“ Folder Structure

Image/
β”œβ”€β”€ backend/                    # FastAPI backend
β”‚   β”œβ”€β”€ main.py                 # App entry point
β”‚   β”œβ”€β”€ config.py               # Configuration & settings
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── generate.py         # /generate API route
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── diffusion_service.py # Stable Diffusion logic
β”‚   └── utils/
β”‚       └── image_utils.py      # Image processing helpers
β”œβ”€β”€ frontend/                   # Web UI
β”‚   β”œβ”€β”€ index.html              # Main HTML page
β”‚   β”œβ”€β”€ styles.css              # Responsive CSS with animations
β”‚   └── script.js               # API calls and UI logic
β”œβ”€β”€ outputs/                    # Generated images (auto-created)
β”œβ”€β”€ docs/
β”‚   └── ARCHITECTURE.md         # Deep technical architecture
β”œβ”€β”€ .env.example                # Environment variable template
β”œβ”€β”€ .gitignore
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ requirements.txt            # Python dependencies
└── README.md                   # This file

βš™οΈ Installation

Prerequisites

  • Python 3.10+
  • CUDA-enabled GPU (recommended) OR CPU (slower)
  • 8GB+ RAM / 4GB+ VRAM (GPU)
  • pip or conda

Step 1: Clone the Repository

git clone https://github.com/yourusername/image-gen-ai.git
cd image-gen-ai

Step 2: Create Virtual Environment

python -m venv venv

# On Windows:
venv\Scripts\activate

# On Linux/Mac:
source venv/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Configure Environment

cp .env.example .env
# Edit .env with your settings (HuggingFace token, etc.)

πŸš€ Running the Application

Start the Backend Server

uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000

Open the Frontend

Open frontend/index.html in your browser, or serve it:

python -m http.server 3000 --directory frontend

Then visit: http://localhost:3000

Access API Docs (Swagger UI)

Visit: http://localhost:8000/docs


πŸ“‘ API Endpoints

POST /generate

Generate an image from a text prompt.

Request Body:

{
  "prompt": "a beautiful sunset over the ocean, photorealistic",
  "negative_prompt": "blurry, low quality",
  "num_inference_steps": 50,
  "guidance_scale": 7.5,
  "width": 512,
  "height": 512,
  "seed": -1
}

Response:

{
  "success": true,
  "image_base64": "data:image/png;base64,iVBORw0KGgo...",
  "image_path": "outputs/generated_1234567890.png",
  "prompt": "a beautiful sunset over the ocean, photorealistic",
  "generation_time": 12.45,
  "parameters": { ... }
}

GET /health

Check server and model status.

GET /models

List available models.


🌐 Deployment

🐳 Docker Deployment

docker-compose up --build

☁️ AWS EC2 Deployment

  1. Launch a g4dn.xlarge instance (NVIDIA T4 GPU)
  2. Install CUDA drivers and Docker
  3. Clone the repo and run docker-compose up
  4. Configure Security Group to allow port 8000

🌩️ Google Cloud Platform

  1. Create a Compute Engine instance with NVIDIA GPU
  2. Use Cloud Run for serverless deployment
  3. Push Docker image to Artifact Registry

πŸ€— Hugging Face Spaces

  1. Create a new Space with Gradio SDK
  2. Upload your code
  3. Set HF_TOKEN in Space secrets
  4. The Space will auto-build and deploy

πŸ”§ Troubleshooting

Issue Solution
Out of memory (GPU) Reduce width/height to 512x512, enable enable_attention_slicing
Slow generation (CPU) Use torch_dtype=torch.float32, expect 5-10 minutes per image
Model download fails Set HF_TOKEN in .env for gated models
Port already in use Change port: --port 8001
CUDA not found Install CUDA 11.8+, reinstall torch with CUDA support

πŸ“œ License

MIT License β€” free to use, modify, and distribute.


πŸ™ Acknowledgements

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages