Spaces:
Sleeping
Sleeping
File size: 46,693 Bytes
0573370 a00eb68 0573370 0b77f9b 0573370 0b77f9b 0573370 | 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 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 | # /// script
# requires-python = ">=3.11"
# dependencies = [
# "httpx",
# "python-dotenv",
# "fastapi",
# "uvicorn",
# "beautifulsoup4",
# "playwright",
# "pandas",
# "pdfplumber",
# "pillow",
# "pytesseract",
# "google-generativeai",
# "pandas"
# ]
# ///
import os
import asyncio
import json
import subprocess
import logging
import base64
import re
import io
from datetime import datetime
from typing import Dict, Any, Optional, List, Tuple
from urllib.parse import urljoin, urlparse, parse_qs
from fastapi import FastAPI, Request, BackgroundTasks
from fastapi.responses import JSONResponse
from dotenv import load_dotenv
import httpx
from bs4 import BeautifulSoup
import google.generativeai as genai
import pandas as pd
load_dotenv()
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
log_filename = os.path.join(log_dir, f"quiz_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - [%(funcName)s:%(lineno)d] - %(message)s',
handlers=[logging.FileHandler(log_filename), logging.StreamHandler()]
)
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
SECRET_KEY = os.getenv('SECRET_KEY')
BASE_URL = "https://tds-llm-analysis.s-anand.net"
if GEMINI_API_KEY:
genai.configure(api_key=GEMINI_API_KEY)
logger.info("[INIT] Gemini API configured successfully")
else:
logger.warning("[INIT] GEMINI_API_KEY not set")
app = FastAPI()
class QuizContext:
def __init__(self, email: str, quiz_url: str):
self.email = email
self.quiz_url = quiz_url
self.variables = {'EMAIL': email}
self.extract_url_params()
def extract_url_params(self):
try:
parsed = urlparse(self.quiz_url)
params = parse_qs(parsed.query)
for key, values in params.items():
if values:
self.variables[key.upper()] = values[0]
self.variables[key] = values[0]
logger.debug(f"[CONTEXT] Extracted variables: {self.variables}")
except Exception as e:
logger.debug(f"[CONTEXT] Error extracting URL params: {e}")
def substitute_variables(self, text: str) -> str:
if not text:
return text
result = text
for key, value in self.variables.items():
result = result.replace(f"${key}", value)
result = result.replace(f"${{{key}}}", value)
logger.debug(f"[CONTEXT] Substituted '{text}' -> '{result}'")
return result
class QuizData:
def __init__(self, url: str):
self.url = url
self.html = ""
self.visible_text = ""
self.decoded_content = ""
self.tables = []
self.links = []
self.forms = []
self.files = {"metadata": {}, "base64_samples": []}
self.title = ""
def detect_dynamic_content(html: str) -> bool:
logger.debug("[DETECT] Checking for dynamic content")
if not html:
return False
html_lower = html.lower()
indicators = ['react', 'vue', '__nuxt__', '__next_data__', 'angular', 'ember', 'svelte',
'id="app"', 'id="root"', 'id="__next"', 'fetch(', 'axios.', 'xmlhttprequest']
for indicator in indicators:
if indicator in html_lower:
logger.debug(f"[DETECT] Found: {indicator}")
return True
try:
soup = BeautifulSoup(html, 'html.parser')
body = soup.find('body')
if body and len(body.get_text(strip=True)) < 100 and body.find_all(['script', 'link']):
logger.debug("[DETECT] Empty body with scripts")
return True
except:
pass
return False
async def fetch_page(url: str, force_playwright: bool = False) -> str:
logger.info(f"[FETCH] Starting fetch: {url}")
if not force_playwright:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
try:
logger.debug("[FETCH] Attempting static fetch")
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url, headers=headers, follow_redirects=True)
response.raise_for_status()
html = response.text
logger.debug(f"[FETCH] Static fetch successful: {len(html)} bytes")
# Check if content looks like it needs JavaScript
if detect_dynamic_content(html) or len(html.strip()) < 200:
logger.info("[FETCH] Content appears to need JavaScript, switching to Playwright")
force_playwright = True
else:
return html
except Exception as e:
logger.warning(f"[FETCH] Static fetch failed: {e}")
force_playwright = True
if force_playwright:
logger.info("[FETCH] Using Playwright for JavaScript execution")
try:
from playwright.async_api import async_playwright
except ImportError:
logger.error("[FETCH] Playwright not installed")
raise
try:
subprocess.run(['playwright', 'install'], capture_output=True, text=True)
except:
logger.warning("[FETCH] Playwright auto-install skipped")
async def render():
try:
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
context = await browser.new_context()
page = await context.new_page()
logger.info(f"[FETCH] Loading page with Playwright: {url}")
await page.goto(url, wait_until='domcontentloaded')
await page.wait_for_load_state('load')
logger.debug("[FETCH] Waiting for JavaScript execution (5s)")
await page.wait_for_timeout(5000)
html = await page.content()
await context.close()
await browser.close()
logger.info(f"[FETCH] Playwright render complete: {len(html)} bytes")
return html
except Exception as e:
logger.error(f"[FETCH] Playwright error: {e}")
raise
try:
loop = asyncio.get_running_loop()
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(asyncio.run, render())
return future.result()
except RuntimeError:
return await render()
raise Exception("Failed to fetch page")
def extract_base64_content(html: str) -> str:
logger.debug("[EXTRACT] Searching for base64 encoded content")
pattern = r'`([A-Za-z0-9+/=]{20,})`'
matches = re.findall(pattern, html)
for encoded in matches:
try:
decoded = base64.b64decode(encoded).decode('utf-8', errors='ignore')
logger.debug(f"[EXTRACT] Decoded: {decoded[:100]}")
return decoded
except:
pass
return ""
def extract_file_metadata(html: str, base_url: str) -> Dict[str, Any]:
logger.debug("[EXTRACT] Extracting file metadata")
files = {"metadata": {}, "base64_samples": []}
soup = BeautifulSoup(html, 'html.parser')
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text(strip=True)
if any(ext in href.lower() for ext in ['.pdf', '.csv', '.xlsx', '.xls', '.json', '.zip', '.png', '.jpg', '.jpeg', '.mp3', '.wav']):
full_url = href if href.startswith('http') else urljoin(base_url, href)
ext = href.split('.')[-1].lower()
files["metadata"][text or href] = {
'url': full_url,
'type': ext,
'text': text
}
logger.debug(f"[EXTRACT] Found file: {text} ({ext})")
return files
def extract_data(html: str, url: str) -> QuizData:
logger.info("[EXTRACT] Parsing page data")
data = QuizData(url)
data.html = html
if not html:
logger.warning("[EXTRACT] Empty HTML")
return data
try:
soup = BeautifulSoup(html, 'html.parser')
except Exception as e:
logger.error(f"[EXTRACT] Parse error: {e}")
return data
for tag in soup(['script', 'style']):
tag.decompose()
data.title = soup.title.string if soup.title else "No title"
data.visible_text = soup.get_text(separator='\n', strip=True)[:5000]
try:
data.links = []
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text(strip=True)
if text and href:
abs_url = urljoin(url, href)
data.links.append({'text': text, 'url': abs_url})
logger.debug(f"[EXTRACT] Found {len(data.links)} links")
except Exception as e:
logger.debug(f"[EXTRACT] Link extraction error: {e}")
try:
data.tables = []
for table in soup.find_all('table'):
rows = []
for tr in table.find_all('tr'):
cells = [td.get_text(strip=True) for td in tr.find_all(['td', 'th'])]
if cells:
rows.append(cells)
if rows:
data.tables.append(rows)
logger.debug(f"[EXTRACT] Found {len(data.tables)} tables")
except Exception as e:
logger.debug(f"[EXTRACT] Table extraction error: {e}")
try:
data.forms = []
for form in soup.find_all('form'):
form_data = {
'action': form.get('action', ''),
'method': form.get('method', 'POST'),
'fields': []
}
for field in form.find_all(['input', 'textarea', 'select']):
form_data['fields'].append({
'name': field.get('name', ''),
'type': field.get('type', 'text'),
'value': field.get('value', '')
})
if form_data['fields']:
data.forms.append(form_data)
logger.debug(f"[EXTRACT] Found {len(data.forms)} forms")
except Exception as e:
logger.debug(f"[EXTRACT] Form extraction error: {e}")
data.decoded_content = extract_base64_content(html)
if data.decoded_content:
logger.info(f"[EXTRACT] Decoded content length: {len(data.decoded_content)}")
data.files = extract_file_metadata(html, url)
logger.debug(f"[EXTRACT] Found {len(data.files['metadata'])} files")
return data
def serialize_quiz_data(data: QuizData) -> Dict[str, Any]:
return {
"url": data.url,
"title": data.title,
"visible_text": data.visible_text,
"decoded_content": data.decoded_content,
"tables": data.tables,
"links": data.links,
"forms": data.forms,
"files": data.files
}
async def call_llm_gemini(prompt: str, system_instruction: str, max_tokens: int = 4000) -> Optional[Dict[str, Any]]:
logger.debug(f"[GEMINI] Calling API with max_tokens={max_tokens}")
if not GEMINI_API_KEY:
logger.error("[GEMINI] GEMINI_API_KEY not set")
return None
max_retries = 5
retry_count = 0
backoff_base = 2
generation_config = {
"temperature": 0.7,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": max_tokens,
}
safety_settings = [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
]
while retry_count <= max_retries:
try:
logger.debug(f"[GEMINI] Attempt {retry_count + 1}/{max_retries + 1}")
logger.info(f"[GEMINI] β³ Sending request to Gemini 2.5 Pro...")
model = genai.GenerativeModel(
model_name='gemini-2.5-pro',
generation_config=generation_config,
safety_settings=safety_settings,
system_instruction=system_instruction
)
logger.info(f"[GEMINI] β³ WAITING FOR GEMINI TO ANALYZE AND RESPOND...")
response = await asyncio.to_thread(model.generate_content, prompt)
logger.info(f"[GEMINI] β Received response from Gemini")
# Check for safety blocks
if not response or not response.parts:
if hasattr(response, 'candidates') and response.candidates:
candidate = response.candidates[0]
if hasattr(candidate, 'finish_reason'):
finish_reason = candidate.finish_reason
logger.warning(f"[GEMINI] Blocked: finish_reason={finish_reason}")
if finish_reason == 2: # SAFETY
logger.error("[GEMINI] βββ SAFETY FILTER TRIGGERED βββ")
logger.error("[GEMINI] The input data is too large or inappropriate")
logger.error("[GEMINI] Attempted retry may not help - data needs reduction")
# Check if we should retry with smaller context
if 'Cutoff' in str(prompt) or len(str(prompt)) > 50000:
logger.error("[GEMINI] Prompt is very large - preprocessing may have failed")
if hasattr(candidate, 'safety_ratings'):
logger.error(f"[GEMINI] Safety Ratings: {candidate.safety_ratings}")
return None
logger.error("[GEMINI] Empty response from Gemini")
retry_count += 1
if retry_count > max_retries:
return None
wait_time = backoff_base ** retry_count
logger.warning(f"[GEMINI] Empty response. Retry {retry_count}/{max_retries} after {wait_time}s")
await asyncio.sleep(wait_time)
continue
content = response.text.strip()
logger.debug(f"[GEMINI] Response content length: {len(content)} chars")
logger.info(f"[GEMINI] Response (first 300 chars): {content[:300]}")
if not content:
logger.error("[GEMINI] Empty response content from LLM")
return None
content_clean = content
if content.startswith('```json'):
content_clean = content.replace('```json', '').replace('```', '').strip()
logger.debug("[GEMINI] Removed markdown code blocks")
elif content.startswith('```'):
content_clean = content.replace('```', '').strip()
logger.debug("[GEMINI] Removed generic code blocks")
try:
logger.debug("[GEMINI] Attempting to parse JSON response")
parsed = json.loads(content_clean)
logger.info("[GEMINI] β Successfully parsed JSON response")
logger.debug(f"[GEMINI] Parsed keys: {list(parsed.keys())}")
return parsed
except json.JSONDecodeError as e:
logger.warning(f"[GEMINI] JSON decode error: {e}")
logger.debug(f"[GEMINI] Attempted to parse: {content_clean[:500]}")
try:
logger.debug("[GEMINI] Attempting regex JSON extraction")
json_match = re.search(r'\{.*\}', content_clean, re.DOTALL)
if json_match:
logger.debug("[GEMINI] JSON match found")
parsed = json.loads(json_match.group())
logger.info("[GEMINI] β Successfully extracted and parsed JSON")
return parsed
else:
logger.error("[GEMINI] No JSON object found in response")
return None
except Exception as e:
logger.error(f"[GEMINI] JSON extraction failed: {e}")
return None
except Exception as e:
error_str = str(e).lower()
# Check for safety block in exception
if 'finish_reason' in error_str or 'safety' in error_str or 'blocked' in error_str:
logger.error(f"[GEMINI] Safety filter triggered: {e}")
logger.error("[GEMINI] Content was blocked - data may be too large or inappropriate")
return None
if '429' in error_str or 'quota' in error_str or 'rate limit' in error_str:
retry_count += 1
if retry_count > max_retries:
logger.error(f"[GEMINI] Rate limited after {max_retries} retries")
return None
wait_time = (backoff_base ** retry_count) * 2
logger.warning(f"[GEMINI] Rate limited. Retry {retry_count}/{max_retries} after {wait_time}s")
logger.info(f"[GEMINI] Sleeping for {wait_time} seconds...")
await asyncio.sleep(wait_time)
logger.info(f"[GEMINI] Retry {retry_count} starting now...")
continue
elif '503' in error_str or 'service unavailable' in error_str:
retry_count += 1
if retry_count > max_retries:
logger.error(f"[GEMINI] Service unavailable after {max_retries} retries")
return None
wait_time = backoff_base ** retry_count
logger.warning(f"[GEMINI] Service unavailable. Retry {retry_count}/{max_retries} after {wait_time}s")
await asyncio.sleep(wait_time)
continue
else:
retry_count += 1
if retry_count > max_retries:
logger.error(f"[GEMINI] Error after {max_retries} retries: {e}")
return None
wait_time = backoff_base ** retry_count
logger.warning(f"[GEMINI] Error: {type(e).__name__}: {e}")
logger.warning(f"[GEMINI] Retry {retry_count}/{max_retries} after {wait_time}s")
await asyncio.sleep(wait_time)
continue
logger.error(f"[GEMINI] Failed after {max_retries} total attempts")
return None
async def llm_understand(data: QuizData, context: QuizContext) -> Optional[Dict[str, Any]]:
logger.info("[LLM_UNDERSTAND] ========== PHASE 1: UNDERSTANDING QUIZ ==========")
logger.info("[LLM_UNDERSTAND] Sending data to Gemini for analysis...")
quiz_data = serialize_quiz_data(data)
quiz_data['context_variables'] = context.variables
prompt = f"""You are analyzing a quiz page. Study the provided data carefully and take your time to understand all requirements.
{json.dumps(quiz_data, indent=2)}
Available context variables for substitution:
{json.dumps(context.variables, indent=2)}
Analyze this data thoroughly and respond with ONLY valid JSON (no markdown, no explanation, no code blocks):
{{
"understanding": "<Your detailed interpretation of what the quiz is asking. Include all requirements, constraints, and expected operations.>",
"needed_steps": [
{{"type": "scrape_url" | "download_file" | "compute" | "extract" | "solve", "target": "<URL or filename or null. Use variables like $EMAIL, $ID if needed>"}}
],
"final_required_action": "solve"
}}
Possible step types:
- "scrape_url": Need to visit another page first. May contain additional data or instructions.
- "download_file": Need to download and parse a file (PDF, CSV, XLSX, JSON, image, audio, etc.)
- "compute": Need to perform calculation on visible data
- "extract": Need to extract/decode information from page content
- "solve": Ready to solve with current data
When specifying target URLs, you can use variables like $EMAIL, $ID, $SECRET, etc. from context variables.
Think step-by-step about what information is needed before solving.
Return ONLY the JSON object, no markdown formatting."""
system_instruction = """You are a precise quiz solver. Your task is to:
1. Analyze quiz pages thoroughly
2. Identify all required steps to solve the quiz
3. Return structured JSON responses only (no markdown code blocks)
4. Substitute variables in URLs using the provided context
5. Be clear and detailed in your understanding field
Take your time to analyze. Provide comprehensive understanding of what needs to be done.
Always return pure JSON without markdown formatting or code blocks."""
logger.info("[LLM_UNDERSTAND] Calling Gemini 2.5 Pro with 4000 tokens...")
result = await call_llm_gemini(prompt, system_instruction, max_tokens=4000)
if result:
logger.info(f"[LLM_UNDERSTAND] β Understanding: {result.get('understanding', '')[:150]}")
logger.info(f"[LLM_UNDERSTAND] Required steps: {len(result.get('needed_steps', []))} steps identified")
logger.info("[LLM_UNDERSTAND] ========== UNDERSTANDING PHASE COMPLETE ==========")
else:
logger.error("[LLM_UNDERSTAND] β Failed to get understanding from Gemini")
return result
async def download_and_process_file(url: str) -> Dict[str, Any]:
logger.info(f"[FILE] Downloading: {url}")
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.get(url)
response.raise_for_status()
content = response.content
logger.debug(f"[FILE] Downloaded {len(content)} bytes")
except Exception as e:
logger.error(f"[FILE] Download error: {e}")
return {'error': str(e)}
ext = url.split('.')[-1].lower()
logger.debug(f"[FILE] Processing {ext} file")
if ext == 'pdf':
try:
import pdfplumber
with pdfplumber.open(io.BytesIO(content)) as pdf:
text = '\n'.join([page.extract_text() or '' for page in pdf.pages])
tables = []
for page in pdf.pages:
page_tables = page.extract_tables()
if page_tables:
tables.extend(page_tables)
logger.info(f"[FILE] PDF: {len(text)} chars, {len(tables)} tables")
return {'type': 'pdf', 'text': text, 'tables': tables, 'pages': len(pdf.pages)}
except Exception as e:
logger.error(f"[FILE] PDF error: {e}")
return {'error': str(e)}
elif ext == 'csv':
try:
import pandas as pd
df = pd.read_csv(io.BytesIO(content))
logger.info(f"[FILE] CSV: {df.shape}")
return {
'type': 'csv',
'data': df.to_dict('records'),
'columns': df.columns.tolist(),
'shape': df.shape
}
except Exception as e:
logger.error(f"[FILE] CSV error: {e}")
return {'error': str(e)}
elif ext in ['xlsx', 'xls']:
try:
import pandas as pd
xls = pd.ExcelFile(io.BytesIO(content))
sheets = {}
for sheet in xls.sheet_names:
df = pd.read_excel(io.BytesIO(content), sheet_name=sheet)
sheets[sheet] = df.to_dict('records')
logger.info(f"[FILE] Excel: {len(sheets)} sheets")
return {'type': 'excel', 'sheets': sheets}
except Exception as e:
logger.error(f"[FILE] Excel error: {e}")
return {'error': str(e)}
elif ext == 'json':
try:
data = json.loads(content.decode('utf-8'))
logger.info(f"[FILE] JSON parsed")
return {'type': 'json', 'data': data}
except Exception as e:
logger.error(f"[FILE] JSON error: {e}")
return {'error': str(e)}
elif ext in ['png', 'jpg', 'jpeg']:
try:
import pytesseract
from PIL import Image
img = Image.open(io.BytesIO(content))
text = pytesseract.image_to_string(img)
return {'type': 'image', 'ocr_text': text, 'size': img.size}
except Exception as e:
logger.warning(f"[FILE] OCR unavailable, sending base64 sample instead")
b64 = base64.b64encode(content).decode('utf-8')
return {'type': 'image', 'base64_sample': b64, 'ocr_error': str(e)}
elif ext in ['mp3', 'wav']:
b64 = base64.b64encode(content[:5000]).decode('utf-8')
logger.info(f"[FILE] Audio file processed")
return {'type': 'audio', 'size': len(content), 'base64_sample': b64}
else:
b64 = base64.b64encode(content[:1000]).decode('utf-8')
logger.info(f"[FILE] File type: {ext}")
return {'type': ext, 'size': len(content), 'base64_sample': b64}
def preprocess_data_for_llm(file_data: Dict[str, Any], max_rows: int = 50) -> Dict[str, Any]:
"""Preprocess file data to reduce size before sending to LLM"""
logger.info(f"[PREPROCESS] Starting data preprocessing")
if not file_data or 'error' in file_data:
return file_data
file_type = file_data.get('type')
if file_type == 'csv':
data = file_data.get('data', [])
columns = file_data.get('columns', [])
shape = file_data.get('shape', (0, 0))
logger.info(f"[PREPROCESS] CSV has {shape[0]} rows, {shape[1]} columns")
if shape[0] > max_rows:
logger.info(f"[PREPROCESS] Sampling {max_rows} rows from {shape[0]} total rows")
# Create summary statistics
import pandas as pd
df = pd.DataFrame(data)
summary = {
'type': 'csv',
'original_shape': shape,
'columns': columns,
'sample_data': data[:max_rows], # First N rows
'statistics': {
'describe': df.describe().to_dict() if not df.empty else {},
'dtypes': {col: str(dtype) for col, dtype in df.dtypes.items()},
'null_counts': df.isnull().sum().to_dict(),
'unique_counts': {col: df[col].nunique() for col in df.columns}
},
'note': f'Showing {max_rows} sample rows out of {shape[0]} total rows with summary statistics'
}
logger.info(f"[PREPROCESS] Created summary with statistics for large CSV")
return summary
else:
return file_data
elif file_type == 'excel':
sheets = file_data.get('sheets', {})
summary_sheets = {}
for sheet_name, sheet_data in sheets.items():
if len(sheet_data) > max_rows:
logger.info(f"[PREPROCESS] Sampling sheet '{sheet_name}': {len(sheet_data)} rows")
summary_sheets[sheet_name] = sheet_data[:max_rows]
else:
summary_sheets[sheet_name] = sheet_data
return {
'type': 'excel',
'sheets': summary_sheets,
'note': f'Large sheets sampled to {max_rows} rows'
}
return file_data
async def perform_step(step: Dict[str, Any], data: QuizData, base_url: str, context: QuizContext) -> Tuple[Optional[QuizData], bool]:
step_type = step.get('type')
target = step.get('target')
if target:
target = context.substitute_variables(target)
logger.info(f"[PERFORM] Executing: {step_type} -> {target}")
if step_type == 'scrape_url':
if not target:
logger.error("[PERFORM] scrape_url requires target")
return None, False
full_url = target if target.startswith('http') else f"{base_url}{target}"
logger.info(f"[PERFORM] Scraping: {full_url}")
try:
# Always use Playwright for quiz URLs to ensure JavaScript execution
logger.info("[PERFORM] Using Playwright to ensure JavaScript execution")
html = await fetch_page(full_url, force_playwright=True)
if not html or len(html.strip()) < 20:
logger.error(f"[PERFORM] Scraped page too small: {len(html) if html else 0} bytes")
return None, False
logger.debug(f"[PERFORM] Page content preview: {html[:300]}")
new_data = extract_data(html, full_url)
# Re-extract base64 content after Playwright render
new_data.decoded_content = extract_base64_content(html)
if new_data.decoded_content:
logger.info(f"[PERFORM] Found decoded content: {len(new_data.decoded_content)} chars")
# Check if we got meaningful content (relaxed validation)
has_content = (
new_data.visible_text and len(new_data.visible_text) > 10
) or (
new_data.decoded_content and len(new_data.decoded_content) > 10
) or (
len(new_data.links) > 0
) or (
len(new_data.tables) > 0
)
if not has_content:
logger.warning(f"[PERFORM] Page has minimal content but proceeding")
logger.debug(f"[PERFORM] visible_text: {len(new_data.visible_text)} chars")
logger.debug(f"[PERFORM] decoded_content: {len(new_data.decoded_content)} chars")
logger.debug(f"[PERFORM] links: {len(new_data.links)}")
# Don't fail - let LLM try to work with what we have
logger.info("[PERFORM] Scrape successful")
return new_data, True
except Exception as e:
logger.error(f"[PERFORM] Scrape failed: {e}", exc_info=True)
return None, False
elif step_type == 'download_file':
if not target:
logger.error("[PERFORM] download_file requires target")
return None, False
full_url = target if target.startswith('http') else f"{base_url}{target}"
try:
file_data = await download_and_process_file(full_url)
if 'error' in file_data and file_data['error']:
logger.warning(f"[PERFORM] File processing had error: {file_data['error']}")
return None, False
data.files['processed'] = file_data
logger.info("[PERFORM] File download and processing successful")
return data, True
except Exception as e:
logger.error(f"[PERFORM] File download failed: {e}")
return None, False
elif step_type == 'extract':
logger.info("[PERFORM] Extract action (already extracted)")
return data, True
elif step_type == 'compute':
logger.info("[PERFORM] Compute action (will handle in solve phase)")
return data, True
else:
logger.warning(f"[PERFORM] Unknown step type: {step_type}")
return data, True
async def llm_solve(data: QuizData, understanding: Dict[str, Any], context: QuizContext) -> Optional[Dict[str, Any]]:
logger.info("[LLM_SOLVE] ========== PHASE 2: SOLVING QUIZ ==========")
logger.info("[LLM_SOLVE] Sending all data to Gemini for solving...")
quiz_data = serialize_quiz_data(data)
processed_files = data.files.get('processed', {})
# Preprocess large datasets before sending to LLM
if processed_files:
logger.info("[LLM_SOLVE] Preprocessing file data to avoid safety filters...")
processed_files = preprocess_data_for_llm(processed_files, max_rows=50)
logger.info("[LLM_SOLVE] File data preprocessed successfully")
prompt = f"""You are solving a quiz. You have been given comprehensive data and must determine the correct answer.
QUIZ DATA:
{json.dumps(quiz_data, indent=2)}
INTERPRETATION FROM PHASE 1:
Understanding: {understanding.get('understanding', 'N/A')}
Identified Required Steps:
{json.dumps(understanding.get('needed_steps', []), indent=2)}
PROCESSED FILE DATA (after executing required steps):
{json.dumps(processed_files, indent=2, default=str)}
AVAILABLE CONTEXT VARIABLES:
{json.dumps(context.variables, indent=2)}
INSTRUCTIONS FOR SOLVING:
1. Review the understanding and steps taken
2. Analyze all available data: visible text, decoded content, tables, links, files, forms
3. If computation is needed, show your work step-by-step
4. If data needs to be extracted or aggregated, identify all relevant values
5. If navigation is required, determine the correct path
6. Provide the final answer with high confidence
Based on ALL the comprehensive information provided, solve the quiz and respond with ONLY valid JSON (no markdown, no code blocks):
{{
"analysis": "<Detailed step-by-step analysis of how you arrived at the answer>",
"answer": <string | number | boolean | json object | array>, // ONLY THE ANSWER VALUE, NOT THE FULL PAYLOAD
"confidence": 0.0-1.0,
"reasoning": "<Brief explanation of the answer>",
"requires_navigation": true | false
}}
Requirements:
- The "answer" field should contain ONLY the answer value to submit
- DO NOT include email, secret, or url in your response
- DO NOT construct the submission payload - just provide the answer value
- DO NOT return null or None for the answer field
- The answer should be the exact value needed for submission
- If numeric: provide the number
- If text: provide the string
- If calculation: show work and provide result
- If URL/path: provide the exact path
- If boolean: provide true or false
- If JSON: provide the complete object
- If array: provide the complete array
- If file data: provide as base64 or structured format
Work through this carefully. Take your time to analyze all data comprehensively.
Return ONLY the JSON object without markdown formatting or code blocks."""
system_instruction = """You are an expert quiz solver with the following capabilities:
- Data analysis and extraction
- Mathematical computation
- Table processing and aggregation
- PDF/CSV/XLSX parsing and analysis
- Text extraction and OCR
- Pattern recognition
- Logical reasoning
- URL construction
- JSON/API response interpretation
Your instructions:
1. Analyze all provided data thoroughly
2. Work through calculations step-by-step
3. Extract and aggregate data systematically
4. Provide detailed reasoning
5. Always return a definitive answer (never null/None)
6. Return structured JSON format without markdown code blocks
7. Be precise and accurate
Take whatever time is needed to solve this correctly."""
logger.info("[LLM_SOLVE] Calling Gemini 2.5 Pro with 4000 tokens...")
result = await call_llm_gemini(prompt, system_instruction, max_tokens=4000)
if not result:
logger.error("[LLM_SOLVE] β Gemini returned None")
return None
answer = result.get('answer')
if answer is None:
logger.warning("[LLM_SOLVE] Answer is None, attempting recovery strategies")
reasoning = result.get('reasoning', '')
analysis = result.get('analysis', '')
if reasoning:
logger.info(f"[LLM_SOLVE] Using reasoning as fallback: {reasoning[:100]}")
result['answer'] = reasoning
answer = reasoning
elif analysis:
logger.info(f"[LLM_SOLVE] Using analysis as fallback: {analysis[:100]}")
result['answer'] = analysis
answer = analysis
else:
logger.error("[LLM_SOLVE] Cannot extract answer from any field")
return None
logger.info(f"[LLM_SOLVE] β Final answer determined: {answer}")
logger.info(f"[LLM_SOLVE] Confidence: {result.get('confidence', 0)}")
logger.info(f"[LLM_SOLVE] Analysis: {result.get('analysis', '')[:200]}")
logger.info("[LLM_SOLVE] ========== SOLVING PHASE COMPLETE ==========")
return result
async def submit_answer(email: str, secret: str, quiz_url: str, answer: Any, base_url: str) -> Tuple[bool, Optional[str]]:
logger.info(f"[SUBMIT] Posting answer to {base_url}/submit")
# If answer is a dict with email/secret/url, extract just the answer field
if isinstance(answer, dict):
logger.debug(f"[SUBMIT] Answer is dict with keys: {answer.keys()}")
if 'answer' in answer:
logger.info("[SUBMIT] Extracting 'answer' field from dict")
actual_answer = answer['answer']
else:
logger.warning("[SUBMIT] Dict doesn't have 'answer' field, using whole dict")
actual_answer = answer
else:
actual_answer = answer
payload = {
'email': email,
'secret': secret, # Always use the secret from function parameter
'url': quiz_url,
'answer': actual_answer
}
logger.info(f"[SUBMIT] Payload preview: email={email}, url={quiz_url}, answer={actual_answer}")
logger.debug(f"[SUBMIT] Secret used:" if secret else "[SUBMIT] No secret")
payload_json = json.dumps(payload)
if len(payload_json) > 1_000_000:
logger.error(f"[SUBMIT] Payload exceeds 1MB: {len(payload_json)} bytes")
return False, None
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(f"{base_url}/submit", json=payload)
response.raise_for_status()
result = response.json()
logger.info(f"[SUBMIT] ===== SUBMISSION RESPONSE =====")
logger.info(f"[SUBMIT] Full Response: {json.dumps(result, indent=2)}")
logger.info(f"[SUBMIT] ================================")
is_correct = result.get('correct', False)
next_url = result.get('url')
reason = result.get('reason', '')
if is_correct:
logger.info("[SUBMIT] βββ ANSWER IS CORRECT βββ")
if next_url:
logger.info(f"[SUBMIT] Next URL provided: {next_url}")
else:
logger.info("[SUBMIT] No next URL - Quiz chain may be complete")
return True, next_url
else:
logger.warning("[SUBMIT] βββ ANSWER IS INCORRECT βββ")
logger.warning(f"[SUBMIT] Reason: {reason}")
if next_url:
logger.warning(f"[SUBMIT] Next URL provided despite wrong answer: {next_url}")
return False, next_url
except httpx.HTTPStatusError as e:
logger.error(f"[SUBMIT] βββ HTTP ERROR {e.response.status_code} βββ")
logger.error(f"[SUBMIT] Status Code: {e.response.status_code}")
try:
error_detail = e.response.json()
logger.error(f"[SUBMIT] Error Response: {json.dumps(error_detail, indent=2)}")
except:
logger.error(f"[SUBMIT] Error Text: {e.response.text}")
return False, None
except Exception as e:
logger.error(f"[SUBMIT] βββ SUBMISSION ERROR βββ")
logger.error(f"[SUBMIT] Error: {type(e).__name__}: {str(e)}")
return False, None
async def process_quiz(quiz_url: str, email: str, secret: str, base_url: str) -> Tuple[bool, Optional[str]]:
logger.info(f"[QUIZ] Starting: {quiz_url}")
context = QuizContext(email, quiz_url)
try:
logger.debug("[QUIZ] Step 1: Fetch page")
html = await fetch_page(quiz_url)
if not html:
logger.error("[QUIZ] Failed to fetch page")
return False, None
logger.debug("[QUIZ] Step 2: Extract data")
data = extract_data(html, quiz_url)
logger.debug("[QUIZ] Step 3: First LLM call - Understanding phase")
understanding = await llm_understand(data, context)
if not understanding:
logger.error("[QUIZ] Understanding phase failed")
return False, None
logger.debug("[QUIZ] Step 4: Perform steps if needed")
needed_steps = understanding.get('needed_steps', [])
for step in needed_steps:
if step.get('type') != 'solve':
new_data, step_success = await perform_step(step, data, base_url, context)
if not step_success:
logger.error(f"[QUIZ] Step failed: {step.get('type')}")
return False, None
if new_data:
data = new_data
if step.get('type') == 'scrape_url':
logger.debug("[QUIZ] Re-extracting data after scrape")
data.decoded_content = extract_base64_content(data.html)
logger.debug("[QUIZ] Step 5: Second LLM call - Solving phase")
solution = await llm_solve(data, understanding, context)
if not solution:
logger.error("[QUIZ] Solving phase failed")
return False, None
answer = solution.get('answer')
if answer is None:
logger.error("[QUIZ] No answer generated even after fallback")
return False, None
logger.debug("[QUIZ] Step 6: Submit answer")
is_correct, next_url = await submit_answer(email, secret, quiz_url, answer, base_url)
logger.info(f"[QUIZ] ========== QUIZ RESULT ==========")
logger.info(f"[QUIZ] Correct: {is_correct}")
logger.info(f"[QUIZ] Next URL: {next_url if next_url else 'None (quiz complete or failed)'}")
logger.info(f"[QUIZ] =====================================")
if is_correct or next_url:
return True, next_url
return is_correct, None
except Exception as e:
logger.error(f"[QUIZ] Error: {e}", exc_info=True)
return False, None
async def process_request(data: Dict[str, Any]):
email = data.get("email")
secret = data.get("secret")
initial_url = data.get("url")
logger.info(f"\n{'='*70}")
logger.info(f"[CHAIN] Starting for {email}")
logger.info(f"[CHAIN] Initial URL: {initial_url}")
logger.info(f"[CHAIN] Extended timeout: 300 seconds (5 minutes)")
logger.info(f"[CHAIN] Using Gemini 2.5 Pro")
logger.info(f"{'='*70}\n")
quiz_count = 0
max_quizzes = 30
start_time = asyncio.get_event_loop().time()
timeout_seconds = 700
current_url = initial_url if initial_url.startswith("http") else f"{BASE_URL}{initial_url}"
while quiz_count < max_quizzes:
elapsed = asyncio.get_event_loop().time() - start_time
if elapsed > timeout_seconds:
logger.warning(f"[TIMEOUT] Exceeded {timeout_seconds}s")
break
remaining = timeout_seconds - elapsed
logger.info(f"[CHAIN] Quiz {quiz_count + 1}/{max_quizzes} | {elapsed:.1f}s elapsed | {remaining:.1f}s remaining")
try:
success, next_url = await process_quiz(current_url, email, secret, BASE_URL)
if not success:
logger.error("[CHAIN] Quiz failed")
break
if not next_url:
logger.info("[CHAIN] Quiz chain complete")
break
quiz_count += 1
current_url = next_url
except Exception as e:
logger.error(f"[CHAIN] Error: {e}", exc_info=True)
break
logger.info(f"\n{'='*70}")
logger.info(f"[CHAIN] Completed {quiz_count} quizzes in {asyncio.get_event_loop().time() - start_time:.1f}s")
logger.info(f"{'='*70}\n")
@app.post("/receive_request")
async def receive_request(request: Request, background_tasks: BackgroundTasks):
logger.info("[API] POST /receive_request")
try:
data = await request.json()
except Exception as e:
logger.error(f"[API] JSON parse error: {e}")
return JSONResponse(status_code=400, content={"message": "Invalid JSON"})
required = ['email', 'secret', 'url']
missing = [f for f in required if f not in data]
if missing:
logger.warning(f"[API] Missing fields: {missing}")
return JSONResponse(status_code=400, content={"message": f"Missing fields: {missing}"})
if data.get("secret") != SECRET_KEY:
logger.warning("[API] Invalid secret")
return JSONResponse(status_code=403, content={"message": "Forbidden"})
logger.info(f"[API] Valid request for {data.get('email')}")
background_tasks.add_task(process_request, data)
return JSONResponse(status_code=200, content={"message": "Request accepted"})
@app.get("/health")
async def health():
logger.debug("[API] GET /health")
return JSONResponse(status_code=200, content={
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"llm_provider": "Google Gemini 2.5 Pro",
"gemini_configured": bool(GEMINI_API_KEY)
})
@app.get("/")
async def root():
return JSONResponse(status_code=200, content={
"service": "LLM Quiz Solver v6 (Gemini Edition)",
"version": "6.0.0",
"llm_provider": "Google Gemini 2.5 Pro",
"endpoints": {
"/": "GET - Service info",
"/health": "GET - Health check",
"/receive_request": "POST - Submit quiz request"
}
})
if __name__ == '__main__':
import uvicorn
logger.info("[START] Quiz Solver Service v6 - Gemini Edition")
logger.info(f"[START] GEMINI_API_KEY configured: {bool(GEMINI_API_KEY)}")
logger.info(f"[START] SECRET_KEY configured: {bool(SECRET_KEY)}")
logger.info(f"[START] LLM Provider: Google Gemini 2.5 Pro")
logger.info(f"[START] Rate Limits: 15 req/min, 1M tokens/min, 1500 req/day")
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|