Bash
Overview
The DesignAI Architecture Pipeline
+--------------------+ +----------------------+ +-------------------+
| Frontend | | Backend API | | External AI |
| (Next.js 14) | | (FastAPI + Python) | | Services |
| | | | | |
| /generate |---->| POST /api/generate |---->| Google Gemini |
| /recommendation | | POST /api/recommend | | (prompt enhance) |
| /deep-learning | | POST /api/dl/clip | | |
| /smart-prompt | | POST /api/evolve | | NanoBanana FLUX |
| /semantic-search | | GET /api/vsm-search | | (image synthesis) |
| /analytics | | GET /api/analytics | | |
| /dashboard | | GET /api/dashboard | | CLIP (PyTorch) |
+--------------------+ +----------------------+ | (similarity score)|
| +-------------------+
v
+-------------------+
| Data Layer |
| |
| PostgreSQL |
| (generation logs) |
| |
| Redis |
| (rate limit + |
| cache dashboard) |
| |
| Cloudflare R2 |
| (image storage) |
+-------------------+
Step One: Project Setup
Backend Dependencies
Text
Backend Project Structure
apidesignai/
├── main.py # FastAPI app, CORS, lifespan
├── core/
│ ├── config.py # Pydantic Settings (env-driven config)
│ ├── auth.py # JWT auth middleware
│ └── redis.py # Redis connection pool
├── database/
│ ├── session.py # Async SQLAlchemy engine
│ └── base.py # Declarative base
├── models/
│ ├── user.py # User model
│ ├── generation.py # GenerationHistory model
│ └── saved_design.py # SavedDesign model
├── routes/
│ ├── generate.py # POST /api/generate
│ ├── recommendation.py # POST /api/recommendation
│ ├── dashboard.py # GET /api/dashboard
│ ├── analytics.py # GET /api/analytics
│ ├── search.py # GET /api/search
│ ├── vsm_search.py # GET /api/vsm-search
│ ├── evolve.py # POST /api/evolve
│ ├── deep_learning.py # POST /api/dl/clip
│ ├── bi.py # GET /api/bi/export
│ ├── credits.py # GET/POST /api/credits
│ ├── ratings.py # POST /api/ratings
│ ├── limits.py # GET /api/limits/status/:feature
│ └── auth_route.py # POST /api/auth/login, /register
├── schemas/ # Pydantic request/response schemas
├── services/
│ ├── generate_service.py # Prompt enhance + image generate orchestrator
│ ├── prompt_service.py # Gemini prompt enhancement
│ ├── nanobanana_service.py # FLUX image generation (NanoBanana API)
│ ├── recommendation_service.py# Multimodal AI recommendation pipeline
│ ├── storage_service.py # Cloudflare R2 upload/delete
│ ├── clip_service.py # CLIP image-text similarity scoring
│ ├── lstm_service.py # LSTM-based prediction model
│ ├── genetic_prompt_service.py# Genetic algorithm prompt evolution
│ ├── fuzzy_credit_service.py # Fuzzy logic credit scoring
│ ├── rating_predictor_service.py # Rating prediction model
│ ├── vsm_service.py # Vector Space Model search
│ └── recommendation_service.py# AI product recommendation
└── alembic/ # Database migrations
Frontend Project Structure
designai/
├── app/
│ ├── page.tsx # Landing page
│ ├── generate/page.tsx # Text-to-image generator
│ ├── recommendation/page.tsx # AI design recommendation
│ ├── deep-learning/page.tsx # CLIP similarity scoring
│ ├── smart-prompt/page.tsx # Genetic prompt evolution
│ ├── semantic-search/page.tsx # VSM semantic search
│ ├── dashboard/page.tsx # User dashboard + gallery
│ ├── analytics/page.tsx # Analytics & charts
│ ├── admin/bi/page.tsx # Business intelligence / CSV export
│ ├── login/page.tsx # Auth (credentials + Google OAuth)
│ ├── register/page.tsx # Registration
│ └── api/
│ └── download/route.ts # Next.js proxy for R2 image download
├── components/
│ ├── Navbar.tsx
│ ├── Footer.tsx
│ └── SpotlightCard.tsx
└── .env.local
Step Two: Configuration with Pydantic Settings
Python
Step Three: Image Generation Pipeline
Text-Only Flow
User Prompt → Gemini Enhance → NanoBanana FLUX → R2 Upload → DB Save → Response
Multimodal Flow (Reference Image)
User Prompt + Image → Gemini Vision Analysis → Merged Prompt → NanoBanana FLUX → R2 Upload
Generate Route
Python
Step Four: AI Recommendation Pipeline
Upload Image → Gemini Vision → Select 3 Products → [FLUX Generate] (parallel) → R2 Upload × 3
Auto Mode (image-only): Gemini reads the style, color, and mood, then picks products autonomously.
Manual Mode (image + prompt): The user adds a direction (e.g., "make it gothic dark") that steers Gemini's product selection.
Python
Step Five: Cloudflare R2 Storage
Python
Step Six: CLIP Deep Learning — Image-Text Similarity
User provides image_url + prompt → CLIP encodes both → cosine similarity → score (0–100)
The score gives actionable feedback: low scores suggest the prompt needs more specificity; high scores confirm strong alignment between the visual output and the original intent.
Step Seven: Genetic Prompt Evolution
Base Prompt
→ Initial population (N variants)
→ Fitness evaluation (Gemini scores each variant)
→ Selection → Crossover → Mutation
→ Next generation
→ Best prompt returned
Python
Step Eight: Fuzzy Credit System
Input factors → Fuzzification → Fuzzy inference rules → Defuzzification → Credit cost
This allows the system to charge proportionally to actual resource consumption rather than a flat rate.
Step Nine: Dashboard with Redis Caching
Python
Step Ten: Image Download via Next.js Proxy
How It Works
Browser click Download
→ fetch("/api/download?url=<R2_URL>")
→ Next.js route.ts fetches image from R2 (server-side, no CORS)
→ Returns blob with Content-Disposition: attachment
→ Browser triggers file download ✅
Proxy Route
Typescript
Download Handler in Dashboard
Typescript
Step Eleven: Authentication
Typescript
Step Twelve: Rate Limiting with Redis
Python
API Endpoints Reference
Performance Notes
Running Locally
Backend
Bash
Frontend
Bash