iitmbs24f commited on
Commit
514caf5
Β·
verified Β·
1 Parent(s): 8676663

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +272 -277
README.md CHANGED
@@ -1,277 +1,272 @@
1
- ---
2
- sdk: docker
3
- ---
4
- # IITM LLM Quiz Solver
5
- title: IITM LLM Quiz Solver
6
- emoji: 🧠
7
- colorFrom: green
8
- colorTo: blue
9
- sdk: docker
10
- sdk_version: "0"
11
- app_file: app/main.py
12
- pinned: false
13
- ---
14
-
15
-
16
- A complete Python project with FastAPI that acts as an API endpoint to automatically solve dynamic quiz tasks using a headless browser and optional LLM reasoning.
17
-
18
- ## Features
19
-
20
- - πŸš€ FastAPI-based REST API
21
- - 🌐 Playwright for headless browser automation
22
- - πŸ€– OpenAI GPT integration for complex reasoning
23
- - πŸ“Š Data processing (CSV, JSON, PDF, etc.)
24
- - πŸ”„ Recursive quiz solving
25
- - ⚑ Async/await for performance
26
- - 🐳 Docker support for easy deployment
27
-
28
- ## Project Structure
29
-
30
- ```
31
- /app
32
- - main.py # FastAPI server
33
- - solver.py # Quiz solving logic
34
- - browser.py # Playwright helper
35
- - llm.py # GPT helper
36
- - utils.py # Utility functions
37
- /Dockerfile
38
- /requirements.txt
39
- /README.md
40
- /LICENSE
41
- ```
42
-
43
- ## Installation
44
-
45
- ### Local Development
46
-
47
- 1. Clone the repository:
48
- ```bash
49
- git clone <repository-url>
50
- cd IITMTdsPrj2
51
- ```
52
-
53
- 2. Install Python dependencies:
54
- ```bash
55
- pip install -r requirements.txt
56
- ```
57
-
58
- 3. Install Playwright browsers:
59
- ```bash
60
- playwright install chromium
61
- ```
62
-
63
- 4. Set environment variables:
64
-
65
- **Quick Setup (Windows PowerShell):**
66
- ```powershell
67
- .\setup_env.ps1
68
- ```
69
-
70
- **Quick Setup (Linux/Mac):**
71
- ```bash
72
- source setup_env.sh
73
- ```
74
-
75
- **Manual Setup (choose whichever LLM provider you prefer):**
76
- ```bash
77
- # Windows PowerShell
78
- $env:QUIZ_SECRET = "your_secret_key"
79
- $env:OPENAI_API_KEY = "sk-your-openai-api-key" # Optional - OpenAI
80
- $env:OPENROUTER_API_KEY = "sk-or-your-openrouter" # Optional - OpenRouter GPT-5-nano
81
-
82
- # Linux/Mac
83
- export QUIZ_SECRET="your_secret_key"
84
- export OPENAI_API_KEY="sk-your-openai-api-key" # Optional
85
- export OPENROUTER_API_KEY="sk-or-your-openrouter" # Optional
86
- ```
87
-
88
- **Or use .env file:**
89
- - Copy `.env.example` to `.env` (if available)
90
- - Fill in your values
91
- - The app will automatically load it
92
-
93
- πŸ“– **See [ENV_SETUP.md](ENV_SETUP.md) for detailed instructions**
94
-
95
- 5. Run the server:
96
- ```bash
97
- python -m app.main
98
- # or
99
- uvicorn app.main:app --host 0.0.0.0 --port 8000
100
- ```
101
-
102
- ## API Endpoints
103
-
104
- ### POST /solve
105
-
106
- Main endpoint to solve a quiz.
107
-
108
- **Request Body:**
109
- ```json
110
- {
111
- "email": "user@example.com",
112
- "secret": "your_secret",
113
- "url": "https://example.com/quiz"
114
- }
115
- ```
116
-
117
- **Response:**
118
- - `200 OK`: Quiz solved successfully
119
- - `400 Bad Request`: Invalid request format
120
- - `403 Forbidden`: Invalid secret
121
- - `500 Internal Server Error`: Server error
122
- - `504 Gateway Timeout`: Request timeout (>3 minutes)
123
-
124
- ### POST /demo
125
-
126
- Demo endpoint for testing (same as `/solve` but with more lenient error handling).
127
-
128
- **Request Body:** Same as `/solve`
129
-
130
- ### GET /health
131
-
132
- Health check endpoint.
133
-
134
- **Response:**
135
- ```json
136
- {
137
- "status": "healthy"
138
- }
139
- ```
140
-
141
- ## Deployment on Hugging Face Spaces
142
-
143
- ### Method 1: Using Dockerfile (Recommended)
144
-
145
- 1. **Create a new Space on Hugging Face:**
146
- - Go to https://huggingface.co/spaces
147
- - Create a new Space
148
- - Select "Docker" as the SDK
149
-
150
- 2. **Upload your files:**
151
- - Upload all project files to your Space
152
- - Ensure `Dockerfile` is in the root directory
153
-
154
- 3. **Set Environment Variables:**
155
- - Go to Space Settings β†’ Variables and secrets
156
- - Add the following:
157
- - `QUIZ_SECRET`: Your secret key for authentication
158
- - `OPENAI_API_KEY`: Your OpenAI API key (optional)
159
- - `OPENROUTER_API_KEY`: Your OpenRouter key (e.g., GPT-5-nano)
160
- - `PORT`: 8000 (usually set automatically)
161
-
162
- 4. **Deploy:**
163
- - Hugging Face will automatically build and deploy your Docker container
164
- - The API will be available at: `https://<your-username>-<space-name>.hf.space`
165
-
166
- ### Method 2: Using Docker Compose (Alternative)
167
-
168
- If you need more control, you can use `docker-compose.yml`:
169
-
170
- ```yaml
171
- version: '3.8'
172
- services:
173
- app:
174
- build: .
175
- ports:
176
- - "8000:8000"
177
- environment:
178
- - QUIZ_SECRET=${QUIZ_SECRET}
179
- - OPENAI_API_KEY=${OPENAI_API_KEY}
180
- ```
181
-
182
- ## Environment Variables
183
-
184
- | Variable | Description | Required | Default |
185
- |----------|-------------|----------|---------|
186
- | `QUIZ_SECRET` | Secret key for API authentication | Yes | `default_secret_change_me` |
187
- | `OPENAI_API_KEY` | OpenAI API key for LLM features | No | - |
188
- | `OPENROUTER_API_KEY` | OpenRouter key (e.g., GPT-5-nano) | No | - |
189
- | `OPENROUTER_MODEL` | Override OpenRouter model (default gpt-5-nano) | No | `gpt-5-nano` |
190
- | `PORT` | Server port | No | `8000` |
191
-
192
- ## Testing
193
-
194
- ### Test with curl:
195
-
196
- ```bash
197
- curl -X POST "https://tds-llm-analysis.s-anand.net/demo" \
198
- -H "Content-Type: application/json" \
199
- -d '{
200
- "email": "test@example.com",
201
- "secret": "your_secret",
202
- "url": "https://example.com/quiz"
203
- }'
204
- ```
205
-
206
- ### Test with Python:
207
-
208
- ```python
209
- import requests
210
-
211
- response = requests.post(
212
- "https://tds-llm-analysis.s-anand.net/demo",
213
- json={
214
- "email": "test@example.com",
215
- "secret": "your_secret",
216
- "url": "https://example.com/quiz"
217
- }
218
- )
219
-
220
- print(response.json())
221
- ```
222
-
223
- ## How It Works
224
-
225
- 1. **Request Validation**: Validates email, secret, and URL format
226
- 2. **Secret Authentication**: Checks secret against expected value (403 if wrong)
227
- 3. **Page Loading**: Uses Playwright to load and render the quiz page
228
- 4. **Content Extraction**: Extracts all text, HTML, links, and images
229
- 5. **Submit URL Detection**: Automatically finds the submit URL from page content
230
- 6. **Question Solving**:
231
- - Extracts question text
232
- - Tries multiple strategies:
233
- - Check if answer is in page
234
- - Download and process data files (CSV, JSON, PDF)
235
- - Use LLM for complex reasoning
236
- 7. **Answer Submission**: Submits answer to detected submit URL
237
- 8. **Recursive Solving**: If response contains next URL, solves recursively
238
- 9. **Response**: Returns final result
239
-
240
- ## Solver Strategies
241
-
242
- The solver uses multiple strategies in order:
243
-
244
- 1. **Direct Answer Extraction**: Checks if answer is already in page
245
- 2. **Data File Processing**: Downloads and processes CSV, JSON, PDF files
246
- 3. **LLM Reasoning**: Uses GPT-4o-mini (OpenAI) or GPT-5-nano (OpenRouter) for complex questions
247
- 4. **Fallback**: Returns question analysis if all else fails
248
-
249
- ## Error Handling
250
-
251
- - Invalid JSON β†’ 400 Bad Request
252
- - Wrong secret β†’ 403 Forbidden
253
- - Page load errors β†’ 500 with error details
254
- - Timeout (>3 min) β†’ 504 Gateway Timeout
255
- - All errors are logged for debugging
256
-
257
- ## Limitations
258
-
259
- - Maximum recursion depth: 10 quizzes
260
- - Timeout: 3 minutes per request
261
- - Requires internet connection for external URLs
262
- - OpenAI API key needed for LLM features (optional)
263
-
264
- ## License
265
-
266
- MIT License - see LICENSE file for details.
267
-
268
- ## Contributing
269
-
270
- 1. Fork the repository
271
- 2. Create a feature branch
272
- 3. Make your changes
273
- 4. Submit a pull request
274
-
275
- ## Support
276
-
277
- For issues and questions, please open an issue on the repository.
 
