A FastAPI-based intelligent image generation application that uses DSPy optimization pipeline with LLM evaluation to iteratively improve image generation prompts. The system combines Gemini 2.5 Pro for evaluation and FAL AI Imagen4 for high-quality image generation.
- DSPy Optimization Pipeline: Iterative prompt improvement using structured evaluation
- Multi-Model Support: Integration with various AI image generation models
- LLM Evaluation: Gemini 2.5 Pro provides detailed feedback on generated images
- RAG-Enhanced Optimization: Learn from historical successful prompts
- Web Interface: Clean, responsive UI for image generation and history viewing
- Comprehensive Logging: CSV-based tracking of all generations with detailed metrics
text_to_image_generation/
├── app/ # Main FastAPI application
│ ├── main.py # FastAPI server entry point
│ ├── data/ # Configuration and templates
│ ├── imagesdata/ # Generated images and CSV logs
│ ├── models/ # Data models and managers
│ ├── routers/ # API route handlers
│ ├── services/ # Business logic and DSPy optimization
│ ├── static/ # CSS and JavaScript files
│ └── templates/ # HTML templates
├── modelexperiment/ # Multi-model testing tools
├── optimization/ # RAG-based prompt optimization
└── README.md # This file
main.py- FastAPI server with Jinja2 templates and static file servingdata/- Contains prompt templates and configuration filesimagesdata/- Storage for generated images and CSV logs with evaluation data
image_manager.py- Handles image storage, base64 conversion, and CSV loggingprompt_manager.py- Manages prompt templates and formatting
image_generator_router.py- HTTP request routing and error handling for image generation endpoints
dspy_optimization.py- Core DSPy optimization pipeline with iterative improvementimage_generator_service.py- Business logic for image generation workflow
templates/image_generator.html- Main web interfacestatic/style.css- Styling for the web interfacestatic/script.js- Frontend JavaScript for API interactions
- FastAPI - Modern web framework for building APIs
- DSPy - Framework for optimizing language model pipelines
- FAL AI - Image generation service (Imagen4)
- Google Gemini 2.5 Pro - LLM for image evaluation and feedback
- FAISS - Vector database for similarity search
- Upstage - Language model for RAG optimization
-
Clone the repository
git clone <repository-url> cd text_to_image_generation
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables Create a
.envfile with:FAL_KEY=your_fal_ai_api_key GOOGLE_API_KEY=your_gemini_api_key UPSTAGE_API_KEY=your_upstage_api_key -
Run the application
cd app python main.py -
Access the web interface Open
http://localhost:8000in your browser
- Enter your image description in the text input
- Click "Generate Image" to start the DSPy optimization process
- View the iterative improvements and final result
- Browse generation history and evaluation metrics
POST /api/generate_image- Generate image with DSPy optimizationGET /api/get_images- Retrieve image generation historyGET /api/get_csv_log- Download CSV log of all generations
- Initial Generation - Create image from user prompt
- LLM Evaluation - Gemini 2.5 Pro evaluates image quality across multiple dimensions
- Feedback Analysis - Identify areas for improvement
- Prompt Refinement - Generate improved prompt based on feedback
- Iteration - Repeat up to 5 times until optimal result
- Subject accuracy and presence
- Art type/medium matching
- Art style consistency
- Art movement alignment
- Conflict detection
- Overall quality score (1-10)
Test and compare different AI image generation models with the same prompts. See modelexperiment/README.md for details.
Enhance prompt generation using historical data and few-shot learning. See optimization/README.md for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.