AI Portrait Studio is a web application that allows users to generate custom AI-powered portraits using advanced machine learning techniques. Users can upload images, train custom AI models, and create unique portraits based on their specifications.
This application combines the power of React for the frontend, Firebase for authentication and data storage, and integrates with Stripe for payment processing. It leverages AI technologies to offer a unique and personalized portrait creation experience.
.
├── src
│ ├── components
│ ├── contexts
│ ├── lib
│ │ ├── fai
│ │ └── firebase
│ ├── pages
│ └── utils
├── .env
├── package.json
├── vite.config.js
└── vercel.json
src/App.jsx: Main application component and routing setupsrc/pages/Create.jsx: Core functionality for creating and generating portraitssrc/lib/firebase/stripePayments.js: Firebase-based payment processingpackage.json: Project dependencies and scriptsvite.config.js: Vite build tool configurationvercel.json: Vercel deployment configuration
- Firebase Authentication: User management and authentication
- Firestore: Data storage for user models, portraits, and payment processing
- Stripe: Payment processing integration
- Fal AI: AI model training and image generation
Prerequisites:
- Node.js (v14 or later)
- npm (v6 or later)
Steps:
- Clone the repository
- Navigate to the project directory
- Install dependencies:
npm install - Create a
.envfile in the root directory and add the necessary environment variables:VITE_FIREBASE_API_KEY=your_firebase_api_key VITE_FIREBASE_AUTH_DOMAIN=your_firebase_auth_domain VITE_FIREBASE_PROJECT_ID=your_firebase_project_id VITE_FIREBASE_STORAGE_BUCKET=your_firebase_storage_bucket VITE_FIREBASE_MESSAGING_SENDER_ID=your_firebase_messaging_sender_id VITE_FIREBASE_APP_ID=your_firebase_app_id VITE_FIREBASE_MEASUREMENT_ID=your_firebase_measurement_id VITE_STRIPE_PUBLIC_KEY=your_stripe_public_key
- Start the development server:
npm run dev - Open your browser and navigate to
http://localhost:3000
- Firebase: Update the Firebase configuration in
src/lib/firebase/config.jsif needed - Stripe: Ensure the Stripe public key is correctly set in the
.envfile
-
User Authentication:
import { useAuth } from '../contexts/AuthContext'; const { user, signOut } = useAuth();
-
Creating a Portrait:
const handleTrain = async () => { // Implementation in src/pages/Create.jsx }; const handleGenerate = async () => { // Implementation in src/pages/Create.jsx };
-
Handling Payments:
// Firebase listener for payment processing in src/lib/firebase/stripePayments.js export const listenForPayments = (app, userId) => { // Implementation details };
-
Firebase Authentication:
import { auth } from '../lib/firebase/config'; const user = auth.currentUser;
-
Firestore Data Access:
import { db } from '../lib/firebase/config'; import { doc, getDoc, updateDoc } from 'firebase/firestore'; const userRef = doc(db, 'users', userId); const userDoc = await getDoc(userRef);
-
Stripe Integration:
import { loadStripe } from '@stripe/stripe-js'; const stripePromise = loadStripe(import.meta.env.VITE_STRIPE_PUBLIC_KEY);
- Run linter:
npm run lint
-
Issue: Firebase authentication not working
- Confirm Firebase configuration in
src/lib/firebase/config.js - Verify Firebase project settings in the Firebase console
- Confirm Firebase configuration in
-
Issue: Portrait generation fails
- Check Fal AI API credentials and quota
- Ensure user has sufficient credits
- Review server logs for specific error messages
-
Issue: Payment processing not updating user credits
- Verify the
listenForPaymentsfunction is properly set up and running - Check Firestore rules to ensure proper access to the payments subcollection
- Review Firebase console logs for any errors in the payment listener function
- Verify the
Debugging:
- Enable verbose logging in
src/pages/Create.jsx:console.log('Debug: ', { uploadedImages, selectedModelId, formData });
- Check browser console and Firebase console logs for error messages
- Use Firebase Console to inspect Firestore data and Authentication status
Performance Optimization:
- Monitor Firestore read/write operations
- Optimize image uploads by compressing images before upload
- Implement caching for frequently accessed data
The AI Portrait Studio application follows a client-server architecture with integration to external services. Here's an overview of the data flow:
-
User Authentication: Client -> Firebase Authentication -> Client
-
Model Training: Client -> Server -> Fal AI API -> Server -> Firestore
-
Portrait Generation: Client -> Server -> Fal AI API -> Server -> Firestore -> Client
-
Payment Processing: Client -> Stripe -> Firebase (Payments Subcollection) -> Firestore (User Credits Update)
+--------+ +--------+ +-------------+
| Client |<-->| Server |<-->| Fal AI API |
+--------+ +--------+ +-------------+
^ ^
| |
v v
+--------+ +----------+
| Stripe | | Firebase |
+--------+ +----------+
Notes:
- The client communicates with the server for AI operations and data management
- Firebase handles authentication, data storage, and payment processing
- Stripe processes payments, with Firebase monitoring the payments subcollection for successful transactions
- The Fal AI API is used for model training and image generation