Upload README.md
Browse files
README.md
CHANGED
|
@@ -1,277 +1,272 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
/
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
```bash
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
```
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
``
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
###
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
**
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
```
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
-
|
| 254 |
-
- Timeout
|
| 255 |
-
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|