An API to evaluate student answers using Google's Generative AI. This project uses a FastAPI backend to process evaluation requests, interacts with a database to store questions and answers, and leverages the Google Gemini API to check if the provided answers are correct.
- Bulk Evaluation: Trigger an evaluation for all answers in the database.
- Subject-Specific Evaluation: Trigger an evaluation for all answers related to a specific subject.
- Background Processing: Evaluations run as background tasks to prevent blocking the API.
- AI-Powered: Uses Google's Gemini API to determine the correctness of answers.
- Database Logging: Logs request metadata, including token usage and duration, for each evaluation.
.
├── app
│ ├── __init__.py
│ ├── crud.py # Database Create, Read, Update, Delete operations
│ ├── database.py # Database engine and session setup
│ ├── main.py # FastAPI application and endpoints
│ ├── models.py # SQLAlchemy database models
│ ├── schemas.py # Pydantic data validation schemas
│ └── services.py # Logic for interacting with the Google Gemini AI
├── .gitignore
├── readme.md
└── requirements.txt
- Python 3.8+
- pip
- A running PostgreSQL database instance
-
Clone the repository
git clone <your-repository-url> cd <your-repository-name>
-
Create and activate a virtual environment
-
macOS/Linux:
python3 -m venv venv source venv/bin/activate -
Windows:
python -m venv venv .\venv\Scripts\activate
-
-
Install dependencies
Install all the required Python packages from the
requirements.txtfile.pip install -r requirements.txt
-
Set up environment variables
Create a file named
.envin the root directory of the project. This file will store your secret keys and database connection string. The.gitignorefile is already configured to ignore this file.Your
.envfile should contain the following:# Your connection string for the PostgreSQL database DATABASE_URL="postgresql://user:password@host:port/dbname" # Your API key for the Google Gemini API GEMINI_API_KEY="your-gemini-api-key"
DATABASE_URL: The application uses this to connect to your database.GEMINI_API_KEY: The application needs this to send requests to the Gemini API for answer evaluation.
Once the setup is complete, you can run the FastAPI application using uvicorn. The --reload flag will automatically restart the server whenever you make changes to the code.
uvicorn app.main:app --reloadThe API will be available at http://127.0.0.1:8000.
The interactive API documentation (powered by Swagger UI) will be available at http://127.0.0.1:8000/docs.
-
Triggers a background task to evaluate all answers for all questions currently in the database.
- Response
202 Accepted:{ "message": "Evaluation process started in the background. The `status` field in the `task_answers` table will be updated upon completion." }
- Response
-
Triggers a background task to evaluate all answers for questions belonging to a specific subject.
- Path Parameter:
subject_id(UUID): The unique identifier for the subject.
- Response
202 Accepted:{ "message": "Evaluation process for subject {subject_id} started in the background. The `status` field in the `task_answers` table will be updated upon completion." }
- Path Parameter:
-
Retrieves the current evaluation status of all answers that have been processed by the AI.
- Response
200 OK:[ { "task_answer_id": "uuid-of-the-answer", "correct": true }, { "task_answer_id": "another-uuid-of-an-answer", "correct": false } ]
- Response