File size: 11,331 Bytes
574b6ca
 
 
d591a7a
086b425
d591a7a
 
0f20e93
d591a7a
 
 
 
 
8c139ea
9f29ca9
8c139ea
f0b3f91
 
8c139ea
d3c0517
 
 
 
 
8c139ea
9f29ca9
 
 
757ebd9
d3c0517
3db6293
d3c0517
 
9f29ca9
d3c0517
 
9f29ca9
d3c0517
9f29ca9
 
 
e80aab9
d3c0517
f0b3f91
9f29ca9
d3c0517
d591a7a
 
f0b3f91
cccb073
 
 
d3c0517
8c139ea
 
 
 
d3c0517
 
 
d591a7a
25405da
8c139ea
 
9f29ca9
d3c0517
 
 
 
 
 
 
 
 
 
 
d591a7a
 
 
d3c0517
d591a7a
d3c0517
d591a7a
9f29ca9
d3c0517
cccb073
9f29ca9
 
 
8c139ea
d3c0517
9f29ca9
 
d3c0517
 
 
9f29ca9
 
d3c0517
 
 
 
 
d591a7a
 
d3c0517
d591a7a
d3c0517
 
 
 
cccb073
d3c0517
 
d591a7a
 
d3c0517
d591a7a
8c139ea
d3c0517
 
 
d591a7a
 
d3c0517
d591a7a
d3c0517
d591a7a
8c139ea
d3c0517
 
 
d591a7a
 
 
d3c0517
d591a7a
 
 
 
d3c0517
 
bbb34b9
d591a7a
d3c0517
 
 
 
 
d591a7a
 
 
cccb073
0f20e93
8c139ea
d3c0517
cccb073
8c139ea
cccb073
d3c0517
 
 
cccb073
d3c0517
 
 
 
c66203c
d3c0517
 
 
 
8c139ea
d3c0517
cccb073
d3c0517
 
 
8c139ea
d3c0517
8c139ea
 
d3c0517
d591a7a
d3c0517
8c139ea
d3c0517
 
 
8c139ea
d3c0517
 
8c139ea
d3c0517
cccb073
8c139ea
 
d3c0517
8c139ea
 
cccb073
d3c0517
cccb073
8c139ea
 
d3c0517
 
 
8c139ea
cccb073
8c139ea
d3c0517
8c139ea
 
 
 
d3c0517
d591a7a
d3c0517
d591a7a
d3c0517
 
 
 
 
 
 
 
 
 
 
 
 
cccb073
 
d3c0517
03ca047
d3c0517
d591a7a
cccb073
d3c0517
c66203c
cccb073
d3c0517
 
 
d591a7a
d3c0517
 
 
 
 
eccf8e4
d3c0517
 
 
a39e119
d3c0517
d591a7a
d3c0517
 
 
 
8c139ea
d3c0517
bbb34b9
d3c0517
8c139ea
d3c0517
f96a820
8c139ea
d3c0517
 
086b425
d3c0517
 
 
 
 
 
8c139ea
03ca047
d3c0517
 
 
 
 
 
8c139ea
d3c0517
 
 
 
 
 
 
 
cccb073
e80aab9
d3c0517
 
 
 
 
 
 
 
cccb073
d3c0517
 
 
 
 
 
 
d591a7a
cccb073
d3c0517
cccb073
7963312
d3c0517
 
d591a7a
d3c0517
 
 
 
7963312
d3c0517
8c139ea
d3c0517
8c139ea
d3c0517
 
9f29ca9
d3c0517
e80aab9
 
d3c0517
 
9f29ca9
8c139ea
d3c0517
8c139ea
 
d3c0517
8c139ea
 
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
import os
import gradio as gr
import requests
import json
import re
import numexpr
import pandas as pd
import math
import pdfminer
from duckduckgo_search import DDGS
from pdfminer.high_level import extract_text
from bs4 import BeautifulSoup
import html2text
from typing import Dict, Any, List, Tuple, Callable, Optional
from dotenv import load_dotenv
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
import torch
import time
import gc
import warnings

# Suppress warnings
warnings.filterwarnings("ignore")
os.environ["TOKENIZERS_PARALLELISM"] = "false"

# --- Load Environment Variables ---
load_dotenv()
SERPER_API_KEY = os.getenv("SERPER_API_KEY")

# --- Constants (ULTRA FAST MODE) ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
MAX_STEPS = 3  # Reduced to 3
MAX_TOKENS = 64  # Very short responses
MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
TIMEOUT_PER_QUESTION = 15  # 15 seconds max
MAX_CONTEXT = 1024  # Very short context

# --- Configure Environment ---
os.environ["PIP_BREAK_SYSTEM_PACKAGES"] = "1"
os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"
os.environ["BITSANDBYTES_NOWELCOME"] = "1"

