Niloy commited on
Commit
df532e4
·
0 Parent(s):

Initial commit: LLM Analysis Quiz Solver Agent

Browse files
.env.example ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ GOOGLE_API_KEY=your_gemini_api_key
2
+ EMAIL=your_email
3
+ SECRET=your_secret
.env.example:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
.gitignore ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+ .env
9
+ # Virtual environments
10
+ .venv
11
+ tests
12
+ LLMFiles
.gitignore:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.12
.python-version:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # --- System deps required by Playwright browsers ---
4
+ RUN apt-get update && apt-get install -y \
5
+ wget gnupg ca-certificates curl unzip \
6
+ libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libxkbcommon0 \
7
+ libgtk-3-0 libgbm1 libasound2 libxcomposite1 libxdamage1 libxrandr2 \
8
+ libxfixes3 libpango-1.0-0 libcairo2 \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ # --- Install Playwright + Chromium ---
12
+ RUN pip install playwright && playwright install --with-deps chromium
13
+
14
+ # --- Install uv package manager ---
15
+ RUN pip install uv
16
+
17
+ # --- Copy app to container ---
18
+ WORKDIR /app
19
+
20
+ COPY . .
21
+
22
+ ENV PYTHONUNBUFFERED=1
23
+ ENV PYTHONIOENCODING=utf-8
24
+
25
+ # --- Install project dependencies using uv ---
26
+ RUN uv sync --frozen
27
+
28
+ # HuggingFace Spaces exposes port 7860
29
+ EXPOSE 7860
30
+
31
+ # --- Run your FastAPI app ---
32
+ # uvicorn must be in pyproject dependencies
33
+ CMD ["uv", "run", "main.py"]
Dockerfile:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sai Vijay Ragav
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
LICENSE:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
README.md ADDED
@@ -0,0 +1,389 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: LLM Analysis Quiz Solver
3
+ emoji: 🏃
4
+ colorFrom: red
5
+ colorTo: blue
6
+ sdk: docker
7
+ pinned: false
8
+ app_port: 7860
9
+ ---
10
+
11
+ # LLM Analysis - Autonomous Quiz Solver Agent
12
+
13
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
14
+ [![Python 3.12+](https://img.shields.io/badge/python-3.12+-blue.svg)](https://www.python.org/downloads/)
15
+ [![FastAPI](https://img.shields.io/badge/FastAPI-0.121.3+-green.svg)](https://fastapi.tiangolo.com/)
16
+
17
+ An intelligent, autonomous agent built with LangGraph and LangChain that solves data-related quizzes involving web scraping, data processing, analysis, and visualization tasks. The system uses Google's Gemini 2.5 Flash model to orchestrate tool usage and make decisions.
18
+
19
+ ## 📋 Table of Contents
20
+
21
+ - [Overview](#overview)
22
+ - [Architecture](#architecture)
23
+ - [Features](#features)
24
+ - [Project Structure](#project-structure)
25
+ - [Installation](#installation)
26
+ - [Configuration](#configuration)
27
+ - [Usage](#usage)
28
+ - [API Endpoints](#api-endpoints)
29
+ - [Tools & Capabilities](#tools--capabilities)
30
+ - [Docker Deployment](#docker-deployment)
31
+ - [How It Works](#how-it-works)
32
+ - [License](#license)
33
+
34
+ ## 🔍 Overview
35
+
36
+ This project was developed for the TDS (Tools in Data Science) course project, where the objective is to build an application that can autonomously solve multi-step quiz tasks involving:
37
+
38
+ - **Data sourcing**: Scraping websites, calling APIs, downloading files
39
+ - **Data preparation**: Cleaning text, PDFs, and various data formats
40
+ - **Data analysis**: Filtering, aggregating, statistical analysis, ML models
41
+ - **Data visualization**: Generating charts, narratives, and presentations
42
+
43
+ The system receives quiz URLs via a REST API, navigates through multiple quiz pages, solves each task using LLM-powered reasoning and specialized tools, and submits answers back to the evaluation server.
44
+
45
+ ## 🏗️ Architecture
46
+
47
+ The project uses a **LangGraph state machine** architecture with the following components:
48
+
49
+ ```
50
+ ┌─────────────┐
51
+ │ FastAPI │ ← Receives POST requests with quiz URLs
52
+ │ Server │
53
+ └──────┬──────┘
54
+
55
+
56
+ ┌─────────────┐
57
+ │ Agent │ ← LangGraph orchestrator with Gemini 2.5 Flash
58
+ │ (LLM) │
59
+ └──────┬──────┘
60
+
61
+ ├────────────┬────────────┬─────────────┬──────────────┐
62
+ ▼ ▼ ▼ ▼ ▼
63
+ [Scraper] [Downloader] [Code Exec] [POST Req] [Add Deps]
64
+ ```
65
+
66
+ ### Key Components:
67
+
68
+ 1. **FastAPI Server** (`main.py`): Handles incoming POST requests, validates secrets, and triggers the agent
69
+ 2. **LangGraph Agent** (`agent.py`): State machine that coordinates tool usage and decision-making
70
+ 3. **Tools Package** (`tools/`): Modular tools for different capabilities
71
+ 4. **LLM**: Google Gemini 2.5 Flash with rate limiting (9 requests per minute)
72
+
73
+ ## ✨ Features
74
+
75
+ - ✅ **Autonomous multi-step problem solving**: Chains together multiple quiz pages
76
+ - ✅ **Dynamic JavaScript rendering**: Uses Playwright for client-side rendered pages
77
+ - ✅ **Code generation & execution**: Writes and runs Python code for data tasks
78
+ - ✅ **Flexible data handling**: Downloads files, processes PDFs, CSVs, images, etc.
79
+ - ✅ **Self-installing dependencies**: Automatically adds required Python packages
80
+ - ✅ **Robust error handling**: Retries failed attempts within time limits
81
+ - ✅ **Docker containerization**: Ready for deployment on HuggingFace Spaces or cloud platforms
82
+ - ✅ **Rate limiting**: Respects API quotas with exponential backoff
83
+
84
+ ## 📁 Project Structure
85
+
86
+ ```
87
+ LLM-Analysis-TDS-Project-2/
88
+ ├── agent.py # LangGraph state machine & orchestration
89
+ ├── main.py # FastAPI server with /solve endpoint
90
+ ├── pyproject.toml # Project dependencies & configuration
91
+ ├── Dockerfile # Container image with Playwright
92
+ ├── .env # Environment variables (not in repo)
93
+ ├── tools/
94
+ │ ├── __init__.py
95
+ │ ├── web_scraper.py # Playwright-based HTML renderer
96
+ │ ├── code_generate_and_run.py # Python code executor
97
+ │ ├── download_file.py # File downloader
98
+ │ ├── send_request.py # HTTP POST tool
99
+ │ └── add_dependencies.py # Package installer
100
+ └── README.md
101
+ ```
102
+
103
+ ## 📦 Installation
104
+
105
+ ### Prerequisites
106
+
107
+ - Python 3.12 or higher
108
+ - [uv](https://github.com/astral-sh/uv) package manager (recommended) or pip
109
+ - Git
110
+
111
+ ### Step 1: Clone the Repository
112
+
113
+ ```bash
114
+ git clone https://github.com/saivijayragav/LLM-Analysis-TDS-Project-2.git
115
+ cd LLM-Analysis-TDS-Project-2
116
+ ```
117
+
118
+ ### Step 2: Install Dependencies
119
+
120
+ #### Option A: Using `uv` (Recommended)
121
+
122
+
123
+ Ensure you have uv installed, then sync the project:
124
+
125
+ ```
126
+ # Install uv if you haven't already
127
+ pip install uv
128
+
129
+ # Sync dependencies
130
+ uv sync
131
+ uv run playwright install chromium
132
+ ```
133
+
134
+ Start the FastAPI server:
135
+ ```
136
+ uv run main.py
137
+ ```
138
+ The server will start at ```http://0.0.0.0:7860```.
139
+
140
+ #### Option B: Using `pip`
141
+
142
+ ```bash
143
+ # Create virtual environment
144
+ python -m venv venv
145
+ .\venv\Scripts\activate # Windows
146
+ # source venv/bin/activate # macOS/Linux
147
+
148
+ # Install dependencies
149
+ pip install -e .
150
+
151
+ # Install Playwright browsers
152
+ playwright install chromium
153
+ ```
154
+
155
+ ## ⚙️ Configuration
156
+
157
+ ### Environment Variables
158
+
159
+ Create a `.env` file in the project root:
160
+
161
+ ```env
162
+ # Your credentials from the Google Form submission
163
+ EMAIL=your.email@example.com
164
+ SECRET=your_secret_string
165
+
166
+ # Google Gemini API Key
167
+ GOOGLE_API_KEY=your_gemini_api_key_here
168
+ ```
169
+
170
+ ### Getting a Gemini API Key
171
+
172
+ 1. Visit [Google AI Studio](https://aistudio.google.com/app/apikey)
173
+ 2. Create a new API key
174
+ 3. Copy it to your `.env` file
175
+
176
+ ## 🚀 Usage
177
+
178
+ ### Local Development
179
+
180
+ Start the FastAPI server:
181
+
182
+ ```bash
183
+ # If using uv
184
+ uv run main.py
185
+
186
+ # If using standard Python
187
+ python main.py
188
+ ```
189
+
190
+ The server will start on `http://0.0.0.0:7860`
191
+
192
+ ### Testing the Endpoint
193
+
194
+ Send a POST request to test your setup:
195
+
196
+ ```bash
197
+ curl -X POST http://localhost:7860/solve \
198
+ -H "Content-Type: application/json" \
199
+ -d '{
200
+ "email": "your.email@example.com",
201
+ "secret": "your_secret_string",
202
+ "url": "https://tds-llm-analysis.s-anand.net/demo"
203
+ }'
204
+ ```
205
+
206
+ Expected response:
207
+
208
+ ```json
209
+ {
210
+ "status": "ok"
211
+ }
212
+ ```
213
+
214
+ The agent will run in the background and solve the quiz chain autonomously.
215
+
216
+ ## 🌐 API Endpoints
217
+
218
+ ### `POST /solve`
219
+
220
+ Receives quiz tasks and triggers the autonomous agent.
221
+
222
+ **Request Body:**
223
+
224
+ ```json
225
+ {
226
+ "email": "your.email@example.com",
227
+ "secret": "your_secret_string",
228
+ "url": "https://example.com/quiz-123"
229
+ }
230
+ ```
231
+
232
+ **Responses:**
233
+
234
+ | Status Code | Description |
235
+ | ----------- | ------------------------------ |
236
+ | `200` | Secret verified, agent started |
237
+ | `400` | Invalid JSON payload |
238
+ | `403` | Invalid secret |
239
+
240
+ ### `GET /healthz`
241
+
242
+ Health check endpoint for monitoring.
243
+
244
+ **Response:**
245
+
246
+ ```json
247
+ {
248
+ "status": "ok",
249
+ "uptime_seconds": 3600
250
+ }
251
+ ```
252
+
253
+ ## 🛠️ Tools & Capabilities
254
+
255
+ The agent has access to the following tools:
256
+
257
+ ### 1. **Web Scraper** (`get_rendered_html`)
258
+
259
+ - Uses Playwright to render JavaScript-heavy pages
260
+ - Waits for network idle before extracting content
261
+ - Returns fully rendered HTML for parsing
262
+
263
+ ### 2. **File Downloader** (`download_file`)
264
+
265
+ - Downloads files (PDFs, CSVs, images, etc.) from direct URLs
266
+ - Saves files to `LLMFiles/` directory
267
+ - Returns the saved filename
268
+
269
+ ### 3. **Code Executor** (`run_code`)
270
+
271
+ - Executes arbitrary Python code in an isolated subprocess
272
+ - Returns stdout, stderr, and exit code
273
+ - Useful for data processing, analysis, and visualization
274
+
275
+ ### 4. **POST Request** (`post_request`)
276
+
277
+ - Sends JSON payloads to submission endpoints
278
+ - Includes automatic error handling and response parsing
279
+ - Prevents resubmission if answer is incorrect and time limit exceeded
280
+
281
+ ### 5. **Dependency Installer** (`add_dependencies`)
282
+
283
+ - Dynamically installs Python packages as needed
284
+ - Uses `uv add` for fast package resolution
285
+ - Enables the agent to adapt to different task requirements
286
+
287
+ ## 🐳 Docker Deployment
288
+
289
+ ### Build the Image
290
+
291
+ ```bash
292
+ docker build -t llm-analysis-agent .
293
+ ```
294
+
295
+ ### Run the Container
296
+
297
+ ```bash
298
+ docker run -p 7860:7860 \
299
+ -e EMAIL="your.email@example.com" \
300
+ -e SECRET="your_secret_string" \
301
+ -e GOOGLE_API_KEY="your_api_key" \
302
+ llm-analysis-agent
303
+ ```
304
+
305
+ ### Deploy to HuggingFace Spaces
306
+
307
+ 1. Create a new Space with Docker SDK
308
+ 2. Push this repository to your Space
309
+ 3. Add secrets in Space settings:
310
+ - `EMAIL`
311
+ - `SECRET`
312
+ - `GOOGLE_API_KEY`
313
+ 4. The Space will automatically build and deploy
314
+
315
+ ## 🧠 How It Works
316
+
317
+ ### 1. Request Reception
318
+
319
+ - FastAPI receives a POST request with quiz URL
320
+ - Validates the secret against environment variables
321
+ - Returns 200 OK and starts the agent in the background
322
+
323
+ ### 2. Agent Initialization
324
+
325
+ - LangGraph creates a state machine with two nodes: `agent` and `tools`
326
+ - The initial state contains the quiz URL as a user message
327
+
328
+ ### 3. Task Loop
329
+
330
+ The agent follows this loop:
331
+
332
+ ```
333
+ ┌─────────────────────────────────────────┐
334
+ │ 1. LLM analyzes current state │
335
+ │ - Reads quiz page instructions │
336
+ │ - Plans tool usage │
337
+ └─────────────────┬───────────────────────┘
338
+
339
+ ┌─────────────────────────────────────────┐
340
+ │ 2. Tool execution │
341
+ │ - Scrapes page / downloads files │
342
+ │ - Runs analysis code │
343
+ │ - Submits answer │
344
+ └─────────────────┬───────────────────────┘
345
+
346
+ ┌─────────────────────────────────────────┐
347
+ │ 3. Response evaluation │
348
+ │ - Checks if answer is correct │
349
+ │ - Extracts next quiz URL (if exists) │
350
+ └─────────────────┬───────────────────────┘
351
+
352
+ ┌─────────────────────────────────────────┐
353
+ │ 4. Decision │
354
+ │ - If new URL exists: Loop to step 1 │
355
+ │ - If no URL: Return "END" │
356
+ └─────────────────────────────────────────┘
357
+ ```
358
+
359
+ ### 4. State Management
360
+
361
+ - All messages (user, assistant, tool) are stored in state
362
+ - The LLM uses full history to make informed decisions
363
+ - Recursion limit set to 200 to handle long quiz chains
364
+
365
+ ### 5. Completion
366
+
367
+ - Agent returns "END" when no new URL is provided
368
+ - Background task completes
369
+ - Logs indicate success or failure
370
+
371
+ ## 📝 Key Design Decisions
372
+
373
+ 1. **LangGraph over Sequential Execution**: Allows flexible routing and complex decision-making
374
+ 2. **Background Processing**: Prevents HTTP timeouts for long-running quiz chains
375
+ 3. **Tool Modularity**: Each tool is independent and can be tested/debugged separately
376
+ 4. **Rate Limiting**: Prevents API quota exhaustion (9 req/min for Gemini)
377
+ 5. **Code Execution**: Dynamically generates and runs Python for complex data tasks
378
+ 6. **Playwright for Scraping**: Handles JavaScript-rendered pages that `requests` cannot
379
+ 7. **uv for Dependencies**: Fast package resolution and installation
380
+
381
+ ## 📄 License
382
+
383
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
384
+
385
+ ---
386
+
387
+ **Author**: Syph0n9
388
+ **Course**: Tools in Data Science (TDS)
389
+ **Institution**: IIT Madras
README.md:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
__init__.py ADDED
File without changes
__init__.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
agent.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langgraph.graph import StateGraph, END, START
2
+ from langchain_core.rate_limiters import InMemoryRateLimiter
3
+ from langgraph.prebuilt import ToolNode
4
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
5
+ from tools import get_rendered_html, download_file, post_request, run_code, add_dependencies
6
+ from typing import TypedDict, Annotated, List, Any
7
+ from langchain.chat_models import init_chat_model
8
+ from langgraph.graph.message import add_messages
9
+ import os
10
+ from dotenv import load_dotenv
11
+ load_dotenv()
12
+
13
+ EMAIL = os.getenv("EMAIL")
14
+ SECRET = os.getenv("SECRET")
15
+ RECURSION_LIMIT = 5000
16
+ # -------------------------------------------------
17
+ # STATE
18
+ # -------------------------------------------------
19
+ class AgentState(TypedDict):
20
+ messages: Annotated[List, add_messages]
21
+
22
+
23
+ TOOLS = [run_code, get_rendered_html, download_file, post_request, add_dependencies]
24
+
25
+
26
+ # -------------------------------------------------
27
+ # GEMINI LLM
28
+ # -------------------------------------------------
29
+ rate_limiter = InMemoryRateLimiter(
30
+ requests_per_second=9/60,
31
+ check_every_n_seconds=1,
32
+ max_bucket_size=9
33
+ )
34
+ llm = init_chat_model(
35
+ model_provider="google_genai",
36
+ model="gemini-2.5-flash",
37
+ rate_limiter=rate_limiter
38
+ ).bind_tools(TOOLS)
39
+
40
+
41
+ # -------------------------------------------------
42
+ # SYSTEM PROMPT
43
+ # -------------------------------------------------
44
+ SYSTEM_PROMPT = f"""
45
+ You are an autonomous quiz-solving agent.
46
+
47
+ Your job is to:
48
+ 1. Load the quiz page from the given URL.
49
+ 2. Extract ALL instructions, required parameters, submission rules, and the submit endpoint.
50
+ 3. Solve the task exactly as required.
51
+ 4. Submit the answer ONLY to the endpoint specified on the current page (never make up URLs).
52
+ 5. Read the server response and:
53
+ - If it contains a new quiz URL → fetch it immediately and continue.
54
+ - If no new URL is present → return "END".
55
+
56
+ STRICT RULES — FOLLOW EXACTLY:
57
+
58
+ GENERAL RULES:
59
+ - NEVER stop early. Continue solving tasks until no new URL is provided.
60
+ - NEVER hallucinate URLs, endpoints, fields, values, or JSON structure.
61
+ - NEVER shorten or modify URLs. Always submit the full URL.
62
+ - NEVER re-submit unless the server explicitly allows or it's within the 3-minute limit.
63
+ - ALWAYS inspect the server response before deciding what to do next.
64
+ - ALWAYS use the tools provided to fetch, scrape, download, render HTML, or send requests.
65
+
66
+ TIME LIMIT RULES:
67
+ - Each task has a hard 3-minute limit.
68
+ - The server response includes a "delay" field indicating elapsed time.
69
+ - If your answer is wrong retry again.
70
+
71
+ STOPPING CONDITION:
72
+ - Only return "END" when a server response explicitly contains NO new URL.
73
+ - DO NOT return END under any other condition.
74
+
75
+ ADDITIONAL INFORMATION YOU MUST INCLUDE WHEN REQUIRED:
76
+ - Email: {EMAIL}
77
+ - Secret: {SECRET}
78
+
79
+ YOUR JOB:
80
+ - Follow pages exactly.
81
+ - Extract data reliably.
82
+ - Never guess.
83
+ - Submit correct answers.
84
+ - Continue until no new URL.
85
+ - Then respond with: END
86
+ """
87
+
88
+ prompt = ChatPromptTemplate.from_messages([
89
+ ("system", SYSTEM_PROMPT),
90
+ MessagesPlaceholder(variable_name="messages")
91
+ ])
92
+
93
+ llm_with_prompt = prompt | llm
94
+
95
+
96
+ # -------------------------------------------------
97
+ # AGENT NODE
98
+ # -------------------------------------------------
99
+ def agent_node(state: AgentState):
100
+ result = llm_with_prompt.invoke({"messages": state["messages"]})
101
+ return {"messages": state["messages"] + [result]}
102
+
103
+
104
+ # -------------------------------------------------
105
+ # GRAPH
106
+ # -------------------------------------------------
107
+ def route(state):
108
+ last = state["messages"][-1]
109
+ # support both objects (with attributes) and plain dicts
110
+ tool_calls = None
111
+ if hasattr(last, "tool_calls"):
112
+ tool_calls = getattr(last, "tool_calls", None)
113
+ elif isinstance(last, dict):
114
+ tool_calls = last.get("tool_calls")
115
+
116
+ if tool_calls:
117
+ return "tools"
118
+ # get content robustly
119
+ content = None
120
+ if hasattr(last, "content"):
121
+ content = getattr(last, "content", None)
122
+ elif isinstance(last, dict):
123
+ content = last.get("content")
124
+
125
+ if isinstance(content, str) and content.strip() == "END":
126
+ return END
127
+ if isinstance(content, list) and content[0].get("text").strip() == "END":
128
+ return END
129
+ return "agent"
130
+ graph = StateGraph(AgentState)
131
+
132
+ graph.add_node("agent", agent_node)
133
+ graph.add_node("tools", ToolNode(TOOLS))
134
+
135
+
136
+
137
+ graph.add_edge(START, "agent")
138
+ graph.add_edge("tools", "agent")
139
+ graph.add_conditional_edges(
140
+ "agent",
141
+ route
142
+ )
143
+
144
+ app = graph.compile()
145
+
146
+
147
+ # -------------------------------------------------
148
+ # TEST
149
+ # -------------------------------------------------
150
+ def run_agent(url: str) -> str:
151
+ app.invoke({
152
+ "messages": [{"role": "user", "content": url}]},
153
+ config={"recursion_limit": RECURSION_LIMIT},
154
+ )
155
+ print("Tasks completed succesfully")
156
+
agent.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
main.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, BackgroundTasks
2
+ from fastapi.responses import JSONResponse
3
+ from fastapi.exceptions import HTTPException
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from agent import run_agent
6
+ from dotenv import load_dotenv
7
+ import uvicorn
8
+ import os
9
+ import time
10
+
11
+ load_dotenv()
12
+
13
+ EMAIL = os.getenv("EMAIL")
14
+ SECRET = os.getenv("SECRET")
15
+
16
+ app = FastAPI()
17
+ app.add_middleware(
18
+ CORSMiddleware,
19
+ allow_origins=["*"], # or specific domains
20
+ allow_credentials=True,
21
+ allow_methods=["*"],
22
+ allow_headers=["*"],
23
+ )
24
+ START_TIME = time.time()
25
+ @app.get("/healthz")
26
+ def healthz():
27
+ """Simple liveness check."""
28
+ return {
29
+ "status": "ok",
30
+ "uptime_seconds": int(time.time() - START_TIME)
31
+ }
32
+
33
+ @app.post("/solve")
34
+ async def solve(request: Request, background_tasks: BackgroundTasks):
35
+ try:
36
+ data = await request.json()
37
+ except Exception:
38
+ raise HTTPException(status_code=400, detail="Invalid JSON")
39
+ if not data:
40
+ raise HTTPException(status_code=400, detail="Invalid JSON")
41
+ url = data.get("url")
42
+ secret = data.get("secret")
43
+ if not url or not secret:
44
+ raise HTTPException(status_code=400, detail="Invalid JSON")
45
+
46
+ if secret != SECRET:
47
+ raise HTTPException(status_code=403, detail="Invalid secret")
48
+ print("Verified starting the task...")
49
+ background_tasks.add_task(run_agent, url)
50
+
51
+ return JSONResponse(status_code=200, content={"status": "ok"})
52
+
53
+
54
+ if __name__ == "__main__":
55
+ uvicorn.run(app, host="0.0.0.0", port=7860)
main.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
pyproject.toml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "tdsproject2"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = [
8
+ "playwright>=1.56.0",
9
+ "beautifulsoup4>=4.14.2",
10
+ "langgraph>=1.0.3",
11
+ "langchain>=0.2.0",
12
+ "langchain-community>=0.2.0",
13
+ "langchain-google-genai>=1.0.0",
14
+ "google-genai>=0.17.0",
15
+ "jsonpatch>=1.33",
16
+ "python-dotenv>=1.2.1",
17
+ "pandas>=2.3.3",
18
+ "fastapi>=0.121.3",
19
+ "uvicorn>=0.38.0",
20
+ "requests>=2.32.5",
21
+ ]
pyproject.toml:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tools/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from .web_scraper import get_rendered_html
2
+ from .run_code import run_code
3
+ from .send_request import post_request
4
+ from .download_file import download_file
5
+ from .add_dependencies import add_dependencies
tools/__init__.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tools/add_dependencies.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from langchain_core.tools import tool
3
+ import subprocess
4
+
5
+
6
+ @tool
7
+ def add_dependencies(dependencies: List[str]) -> str:
8
+ """
9
+ Install the given Python packages into the environment.
10
+
11
+ Parameters:
12
+ dependencies (List[str]):
13
+ A list of Python package names to install. Each name must match the
14
+ corresponding package name on PyPI.
15
+
16
+ Returns:
17
+ str:
18
+ A message indicating success or failure.
19
+ """
20
+
21
+ try:
22
+ subprocess.check_call(
23
+ ["uv", "add"] + dependencies,
24
+ stdout=subprocess.PIPE,
25
+ stderr=subprocess.PIPE,
26
+ text=True
27
+ )
28
+ return "Successfully installed dependencies: " + ", ".join(dependencies)
29
+
30
+ except subprocess.CalledProcessError as e:
31
+ return (
32
+ "Dependency installation failed.\n"
33
+ f"Exit code: {e.returncode}\n"
34
+ f"Error: {e.stderr or 'No error output.'}"
35
+ )
36
+
37
+ except Exception as e:
38
+ return f"Unexpected error while installing dependencies: {e}"
tools/add_dependencies.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tools/download_file.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ import requests
3
+ import os
4
+
5
+ @tool
6
+ def download_file(url: str, filename: str) -> str:
7
+ """
8
+ Download a file from a URL and save it with the given filename
9
+ in the current working directory.
10
+
11
+ Args:
12
+ url (str): Direct URL to the file.
13
+ filename (str): The filename to save the downloaded content as.
14
+
15
+ Returns:
16
+ str: Full path to the saved file.
17
+ """
18
+ try:
19
+ response = requests.get(url, stream=True)
20
+ response.raise_for_status()
21
+ directory_name = "LLMFiles"
22
+ os.makedirs(directory_name, exist_ok=True)
23
+ path = os.path.join(directory_name, filename)
24
+ with open(path, "wb") as f:
25
+ for chunk in response.iter_content(chunk_size=8192):
26
+ if chunk:
27
+ f.write(chunk)
28
+
29
+ return filename
30
+ except Exception as e:
31
+ return f"Error downloading file: {str(e)}"
tools/download_file.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tools/run_code.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from google import genai
2
+ import subprocess
3
+ from langchain_core.tools import tool
4
+ from dotenv import load_dotenv
5
+ import os
6
+ from google.genai import types
7
+ load_dotenv()
8
+ client = genai.Client()
9
+
10
+ def strip_code_fences(code: str) -> str:
11
+ code = code.strip()
12
+ # Remove ```python ... ``` or ``` ... ```
13
+ if code.startswith("```"):
14
+ # remove first line (```python or ```)
15
+ code = code.split("\n", 1)[1]
16
+ if code.endswith("```"):
17
+ code = code.rsplit("\n", 1)[0]
18
+ return code.strip()
19
+
20
+ @tool
21
+ def run_code(code: str) -> dict:
22
+ """
23
+ Executes a Python code
24
+ This tool:
25
+ 1. Takes in python code as input
26
+ 3. Writes code into a temporary .py file
27
+ 4. Executes the file
28
+ 5. Returns its output
29
+
30
+ Parameters
31
+ ----------
32
+ code : str
33
+ Python source code to execute.
34
+
35
+ Returns
36
+ -------
37
+ dict
38
+ {
39
+ "stdout": <program output>,
40
+ "stderr": <errors if any>,
41
+ "return_code": <exit code>
42
+ }
43
+ """
44
+ try:
45
+ filename = "runner.py"
46
+ os.makedirs("LLMFiles", exist_ok=True)
47
+ with open(os.path.join("LLMFiles", filename), "w") as f:
48
+ f.write(code)
49
+
50
+ proc = subprocess.Popen(
51
+ ["uv", "run", filename],
52
+ stdout=subprocess.PIPE,
53
+ stderr=subprocess.PIPE,
54
+ text=True,
55
+ cwd="LLMFiles"
56
+ )
57
+ stdout, stderr = proc.communicate()
58
+
59
+ # --- Step 4: Return everything ---
60
+ return {
61
+ "stdout": stdout,
62
+ "stderr": stderr,
63
+ "return_code": proc.returncode
64
+ }
65
+ except Exception as e:
66
+ return {
67
+ "stdout": "",
68
+ "stderr": str(e),
69
+ "return_code": -1
70
+ }
tools/run_code.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tools/send_request.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ import requests
3
+ import json
4
+ from typing import Any, Dict, Optional
5
+
6
+ @tool
7
+ def post_request(url: str, payload: Dict[str, Any], headers: Optional[Dict[str, str]] = None) -> Any:
8
+ """
9
+ Send an HTTP POST request to the given URL with the provided payload.
10
+
11
+ This function is designed for LangGraph applications, where it can be wrapped
12
+ as a Tool or used inside a Runnable to call external APIs, webhooks, or backend
13
+ services during graph execution.
14
+ REMEMBER: This a blocking function so it may take a while to return. Wait for the response.
15
+ Args:
16
+ url (str): The endpoint to send the POST request to.
17
+ payload (Dict[str, Any]): The JSON-serializable request body.
18
+ headers (Optional[Dict[str, str]]): Optional HTTP headers to include
19
+ in the request. If omitted, a default JSON header is applied.
20
+
21
+ Returns:
22
+ Any: The response body. If the server returns JSON, a parsed dict is
23
+ returned. Otherwise, the raw text response is returned.
24
+
25
+ Raises:
26
+ requests.HTTPError: If the server responds with an unsuccessful status.
27
+ requests.RequestException: For network-related errors.
28
+ """
29
+ headers = headers or {"Content-Type": "application/json"}
30
+ try:
31
+ print(f"\nSending Answer \n{json.dumps(payload, indent=4)}\n to url: {url}")
32
+ response = requests.post(url, json=payload, headers=headers)
33
+
34
+ # Raise on 4xx/5xx
35
+ response.raise_for_status()
36
+
37
+ # Try to return JSON, fallback to raw text
38
+ data = response.json()
39
+ delay = data.get("delay", 0)
40
+ delay = delay if isinstance(delay, (int, float)) else 0
41
+ correct = data.get("correct")
42
+ if not correct and delay < 180:
43
+ del data["url"]
44
+ if delay >= 180:
45
+ data = {
46
+ "url": data.get("url")
47
+ }
48
+ print("Got the response: \n", json.dumps(data, indent=4), '\n')
49
+ return data
50
+ except requests.HTTPError as e:
51
+ # Extract server’s error response
52
+ err_resp = e.response
53
+
54
+ try:
55
+ err_data = err_resp.json()
56
+ except ValueError:
57
+ err_data = err_resp.text
58
+
59
+ print("HTTP Error Response:\n", err_data)
60
+ return err_data
61
+
62
+ except Exception as e:
63
+ print("Unexpected error:", e)
64
+ return str(e)
tools/send_request.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tools/web_scraper.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ from playwright.sync_api import sync_playwright
3
+ from bs4 import BeautifulSoup
4
+
5
+ @tool
6
+ def get_rendered_html(url: str) -> str:
7
+ """
8
+ Fetch and return the fully rendered HTML of a webpage.
9
+
10
+ This function uses Playwright to load a webpage in a headless Chromium
11
+ browser, allowing all JavaScript on the page to execute. Use this for
12
+ dynamic websites that require rendering.
13
+
14
+ IMPORTANT RESTRICTIONS:
15
+ - ONLY use this for actual HTML webpages (articles, documentation, dashboards).
16
+ - DO NOT use this for direct file links (URLs ending in .csv, .pdf, .zip, .png).
17
+ Playwright cannot render these and will crash. Use the 'download_file' tool instead.
18
+
19
+ Parameters
20
+ ----------
21
+ url : str
22
+ The URL of the webpage to retrieve and render.
23
+
24
+ Returns
25
+ -------
26
+ str
27
+ The fully rendered and cleaned HTML content.
28
+ """
29
+ # ... existing code ...
30
+ print("\nFetching and rendering:", url)
31
+ try:
32
+ with sync_playwright() as p:
33
+ browser = p.chromium.launch(headless=True)
34
+ page = browser.new_page()
35
+
36
+ # Load the page (let JS execute)
37
+ page.goto(url, wait_until="networkidle")
38
+
39
+ # Extract rendered HTML
40
+ content = page.content()
41
+
42
+ browser.close()
43
+ return content
44
+
45
+ except Exception as e:
46
+ return f"Error fetching/rendering page: {str(e)}"
tools/web_scraper.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
uv.lock ADDED
The diff for this file is too large to render. See raw diff
 
uv.lock:Zone.Identifier ADDED
Binary file (25 Bytes). View file