A FastAPI-based REST API for AI-powered dream analysis and interpretation with subscription management and image generation capabilities.
- Dream Analysis: AI-powered dream interpretation using Gemini
- Image Generation: Generate visual representations of dreams
- Audio Transcription: Convert voice recordings to text
- Text-to-Speech: Convert text responses to audio
- Subscription Management: Google Play subscription verification
- User Management: Supabase-based user authentication and profiles
- Free Trial System: Limited free access with upgrade options
dreamsense/
βββ app.py # Main FastAPI application
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker configuration
βββ utils/ # Reusable utilities
β βββ __init__.py
β βββ auth.py # Authentication utilities
β βββ validators.py # Request validation utilities
β βββ error_handler.py # Error handling utilities
β βββ config.py # Configuration management
βββ schemas/ # Pydantic models
β βββ __init__.py
β βββ requests.py # Request schemas
β βββ responses.py # Response schemas
βββ services/ # Business logic services
β βββ supabase.py # Supabase database operations
β βββ google_cloud_utils.py # Google Cloud utilities
β βββ google_cloud.py # Google Cloud integration
β βββ purchase_verification.py # Purchase verification
β βββ subscription_service.py # Subscription management
β βββ tts_service.py # Text-to-speech service
βββ routes/ # API route handlers
βββ routes.py # Main API routes
βββ transcribe.py # Audio transcription routes
βββ send_dreams.py # Dream analysis logic
βββ generate_images.py # Image generation logic
-
Clone the repository
git clone <repository-url> cd dreamsense
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.env
file with the following variables:# Supabase Configuration SUPABASE_URL=your_supabase_url SUPABASE_KEY=your_supabase_key # Google Cloud Configuration GOOGLE_SERVICE_ACCOUNT_JSON_B64=your_base64_encoded_service_account_json # API Keys GEMINI_API_KEY=your_gemini_api_key ELEVENLABS_API_KEY=your_elevenlabs_api_key # App Configuration APP_NAME=DreamSense API APP_VERSION=1.0.0 DEBUG=false # Subscription Configuration PACKAGE_NAME=com.dreamsense.app SUBSCRIPTION_ID=dreamsense_pro_1 FREE_TRIAL_DREAMS=2 # Test User Configuration TEST_USER_ID=9e90cd1a-f665-47fb-9903-1b03285e9f6d
-
Run the application
uvicorn app:app --reload
The API documentation is automatically generated and available at:
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
All endpoints are prefixed with /api/v1
Most endpoints require authentication via Bearer token in the Authorization header:
Authorization: Bearer <your_access_token>
GET /api/v1/health
- Health check endpointGET /api/v1/
- Root endpointGET /api/v1/google-cloud-health
- Google Cloud credentials health check
GET /api/v1/generate-token
- Generate test access token
POST /api/v1/send-dream
- Analyze and interpret a dream{ "query": "I was flying over a city", "purchase_token": "com.dreamsense.app.subscription" }
POST /api/v1/generate-image
- Generate an image from a prompt{ "prompt": "A detailed image of a flying person over a city" }
POST /api/v1/verify-subscription
- Verify user subscription status{ "purchase_token": "your_purchase_token" }
The API includes a special test user feature that grants premium access to a specific user ID. This is useful for development and testing purposes.
- Test User ID: Configurable via
TEST_USER_ID
environment variable - Default Value:
9e90cd1a-f665-47fb-9903-1b03285e9f6d
- Access Level: Unlimited premium access (no subscription verification required)
- Use Case: Development, testing, and debugging
When a user with the test user ID makes any request, they automatically receive premium access without needing to verify their subscription with Google Play.
POST /api/v1/transcribe
- Transcribe audio file (multipart/form-data)POST /api/v1/tts
- Convert text to speech{ "text": "Your dream interpretation text" }
- Added Pydantic models for all request and response schemas
- Automatic validation of request payloads
- Consistent response formats across all endpoints
- Created
AuthService
for centralized token verification - Dependency injection for authentication requirements
- Consistent error handling for authentication failures
- Extracted common utilities into reusable components
- Consolidated Google Cloud services to eliminate duplication
- Created centralized configuration management
- Consistent error response formats
- Centralized error handling utilities
- Proper HTTP status codes for different error types
- Type-safe configuration using Pydantic Settings
- Environment variable validation
- Centralized configuration access
- Organized endpoints with proper prefixes and tags
- Clear separation of concerns
- Improved API documentation
Run the test suite:
pytest test_apis.py
Build and run with Docker:
docker build -t dreamsense-api .
docker run -p 8000:8000 dreamsense-api
Variable | Description | Required |
---|---|---|
SUPABASE_URL |
Supabase project URL | Yes |
SUPABASE_KEY |
Supabase service key | Yes |
GOOGLE_SERVICE_ACCOUNT_JSON_B64 |
Base64 encoded Google service account JSON | Yes |
GEMINI_API_KEY |
Google Gemini API key | Yes |
ELEVENLABS_API_KEY |
ElevenLabs API key | Yes |
PACKAGE_NAME |
Android app package name | No (default: com.dreamsense.app) |
SUBSCRIPTION_ID |
Google Play subscription ID | No (default: dreamsense_pro_1) |
FREE_TRIAL_DREAMS |
Number of free trial dreams | No (default: 2) |
TEST_USER_ID |
Test user ID for premium access | No (default: 9e90cd1a-f665-47fb-9903-1b03285e9f6d) |
- Bearer token authentication
- Request payload validation
- Environment variable protection
- Error message sanitization
- Subscription verification
All API responses follow a consistent format:
{
"success": true,
"message": "Operation completed successfully",
"data": {...}
}
{
"success": false,
"message": "Error description",
"type": "error_type",
"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.