print("Loading model (ULTRA FAST mode)...")
start_time = time.time()

# Minimal model loading
model = AutoModelForCausalLM.from_pretrained(
    MODEL_NAME,
    trust_remote_code=True,
    torch_dtype=torch.float32,
    device_map="cpu",
    low_cpu_mem_usage=True,
    use_cache=False
)

tokenizer = AutoTokenizer.from_pretrained(
    MODEL_NAME, 
    use_fast=True,
    trust_remote_code=True,
    padding_side="left"
)

if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

# Pre-compile generation config
GENERATION_CONFIG = GenerationConfig(
    max_new_tokens=MAX_TOKENS,
    temperature=0.3,
    do_sample=True,
    pad_token_id=tokenizer.pad_token_id,
    eos_token_id=tokenizer.eos_token_id,
    use_cache=False,
    repetition_penalty=1.1
)

load_time = time.time() - start_time
print(f"Model loaded in {load_time:.2f} seconds")

# --- Lightning Fast Tools ---
def web_search(query: str) -> str:
    """Ultra-fast web search"""
    try:
        if SERPER_API_KEY:
            params = {'q': query[:100], 'num': 1}  # Single result
            headers = {'X-API-KEY': SERPER_API_KEY, 'Content-Type': 'application/json'}
            response = requests.post(
                'https://google.serper.dev/search',
                headers=headers,
                json=params,
                timeout=3
            )
            results = response.json()
            if 'organic' in results and results['organic']:
                return f"{results['organic'][0]['title']}: {results['organic'][0]['snippet'][:200]}"
            return "No results"
        else:
            with DDGS() as ddgs:
                for result in ddgs.text(query, max_results=1):
                    return f"{result['title']}: {result['body'][:200]}"
            return "No results"
    except:
        return "Search failed"

def calculator(expression: str) -> str:
    """Lightning calculator"""
    try:
        clean_expr = re.sub(r'[^\d+\-*/().\s]', '', str(expression))
        if not clean_expr.strip():
            return "Invalid expression"
        result = eval(clean_expr)  # Simple eval for speed
        return str(float(result))
    except:
        return "Calc error"

def read_pdf(file_path: str) -> str:
    """Fast PDF reader"""
    try:
        text = extract_text(file_path)
        return text[:500] if text else "No PDF text"
    except:
        return "PDF error"

def read_webpage(url: str) -> str:
    """Fast webpage reader"""
    try:
        response = requests.get(url, timeout=3, headers={'User-Agent': 'Bot'})
        soup = BeautifulSoup(response.text, 'html.parser')
        text = soup.get_text(separator=' ', strip=True)
        return text[:500] if text else "No webpage text"
    except:
        return "Webpage error"

TOOLS = {
    "web_search": web_search,
    "calculator": calculator, 
    "read_pdf": read_pdf,
    "read_webpage": read_webpage
}

# --- Ultra Fast Agent ---
class FastGAIA_Agent:
    def __init__(self):
        self.tools = TOOLS
        self.prompt_template = (
            "<|system|>You solve GAIA questions fast. Tools: web_search, calculator, read_pdf, read_webpage.\n"
            "Format: ```json\n{\"tool\": \"name\", \"args\": {\"key\": \"value\"}}```\n"
            "Always end with: Final Answer: [answer]<|end|>\n"
            "<|user|>{history}<|end|>\n<|assistant|>"
        )

    def __call__(self, question: str) -> str:
        start_time = time.time()
        
        try:
            history = f"Question: {question}"
            
            for step in range(MAX_STEPS):
                if time.time() - start_time > TIMEOUT_PER_QUESTION:
                    return "TIMEOUT"
                
                response = self._fast_generate(history)
                
                # Quick final answer check
                if "Final Answer:" in response:
                    answer = response.split("Final Answer:")[-1].strip().split('\n')[0]
                    return answer[:200]  # Limit answer length
                
                # Quick tool parsing
                tool_result = self._quick_tool_use(response)
                if tool_result:
                    history += f"\nAction: {tool_result}"
                else:
                    history += f"\nThought: {response[:100]}"
                
                # Keep history short
                if len(history) > 800:
                    history = history[-800:]
            
            return "No solution found"
            
        except Exception as e:
            return f"Error: {str(e)[:50]}"

    def _fast_generate(self, history: str) -> str:
        try:
            prompt = self.prompt_template.format(history=history)
            
            # Fast tokenization
            inputs = tokenizer(
                prompt,
                return_tensors="pt",
                truncation=True,
                max_length=MAX_CONTEXT,
                padding=False
            )
            
            # Fast generation
            with torch.no_grad():
                outputs = model.generate(
                    inputs.input_ids,
                    generation_config=GENERATION_CONFIG,
                    attention_mask=inputs.attention_mask
                )
            
            # Fast decoding
            response = tokenizer.decode(outputs[0], skip_special_tokens=True)
            response = response.split("<|assistant|>")[-1].strip()
            
            # Immediate cleanup
            del inputs, outputs
            gc.collect()
            
            return response
            
        except Exception as e:
            return f"Gen error: {str(e)}"

    def _quick_tool_use(self, text: str) -> str:
        try:
            # Quick JSON extraction
            json_match = re.search(r'```json\s*({[^}]*})\s*```', text)
            if not json_match:
                return ""
                
            tool_data = json.loads(json_match.group(1))
            tool_name = tool_data.get("tool", "")
            args = tool_data.get("args", {})
            
            if tool_name in self.tools:
                result = self.tools[tool_name](**args)
                return f"Used {tool_name}: {str(result)[:150]}"
            
        except:
            pass
        return ""