1
+ ---
2
+ title: IITM LLM Quiz Solver
3
+ emoji: 🧠
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: docker
7
+ sdk_version: "0"
8
+ app_file: app/main.py
9
+ pinned: false
10
+ ---
11
+
12
+
13
+ A complete Python project with FastAPI that acts as an API endpoint to automatically solve dynamic quiz tasks using a headless browser and optional LLM reasoning.
14
+
15
+ ## Features
16
+
17
+ - πŸš€ FastAPI-based REST API
18
+ - 🌐 Playwright for headless browser automation
19
+ - πŸ€– OpenAI GPT integration for complex reasoning
20
+ - πŸ“Š Data processing (CSV, JSON, PDF, etc.)
21
+ - πŸ”„ Recursive quiz solving
22
+ - ⚑ Async/await for performance
23
+ - 🐳 Docker support for easy deployment
24
+
25
+ ## Project Structure
26
+
27
+ ```
28
+ /app
29
+ - main.py # FastAPI server
30
+ - solver.py # All quiz solving logic (browser, LLM, calculations, handlers - consolidated)
31
+ /Dockerfile
32
+ /requirements.txt
33
+ /README.md
34
+ /LICENSE
35
+ ```
36
+
37
+ ## Installation
38
+
39
+ ### Local Development
40
+
41
+ 1. Clone the repository:
42
+ ```bash
43
+ git clone <repository-url>
44
+ cd IITMTdsPrj2
45
+ ```
46
+
47
+ 2. Install Python dependencies:
48
+ ```bash
49
+ pip install -r requirements.txt
50
+ ```
51
+
52
+ 3. Install Playwright browsers:
53
+ ```bash
54
+ playwright install chromium
55
+ ```
56
+
57
+ 4. Set environment variables:
58
+
59
+ **Quick Setup (Windows PowerShell):**
60
+ ```powershell
61
+ .\setup_env.ps1
62
+ ```
63
+
64
+ **Quick Setup (Linux/Mac):**
65
+ ```bash
66
+ source setup_env.sh
67
+ ```
68
+
69
+ **Manual Setup (choose whichever LLM provider you prefer):**
70
+ ```bash
71
+ # Windows PowerShell
72
+ $env:QUIZ_SECRET = "your_secret_key"
73
+ $env:OPENAI_API_KEY = "sk-your-openai-api-key" # Optional - OpenAI
74
+ $env:OPENROUTER_API_KEY = "sk-or-your-openrouter" # Optional - OpenRouter GPT-5-nano
75
+
76
+ # Linux/Mac
77
+ export QUIZ_SECRET="your_secret_key"
78
+ export OPENAI_API_KEY="sk-your-openai-api-key" # Optional
79
+ export OPENROUTER_API_KEY="sk-or-your-openrouter" # Optional
80
+ ```
81
+
82
+ **Or use .env file:**
83
+ - Copy `.env.example` to `.env` (if available)
84
+ - Fill in your values
85
+ - The app will automatically load it
86
+
87
+ πŸ“– **See [ENV_SETUP.md](ENV_SETUP.md) for detailed instructions**
88
+
89
+ 5. Run the server:
90
+ ```bash
91
+ python -m app.main
92
+ # or
93
+ uvicorn app.main:app --host 0.0.0.0 --port 8000
94
+ ```
95
+
96
+ ## API Endpoints
97
+
98
+ ### POST /solve
99
+
100
+ Main endpoint to solve a quiz.
101
+
102
+ **Request Body:**
103
+ ```json
104
+ {
105
+ "email": "user@example.com",
106
+ "secret": "your_secret",
107
+ "url": "https://example.com/quiz"
108
+ }
109
+ ```
110
+
111
+ **Response:**
112
+ - `200 OK`: Quiz solved successfully
113
+ - `400 Bad Request`: Invalid request format
114
+ - `403 Forbidden`: Invalid secret
115
+ - `500 Internal Server Error`: Server error
116
+ - `504 Gateway Timeout`: Request timeout (>3 minutes)
117
+
118
+ ### POST /demo
119
+
120
+ Demo endpoint for testing (same as `/solve` but with more lenient error handling).
121
+
122
+ **Request Body:** Same as `/solve`
123
+
124
+ ### GET /health
125
+
126
+ Health check endpoint.
127
+
128
+ **Response:**
129
+ ```json
130
+ {
131
+ "status": "healthy"
132
+ }
133
+ ```
134
+
135
+ ## Deployment on Hugging Face Spaces
136
+
137
+ ### Method 1: Using Dockerfile (Recommended)
138
+
139
+ 1. **Create a new Space on Hugging Face:**
140
+ - Go to https://huggingface.co/spaces
141
+ - Create a new Space
142
+ - Select "Docker" as the SDK
143
+
144
+ 2. **Upload your files:**
145
+ - Upload all project files to your Space
146
+ - Ensure `Dockerfile` is in the root directory
147
+
148
+ 3. **Set Environment Variables:**
149
+ - Go to Space Settings β†’ Variables and secrets
150
+ - Add the following:
151
+ - `QUIZ_SECRET`: Your secret key for authentication
152
+ - `OPENAI_API_KEY`: Your OpenAI API key (optional)
153
+ - `OPENROUTER_API_KEY`: Your OpenRouter key (e.g., GPT-5-nano)
154
+ - `PORT`: 8000 (usually set automatically)
155
+
156
+ 4. **Deploy:**
157
+ - Hugging Face will automatically build and deploy your Docker container
158
+ - The API will be available at: `https://<your-username>-<space-name>.hf.space`
159
+
160
+ ### Method 2: Using Docker Compose (Alternative)
161
+
162
+ If you need more control, you can use `docker-compose.yml`:
163
+
164
+ ```yaml
165
+ version: '3.8'
166
+ services:
167
+ app:
168
+ build: .
169
+ ports:
170
+ - "8000:8000"
171
+ environment:
172
+ - QUIZ_SECRET=${QUIZ_SECRET}
173
+ - OPENAI_API_KEY=${OPENAI_API_KEY}
174
+ ```
175
+
176
+ ## Environment Variables
177
+
178
+ | Variable | Description | Required | Default |
179
+ |----------|-------------|----------|---------|
180
+ | `QUIZ_SECRET` | Secret key for API authentication | Yes | `default_secret_change_me` |
181
+ | `OPENAI_API_KEY` | OpenAI API key for LLM features | No | - |
182
+ | `OPENROUTER_API_KEY` | OpenRouter key (e.g., GPT-5-nano) | No | - |
183
+ | `OPENROUTER_MODEL` | Override OpenRouter model (default gpt-5-nano) | No | `gpt-5-nano` |
184
+ | `PORT` | Server port | No | `8000` |
185
+
186
+ ## Testing
187
+
188
+ ### Test with curl:
189
+
190
+ ```bash
191
+ curl -X POST "https://tds-llm-analysis.s-anand.net/demo" \
192
+ -H "Content-Type: application/json" \
193
+ -d '{
194
+ "email": "test@example.com",
195
+ "secret": "your_secret",
196
+ "url": "https://example.com/quiz"
197
+ }'
198
+ ```
199
+
200
+ ### Test with Python:
201
+
202
+ ```python
203
+ import requests
204
+
205
+ response = requests.post(
206
+ "https://tds-llm-analysis.s-anand.net/demo",
207
+ json={
208
+ "email": "test@example.com",
209
+ "secret": "your_secret",
210
+ "url": "https://example.com/quiz"
211
+ }
212
+ )
213
+
214
+ print(response.json())
215
+ ```
216
+
217
+ ## How It Works
218
+
219
+ 1. **Request Validation**: Validates email, secret, and URL format
220
+ 2. **Secret Authentication**: Checks secret against expected value (403 if wrong)
221
+ 3. **Page Loading**: Uses Playwright to load and render the quiz page
222
+ 4. **Content Extraction**: Extracts all text, HTML, links, and images
223
+ 5. **Submit URL Detection**: Automatically finds the submit URL from page content
224
+ 6. **Question Solving**:
225
+ - Extracts question text
226
+ - Tries multiple strategies:
227
+ - Check if answer is in page
228
+ - Download and process data files (CSV, JSON, PDF)
229
+ - Use LLM for complex reasoning
230
+ 7. **Answer Submission**: Submits answer to detected submit URL
231
+ 8. **Recursive Solving**: If response contains next URL, solves recursively
232
+ 9. **Response**: Returns final result
233
+
234
+ ## Solver Strategies
235
+
236
+ The solver uses multiple strategies in order:
237
+
238
+ 1. **Direct Answer Extraction**: Checks if answer is already in page
239
+ 2. **Data File Processing**: Downloads and processes CSV, JSON, PDF files
240
+ 3. **LLM Reasoning**: Uses GPT-4o-mini (OpenAI) or GPT-5-nano (OpenRouter) for complex questions
241
+ 4. **Fallback**: Returns question analysis if all else fails
242
+
243
+ ## Error Handling
244
+
245
+ - Invalid JSON β†’ 400 Bad Request
246
+ - Wrong secret β†’ 403 Forbidden
247
+ - Page load errors β†’ 500 with error details
248
+ - Timeout (>3 min) β†’ 504 Gateway Timeout
249
+ - All errors are logged for debugging
250
+
251
+ ## Limitations
252
+
253
+ - Maximum recursion depth: 10 quizzes
254
+ - Timeout: 3 minutes per request
255
+ - Requires internet connection for external URLs
256
+ - OpenAI API key needed for LLM features (optional)
257
+
258
+ ## License
259
+
260
+ MIT License - see LICENSE file for details.
261
+
262
+ ## Contributing
263
+
264
+ 1. Fork the repository
265
+ 2. Create a feature branch
266
+ 3. Make your changes
267
+ 4. Submit a pull request
268
+
269
+ ## Support
270
+
271
+ For issues and questions, please open an issue on the repository.
272
+