This project is a simple API for managing posts and comments with AI moderation. The API is built using FastAPI and Pydantic, and it includes features for user registration, login, post and comment management, AI-powered content moderation, and analytics. Additionally, a bot is included to generate posts and comments using AI and interact with the API.
.
├── alembic/
├── app/
│ ├── __pycache__/
│ ├── auth.py
│ ├── crud.py
│ ├── database.py
│ ├── deps.py
│ ├── main.py
│ ├── models.py
│ ├── moderation.py
│ ├── schemas.py
├── bot/
│ ├── __pycache__/
│ ├── bot.py
│ ├── config.py
│ ├── template_config.py
├── venv/
├── tests/
│ ├── test_main.py
│ ├── test_post.py
├── .gitignore
├── alembic.ini
├── init_db.py
├── readme
├── requirements.txt
└── test.db
Create and activate a Python virtual environment:
python -m venv venv
venvScriptsactivatepython3 -m venv venv
source venv/bin/activateInstall the necessary Python packages:
pip install -r requirements.txtEnsure you have a Google Cloud service account key file (JSON) and set the GOOGLE_APPLICATION_CREDENTIALS environment variable.
set GOOGLE_APPLICATION_CREDENTIALS=C:pathtoyourservice-account-file.jsonexport GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-file.json"Use Alembic to apply database migrations:
alembic upgrade headRun the following script to initialize the database:
python init_db.pyCreate a .env file in the project root and add the following variables:
GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-file.json"Add any other environment-specific variables you need for your FastAPI settings.
To start the FastAPI server, run:
uvicorn app.main:app --reloadThe API will be accessible at http://127.0.0.1:8000.
Root Endpoint: GET /
Returns a welcome message.
User Registration: POST /register/
Register a new user.
User Login: POST /login/
Obtain an access token for an authenticated user.
Create Post: POST /posts/
Create a new post.
Create Comment: POST /posts/{post_id}/comments/
Add a comment to a post.
Get Analytics: GET /api/comments-daily-breakdown?date_from=<YYYY-MM-DD>&date_to=<YYYY-MM-DD>
Get daily aggregated comment data within a date range.
Automatic Response to Comments: /comments/auto-response/
Endpoint and logic for enabling auto-response to comments after a delay.
The bot settings can be adjusted in the bot/config.py file, including:
To run the bot and start generating posts and comments, run:
python bot/bot.pyTo enable the bot to generate text content using Google's Generative AI, follow these steps:
Create a Google Cloud Project:
Enable the Generative AI API:
Obtain API Credentials:
Install the API Client Library:
pip install google-generativeaiConfigure the Application:
bot/config.py:
# bot/config.py
class Config:
API_URL = "http://localhost:8000" # URL of your FastAPI app
AI_API_URL = "https://ai.google.dev/generate-text" # Google's AI API endpoint
AI_API_KEY = "your_ai_key" # Replace with your Google AI API keyTests are provided for the post creation and analytics functions. To run the tests, execute:
pytestThis will execute tests for post creation, analytics, and other aspects of the application.
git checkout -b feature-branch).git commit -m 'Add some feature').git push origin feature-branch).For any additional questions or troubleshooting, please refer to the official documentation for FastAPI, SQLAlchemy, and Google Generative AI API. ``