# --- Lightning Fast Runner ---
def run_and_submit_all(profile: gr.OAuthProfile | None):
    if not profile:
        return "❌ Please login first", None

    username = profile.username
    
    # Quick setup
    agent = FastGAIA_Agent()
    api_url = DEFAULT_API_URL
    space_id = os.getenv("SPACE_ID", "unknown")
    
    print(f"πŸš€ ULTRA FAST mode - User: {username}")
    
    # Fetch questions quickly
    try:
        response = requests.get(f"{api_url}/questions", timeout=10)
        questions = response.json()
        print(f"πŸ“ Got {len(questions)} questions")
    except Exception as e:
        return f"❌ Failed to get questions: {e}", None

    # Process at lightning speed
    results = []
    answers = []
    start_time = time.time()
    
    for i, item in enumerate(questions):
        task_id = item.get("task_id")
        question = item.get("question", "")
        
        if not task_id:
            continue
            
        print(f"⚑ [{i+1}/{len(questions)}] {task_id[:8]}...")
        
        try:
            answer = agent(question)
            answers.append({"task_id": task_id, "submitted_answer": answer})
            results.append({
                "ID": task_id[:8],
                "Question": question[:60] + "...",
                "Answer": answer[:80] + "..." if len(answer) > 80 else answer
            })
        except Exception as e:
            error_ans = f"ERROR: {str(e)[:30]}"
            answers.append({"task_id": task_id, "submitted_answer": error_ans})
            results.append({
                "ID": task_id[:8],
                "Question": question[:60] + "...",
                "Answer": error_ans
            })
        
        # Quick memory cleanup
        if i % 5 == 0:
            gc.collect()
    
    total_time = time.time() - start_time
    print(f"⏱️  Completed in {total_time:.1f}s ({total_time/len(questions):.1f}s per question)")
    
    # Submit results
    try:
        submission = {
            "username": username,
            "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
            "answers": answers
        }
        
        response = requests.post(f"{api_url}/submit", json=submission, timeout=30)
        result = response.json()
        
        status = (
            f"🎯 ULTRA FAST RESULTS\n"
            f"πŸ‘€ User: {result.get('username', username)}\n"
            f"πŸ“Š Score: {result.get('score', 'N/A')}% "
            f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')})\n"
            f"⏱️  Time: {total_time:.1f}s ({total_time/len(questions):.1f}s/question)\n"
            f"πŸ’¬ {result.get('message', 'Completed!')}"
        )
        
        return status, pd.DataFrame(results)
        
    except Exception as e:
        error_status = f"❌ Submission failed: {str(e)}\n⏱️  Processing time: {total_time:.1f}s"
        return error_status, pd.DataFrame(results)

# --- Ultra Simple UI ---
with gr.Blocks(title="GAIA Agent - ULTRA FAST") as demo:
    gr.Markdown("# ⚑ GAIA Agent - ULTRA FAST MODE")
    gr.Markdown("**Speed settings:** 3 steps max β€’ 64 tokens β€’ 15s timeout β€’ Lightning tools")

    gr.LoginButton()
    
    run_btn = gr.Button("πŸš€ RUN ULTRA FAST", variant="primary", size="lg")
    
    status = gr.Textbox(label="πŸ“Š Results", lines=6, interactive=False)
    table = gr.DataFrame(label="πŸ“‹ Answers", interactive=False)
    
    run_btn.click(run_and_submit_all, outputs=[status, table], show_progress=True)

if __name__ == "__main__":
    print("⚑ ULTRA FAST GAIA Agent Starting...")
    print(f"βš™οΈ  {MAX_STEPS} steps, {MAX_TOKENS} tokens, {TIMEOUT_PER_QUESTION}s timeout")
    
    demo.launch(
        share=True,  # Added share=True for public link
        server_name="0.0.0.0",
        server_port=7860,
        debug=False,
        show_error=True
    )