CoolFace
Apppublic

meetbatra/code-solver

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
App README

Autonomous Quiz Solver Agent

![License: MIT](https://opensource.org/licenses/MIT) ![Python 3.12+](https://www.python.org/downloads/) ![FastAPI](https://fastapi.tiangolo.com/)

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

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:

  1. 1.FastAPI Server (main.py): Handles incoming POST requests, validates secrets, triggers agent in background
  2. 2.LangGraph Agent (agent.py): State machine coordinating tool usage and decision-making
  3. 3.Tools Package (tools/): Five modular tools for different capabilities
  4. 4.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

bash
git clone https://github.com/meetbatra/quiz-solver.git
cd quiz-solver

Step 2: Install Dependencies

Using uv (Recommended)
bash
# Install uv if needed
pip install uv

# Sync dependencies
uv sync

# Install Playwright browser
uv run playwright install chromium
Using pip
bash
# 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:

env
# Your credentials
EMAIL=your.email@example.com
SECRET=your_secret_string

# Google Gemini API Key
GOOGLE_API_KEY=your_gemini_api_key_here

Getting a Gemini API Key

  1. 1.Visit Google AI Studio
  2. 2.Create a new API key
  3. 3.Copy it to your .env file

๐Ÿš€ Usage

Local Development

Start the FastAPI server:

bash
# Using uv
uv run main.py

# Using standard Python
python main.py

The server starts on http://0.0.0.0:7860

Testing the Endpoint

Send a POST request:

bash
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:

json
{
  "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:

json
{
  "email": "your.email@example.com",
  "secret": "your_secret_string",
  "url": "https://example.com/quiz"
}

Responses:

Status CodeDescription
200Secret verified, agent started
400Invalid JSON payload
403Invalid secret

GET /healthz

Health check endpoint.

Response:

json
{
  "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 networkidle before 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 run for 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 url field 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

bash
docker build -t quiz-solver .

Run the Container

bash
docker run -p 7860:7860 \
  -e EMAIL="your.email@example.com" \
  -e SECRET="your_secret_string" \
  -e GOOGLE_API_KEY="your_api_key" \
  quiz-solver

Deploy to HuggingFace Spaces

  1. 1.Create a new Space with Docker SDK
  2. 2.Push this repository to your Space
  3. 3.Add secrets in Space settings:
  4. 4.EMAIL
  5. 5.SECRET
  6. 6.GOOGLE_API_KEY
  7. 7.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: agent and tools (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

  1. 1.LangGraph over Sequential Execution: Enables flexible routing and complex decision-making
  2. 2.Background Processing: Prevents HTTP timeouts for long-running quiz chains
  3. 3.Tool Modularity: Each tool is independent and testable
  4. 4.Rate Limiting: Prevents API quota exhaustion (9 req/min for Gemini) via InMemoryRateLimiter (agent.py:29-33)
  5. 5.Code Execution via subprocess: Isolates code execution for safety (run_code.py:50-56)
  6. 6.Playwright for Scraping: Handles JavaScript-rendered pages (web_scraper.py:32-42)
  7. 7.uv for Dependencies: Fast package resolution and installation (add_dependencies.py:22-27)
  8. 8.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.