meetbatra/code-solver
Autonomous Quiz Solver Agent
  
An intelligent, autonomous agent built with LangGraph and LangChain that solves multi-step quiz tasks involving web scraping, data processing, code execution, and API interactions. The system uses Google's Gemini 2.5 Flash model to orchestrate tool usage and make decisions autonomously.
๐ Table of Contents
- Overview
- Architecture
- Features
- Project Structure
- Installation
- Configuration
- Usage
- API Endpoints
- Tools & Capabilities
- Docker Deployment
- How It Works
- License
๐ Overview
This autonomous agent solves multi-step quiz tasks by:
- Web Scraping: Rendering JavaScript-heavy pages with Playwright
- Data Processing: Downloading and processing files (CSV, PDF, etc.)
- Code Execution: Generating and running Python code for data analysis
- API Integration: Submitting answers and following quiz chains
- Dependency Management: Installing required packages on-the-fly
The system receives quiz URLs via a REST API, navigates through multiple quiz pages, solves each task using LLM-powered reasoning with specialized tools, and submits answers back to evaluation servers.
๐๏ธ Architecture
The project uses a LangGraph state machine architecture:
โโโโโโโโโโโโโโโ
โ FastAPI โ โ Receives POST requests with quiz URLs
โ Server โ
โโโโโโโโฌโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ
โ LangGraph โ โ State machine with Gemini 2.5 Flash
โ Agent โ
โโโโโโโโฌโโโโโโโ
โ
โโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโ
โผ โผ โผ โผ โผ
[Scraper] [Download] [Run Code] [POST Req] [Add Deps]Key Components:
- FastAPI Server (
main.py): Handles incoming POST requests, validates secrets, triggers agent in background - LangGraph Agent (
agent.py): State machine coordinating tool usage and decision-making - Tools Package (
tools/): Five modular tools for different capabilities - LLM: Google Gemini 2.5 Flash with rate limiting (9 requests per minute)
โจ Features
- โ Autonomous multi-step problem solving: Chains together quiz tasks automatically
- โ Dynamic JavaScript rendering: Uses Playwright for client-side rendered pages
- โ Code generation & execution: Writes and runs Python code for data tasks
- โ Flexible data handling: Downloads files of any format
- โ
Self-installing dependencies: Automatically adds required Python packages via
uv - โ Retry logic: Retries failed attempts within 3-minute time limit
- โ Docker containerization: Ready for deployment
- โ Rate limiting: Respects API quotas with exponential backoff
๐ Project Structure
quiz-solver/
โโโ agent.py # LangGraph state machine & orchestration logic
โโโ main.py # FastAPI server with /solve endpoint
โโโ pyproject.toml # Project dependencies (uv configuration)
โโโ Dockerfile # Container image with Playwright & Chromium
โโโ .env # Environment variables (credentials)
โโโ LICENSE # MIT License
โโโ tools/
โ โโโ __init__.py # Tool exports
โ โโโ web_scraper.py # Playwright-based HTML renderer
โ โโโ run_code.py # Python code executor
โ โโโ download_file.py # File downloader
โ โโโ send_request.py # HTTP POST request tool
โ โโโ add_dependencies.py # Package installer via uv
โโโ LLMFiles/ # Working directory for downloads & code execution
โโโ README.md๐ฆ Installation
Prerequisites
- Python 3.12 or higher
- uv package manager (recommended)
- Git
Step 1: Clone the Repository
git clone https://github.com/meetbatra/quiz-solver.git
cd quiz-solverStep 2: Install Dependencies
Using uv (Recommended)
# Install uv if needed
pip install uv
# Sync dependencies
uv sync
# Install Playwright browser
uv run playwright install chromiumUsing pip
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # macOS/Linux
# .venv\Scripts\activate # Windows
# Install dependencies
pip install -e .
# Install Playwright browser
playwright install chromiumโ๏ธ Configuration
Environment Variables
Create a .env file in the project root:
# Your credentials
EMAIL=your.email@example.com
SECRET=your_secret_string
# Google Gemini API Key
GOOGLE_API_KEY=your_gemini_api_key_hereGetting a Gemini API Key
- Visit Google AI Studio
- Create a new API key
- Copy it to your
.envfile
๐ Usage
Local Development
Start the FastAPI server:
# Using uv
uv run main.py
# Using standard Python
python main.pyThe server starts on http://0.0.0.0:7860
Testing the Endpoint
Send a POST request:
curl -X POST http://localhost:7860/solve \
-H "Content-Type: application/json" \
-d '{
"email": "your.email@example.com",
"secret": "your_secret_string",
"url": "https://tds-llm-analysis.s-anand.net/demo"
}'Expected response:
{
"status": "ok"
}The agent runs in the background and solves the quiz chain autonomously.
๐ API Endpoints
POST /solve
Triggers the autonomous agent to solve quiz tasks.
Request Body:
{
"email": "your.email@example.com",
"secret": "your_secret_string",
"url": "https://example.com/quiz"
}Responses:
GET /healthz
Health check endpoint.
Response:
{
"status": "ok",
"uptime_seconds": 3600
}๐ ๏ธ Tools & Capabilities
The agent has access to five specialized tools:
1. Web Scraper (get_rendered_html)
- Uses Playwright to render JavaScript-heavy pages
- Waits for
networkidlebefore extracting content - Returns fully rendered HTML
- File:
tools/web_scraper.py:6
2. File Downloader (download_file)
- Downloads files from direct URLs
- Saves to
LLMFiles/directory - Supports any file format (PDF, CSV, images, etc.)
- File:
tools/download_file.py:6
3. Code Executor (run_code)
- Executes arbitrary Python code in subprocess
- Writes code to
LLMFiles/runner.py - Returns stdout, stderr, and exit code
- Runs via
uv runfor dependency management - File:
tools/run_code.py:21
4. POST Request (post_request)
- Sends JSON payloads to submission endpoints
- Includes automatic error handling
- Implements retry logic based on delay and correctness
- Strips
urlfield if answer is incorrect and within time limit - File:
tools/send_request.py:7
5. Dependency Installer (add_dependencies)
- Dynamically installs Python packages via
uv add - Enables agent to adapt to different task requirements
- Returns installation success/failure message
- File:
tools/add_dependencies.py:7
๐ณ Docker Deployment
Build the Image
docker build -t quiz-solver .Run the Container
docker run -p 7860:7860 \
-e EMAIL="your.email@example.com" \
-e SECRET="your_secret_string" \
-e GOOGLE_API_KEY="your_api_key" \
quiz-solverDeploy to HuggingFace Spaces
- Create a new Space with Docker SDK
- Push this repository to your Space
- Add secrets in Space settings:
EMAILSECRETGOOGLE_API_KEY- The Space will automatically build and deploy
๐ง How It Works
1. Request Reception
- FastAPI receives POST request with quiz URL (main.py:34)
- Validates secret against environment variables (main.py:46)
- Returns 200 OK immediately (main.py:51)
- Starts agent in background task (main.py:49)
2. Agent Initialization
- LangGraph creates state machine with two nodes:
agentandtools(agent.py:130-133) - Initial state contains quiz URL as user message (agent.py:152)
- System prompt guides agent behavior (agent.py:44-86)
3. Task Loop
The agent follows this cycle:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. LLM analyzes current state โ
โ - Reads page instructions โ
โ - Plans tool usage โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. Tool execution โ
โ - Scrapes/downloads data โ
โ - Runs analysis code โ
โ - Submits answer โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Response evaluation โ
โ - Checks correctness โ
โ - Extracts next URL โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. Decision โ
โ - New URL? โ Loop to step 1 โ
โ - No URL? โ Return "END" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ4. State Management
- All messages (user, assistant, tool) stored in state (agent.py:19-20)
- LLM uses full conversation history for context (agent.py:100)
- Recursion limit set to 5000 iterations (agent.py:15)
- Conditional routing based on tool calls or "END" signal (agent.py:107-129)
5. Completion
- Agent returns "END" when no new URL provided (agent.py:125-128)
- Background task completes
- Success message logged to console (agent.py:155)
๐ Key Design Decisions
- LangGraph over Sequential Execution: Enables flexible routing and complex decision-making
- Background Processing: Prevents HTTP timeouts for long-running quiz chains
- Tool Modularity: Each tool is independent and testable
- Rate Limiting: Prevents API quota exhaustion (9 req/min for Gemini) via InMemoryRateLimiter (agent.py:29-33)
- Code Execution via subprocess: Isolates code execution for safety (run_code.py:50-56)
- Playwright for Scraping: Handles JavaScript-rendered pages (web_scraper.py:32-42)
- uv for Dependencies: Fast package resolution and installation (add_dependencies.py:22-27)
- Retry Logic: Resubmits incorrect answers within 3-minute time limit (send_request.py:42-47)
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or issues, please open an issue on the GitHub repository.
