An automated grading system that extracts questions and answers from images, stores them in MongoDB, and uses BAML to grade student responses.
This system automates the grading process for exams by:
- Extracting exam questions from images
- Extracting student answers from images
- Storing both in MongoDB collections
- Matching questions with corresponding answers
- Grading answers using BAML's AI-powered grading function
- Storing grading results in MongoDB for future reference
ACE-V1/
├── Python/ # Python source code directory
│ ├── Extract_Text.py # Functions for extracting text from images
│ ├── Grade.py # Functions for grading student answers
│ ├── baml_src/ # BAML source files
│ │ ├── Grade.baml # BAML grading function definitions
│ │ ├── Question_Extraction.baml # BAML question extraction definitions
│ │ └── generators.baml # BAML generator definitions
│ ├── Questions.png # Sample exam questions image
│ ├── Answers_name.png # Sample student answers image
│ └── Bad_answers.png # Alternative student answers image
├── main.py # Main entry point for the application
├── setup_env.py # Script to set up environment variables
├── .gitignore # Git ignore file
├── requirements.txt # Python dependencies
└── README.md # This file
- Image-to-Text Extraction: Converts exam questions and student answers from images to structured data
- MongoDB Integration: Stores and retrieves data from MongoDB collections
- AI-Powered Grading: Uses BAML to grade student answers with configurable toughness levels
- Detailed Feedback: Provides explanations for each graded answer
- Organized Output: Clear, well-formatted console output with visual separators
- Latest Data Retrieval: Always uses the most recently added data from MongoDB collections
- Comprehensive Grading: Evaluates answers based on correctness and explanation quality
- Python 3.8+
- MongoDB Atlas account
- BAML client (
baml-client) - PIL/Pillow for image processing
- pymongo for MongoDB operations
- API keys for OpenAI and DeepSeekor (see Setup section)
- Node.js 18+ and npm
- Python 3.8+ (with
python3on your PATH) - MongoDB Atlas connection string
- API keys for the LLM providers you intend to use (OpenRouter, OpenAI, etc.)
-
Clone the repository:
git clone <repository-url> cd ACE-V1
-
Run the automated setup script (macOS/Linux/Git Bash):
./setup-dev.sh
This script will:
- Install npm dependencies in the root workspace,
client, andserver - Create
Python/.venvand install Python requirements (includingbaml-py) - Remind you to populate any missing
.envfiles
Windows PowerShell: run
bash setup-dev.shinstead. - Install npm dependencies in the root workspace,
-
Populate environment files:
- Copy
client/.env.example→client/.envand fill in the values - Copy
server/.env.example→server/.env - Create/update
Python/.envwith your API keys and MongoDB URI
- Copy
-
(Optional) Review
Grade.pyandExtract_Text.pyif you need to adjust default MongoDB configuration or API clients.
After the one-time bootstrap, start the full stack with:
npm run devThis command launches:
- The React client on http://localhost:5173 (or another port if 5173 is in use)
- The Node.js server on http://localhost:5001/api
If you need to run the client or server individually:
# Run only the client
npm run client
# Run only the server
npm run serverMake sure you have the following environment files configured:
/client/.env- For Clerk authentication and API URL:
VITE_CLERK_PUBLISHABLE_KEY=your_clerk_publishable_key
VITE_CLERK_SECRET_KEY=your_clerk_secret_key
VITE_API_BASE_URL=http://localhost:5001/api
/server/.env- For MongoDB connection:
PORT=5001
MONGODB_URI=your_mongodb_connection_string
Run the main script to process and grade exam answers:
python main.pyThe script will:
- Add exam questions to MongoDB (if not commented out)
- Add student answers to MongoDB
- Grade the student answers using BAML
- Save the grading results to MongoDB
You can modify main.py to:
- Comment out specific steps (e.g., skip adding questions if they're already in the database)
- Change the image files used for questions and answers
- Adjust the grading toughness level
The system uses three collections in the BadriNath-Org database:
-
Exam-Questions: Stores exam questions with categories and point values
- Structure:
{ category: string, questions: [{ question: string, points: number }] }
- Structure:
-
Answers: Stores student answers with student information
- Structure:
{ name: string, student_id: string, autograde: boolean, answers: [{ category: string, answers: [{ true_or_false: boolean, answer: string }] }] }
- Structure:
-
Grades: Stores grading results with detailed feedback
- Structure:
{ student_name: string, student_id: string, grades: { grade: [{ points: number, explanation: string }] }, total_points: number, percentage: number }
- Structure:
The system grades answers by:
- Retrieving the latest student answers and exam questions from MongoDB (sorted by _id in descending order)
- Matching questions with corresponding answers by their index position
- Sending the combined data to BAML's GradeQuestion function with a specified toughness level
- Processing the grading results and calculating total scores
- Storing the results in the Grades collection with timestamps
- Toughness Level: Adjust the toughness level (1-5) when calling
grade_student_answers()to make grading more or less strict - Output Format: Modify print statements in the code to change output formatting
- Database Connection: Update MongoDB connection details as needed
- Image Sources: Change the image files used for questions and answers
The MongoDB connection URI and API keys contain sensitive credentials. For production use, it's recommended to:
- Use environment variables to store credentials (already implemented with the .env file)
- Implement proper access controls for your MongoDB instance
- Never commit credentials to version control (the .gitignore file is configured to exclude .env files)
- MongoDB Connection Issues: Verify your network connection and MongoDB Atlas credentials
- Image Processing Errors: Ensure the image files exist and are readable
- BAML Errors: Check that your API keys are correctly set in the .env file
- Grading Issues: Verify that the question and answer formats match the expected structure
[Your License Here]
[Your Contact Information]