This guide provides step-by-step instructions on how to set up your local development environment, start the Django web server, and test the core functionalities (like the Google Cloud Vertex AI chatbots).
Before starting the server or testing scripts, ensure your Python virtual environment is activated and dependencies are installed.
Open your terminal and run:
# 1. Activate the virtual environment (Windows PowerShell)
.\.venv\Scripts\Activate.ps1
# 2. Install all required dependencies
pip install -r requirements.txtThis project uses a local SQLite database (db.sqlite3) for MVP development. If you pull new model changes, you need to apply them to the database.
Run the following commands:
# Generate the migration scripts based on models.py
python manage.py makemigrations api
python manage.py makemigrations portal
# Apply the migrations to build the tables in db.sqlite3
python manage.py migrate
# (Optional) Create an admin account so you can log into the Django Admin panel
python manage.py createsuperuserOnce the database is set up, you can start the local development web server.
Run the server:
python manage.py runserverWhere to view it:
- Web Portal: Open your browser and navigate to
http://localhost:8000/portal/login/ - Django Admin Backend: Navigate to
http://localhost:8000/admin/
Before integrating AI heavily into the Django views, we test it in an isolated standalone script: test_vertex.py.
You must authenticate your local terminal with Google Cloud.
# 1. Install Google Cloud SDK (CLI) if you haven't already
# 2. Login to your Google account with application default credentials:
gcloud auth application-default login(Note: test_vertex.py currently hardcodes the credential path so it works out-of-the-box, but standard gcloud login is best practice).
We have two AI personas to test: The Compliance Bot (RAG) and the Report Generation Bot (Structured JSON output).
- Open
test_vertex.pyin your editor. - Scroll to the bottom of the file to the
if __name__ == "__main__":block. - To test the Compliance Bot:
test_compliance_bot() # test_report_bot()
- To test the Report Bot:
# test_compliance_bot() test_report_bot()
- Run the file in your terminal:
python test_vertex.py
- Missing Module (
ModuleNotFoundError: No module named 'vertexai'): This means your virtual environment is either not activated, or the package wasn't installed. Make sure.venvshows in your terminal prompt, then runpip install google-cloud-aiplatform. - Google Auth Errors: Ensure you have run
gcloud auth application-default loginOR that theGOOGLE_APPLICATION_CREDENTIALSpath insidetest_vertex.pypoints to a valid JSON service account key.
This project is packaged with a Dockerfile and is designed to be deployed to Google Cloud Run for a serverless, scalable backend.
Before deploying to production, make sure to update your requirements.txt:
- Uncomment
gunicorn(required for serving the app in the Docker container). - Uncomment
psycopg2-binaryif you are switching from the local SQLite database to a Google Cloud SQL (PostgreSQL) instance.
You can build and deploy the container directly using the gcloud CLI. Ensure you are in the root directory (where the Dockerfile is located).
# 1. Set your project ID
gcloud config set project YOUR_PROJECT_ID
# 2. Build the Docker image and submit it to Google Cloud Build, then deploy to Cloud Run
gcloud run deploy finsight-backend `
--source . `
--region asia-east1 `
--allow-unauthenticated `
--port 8080 `
--set-env-vars="DEBUG=False,SECRET_KEY=your_production_secret"In production, Cloud Run should not use the local db.sqlite3 because Cloud Run containers are stateless (data will be lost when the container shuts down).
- Create a Cloud SQL for PostgreSQL instance in Google Cloud Console.
- When deploying to Cloud Run, add the Cloud SQL connection and your database URL. Add this to your
gcloud run deploycommand:--add-cloudsql-instances="YOUR_PROJECT_ID:asia-east1:YOUR_INSTANCE_NAME" ` --set-env-vars="DATABASE_URL=postgres://db_user:db_password@/db_name?host=/cloudsql/YOUR_PROJECT_ID:asia-east1:YOUR_INSTANCE_NAME"
Since Cloud Run is serverless, you cannot easily run python manage.py migrate directly on the server. The best ways to apply database migrations in production are:
- Locally via Cloud SQL Auth Proxy: Connect your local machine to the production database and run
python manage.py migrate. - Cloud Run Jobs: Create a Cloud Run Job that executes
python manage.py migrateand run it during your CI/CD pipeline before deploying the main service.