File size: 17,953 Bytes
43da00a
7da4369
c777509
10e9b7d
c777509
 
7da4369
10e9b7d
3c4371f
7da4369
ffa4ee4
 
 
c777509
 
10e9b7d
ffa4ee4
 
 
 
 
e80aab9
3db6293
ffa4ee4
c777509
ffa4ee4
 
 
e80aab9
c777509
 
 
 
31243f4
1c5b21d
 
 
c777509
 
 
43da00a
 
 
c777509
 
 
43da00a
c777509
 
 
 
ffa4ee4
43da00a
 
 
 
ffa4ee4
a718e51
05454e1
ffa4ee4
c777509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffa4ee4
43da00a
c777509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43da00a
05454e1
 
43da00a
c777509
43da00a
c777509
 
43da00a
c777509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05454e1
c777509
05454e1
 
c777509
 
 
 
 
 
 
05454e1
c777509
 
 
05454e1
c777509
 
05454e1
c777509
 
 
 
 
 
 
 
 
 
 
05454e1
c777509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43da00a
 
c777509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4021bf3
05454e1
c777509
 
 
 
 
 
3c4371f
7e4a06b
31243f4
 
e80aab9
c777509
31243f4
c777509
31243f4
c777509
31243f4
3c4371f
c777509
 
 
 
 
eccf8e4
c777509
31243f4
7d65c66
31243f4
c777509
31243f4
c777509
 
31243f4
c777509
31243f4
 
e80aab9
c777509
05454e1
c777509
05454e1
c777509
 
31243f4
 
c777509
31243f4
c777509
 
 
 
 
 
e80aab9
 
c777509
7d65c66
e80aab9
 
c777509
31243f4
e80aab9
 
3c4371f
 
 
e80aab9
c777509
31243f4
 
c777509
7d65c66
c777509
31243f4
c777509
e80aab9
c777509
 
 
0ee0419
e514fd7
 
 
c777509
 
 
 
 
 
 
 
 
 
e514fd7
 
 
e80aab9
 
7e4a06b
e80aab9
c777509
 
 
 
e80aab9
c777509
7d65c66
e80aab9
c777509
 
 
 
 
 
 
 
 
 
 
 
31243f4
 
e80aab9
 
 
c777509
 
 
 
7d65c66
c777509
 
 
7d65c66
c777509
7d65c66
c777509
 
 
 
 
3c4371f
c777509
 
 
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
import asyncio
import inspect
import json
import os
import time
from typing import Any, Dict, List, Optional

import gradio as gr
import pandas as pd
import requests
from dotenv import load_dotenv
from langchain_community.chat_models import ChatHuggingFace
from langchain_community.llms import HuggingFaceEndpoint
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.tools import StructuredTool

from tools import (absolute, add, divide, exponential, floor_divide,
                   get_current_time_in_timezone, logarithm, modulus, multiply,
                   power, roman_calculator_converter, square_root, subtract,
                   web_search)

# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
MAX_AGENT_ITERATIONS = 15
MAX_CONCURRENT_REQUESTS = 5  # Limit concurrent requests to avoid overwhelming the API

load_dotenv()
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")

# Global cache for answers
answer_cache = {}

class ImprovedAgent:
    def __init__(self):
        if not HUGGINGFACEHUB_API_TOKEN:
            raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN.")

        print("ImprovedAgent initialized.")
        
        # Initialize LLM with better parameters
        self.llm = HuggingFaceEndpoint(
            repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
            huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
            temperature=0.1,  # Lower temperature for more consistent responses
            max_new_tokens=1024,
            timeout=30,
        )
        
        self.chat = ChatHuggingFace(llm=self.llm, verbose=False)
        
        # Initialize tools
        self.tools = [
            multiply, add, subtract, power, divide, modulus,
            square_root, floor_divide, absolute, logarithm,
            exponential, web_search, roman_calculator_converter,
            get_current_time_in_timezone
        ]
        
        self.chat_with_tools = self.chat.bind_tools(self.tools)
        print(f"Total tools available: {len(self.tools)}")
        
        # Create tool mapping for easier access
        self.tool_map = {tool.name: tool for tool in self.tools}

    def _extract_tool_calls(self, response) -> List[Dict]:
        """Extract tool calls from the response"""
        tool_calls = []
        if hasattr(response, 'tool_calls') and response.tool_calls:
            for tool_call in response.tool_calls:
                tool_calls.append({
                    'name': tool_call['name'],
                    'args': tool_call['args']
                })
        return tool_calls

    def _execute_tool_calls(self, tool_calls: List[Dict]) -> List[str]:
        """Execute tool calls and return results"""
        results = []
        for tool_call in tool_calls:
            tool_name = tool_call['name']
            tool_args = tool_call['args']
            
            if tool_name in self.tool_map:
                try:
                    tool = self.tool_map[tool_name]
                    result = tool.invoke(tool_args)
                    results.append(f"Tool {tool_name} result: {result}")
                except Exception as e:
                    results.append(f"Tool {tool_name} error: {str(e)}")
            else:
                results.append(f"Unknown tool: {tool_name}")
        
        return results

    async def answer(self, question: str) -> str:
        """Improved answer method with better error handling and tool usage"""
        print(f"Processing question: {question[:100]}...")
        
        try:
            # Create system prompt for better instruction following
            system_prompt = """You are a helpful AI assistant with access to various tools. 
            When answering questions, use the appropriate tools when needed and provide clear, concise answers.
            If you need to perform calculations, use the math tools available.
            If you need current information, use the web search tool.
            Always provide a final answer after using tools."""
            
            messages = [
                HumanMessage(content=f"{system_prompt}\n\nQuestion: {question}")
            ]
            
            # Initial response
            response = await asyncio.to_thread(self.chat_with_tools.invoke, messages)
            
            # Handle tool calls if present
            max_iterations = 3
            iteration = 0
            
            while iteration < max_iterations:
                tool_calls = self._extract_tool_calls(response)
                
                if not tool_calls:
                    break
                
                # Execute tool calls
                tool_results = self._execute_tool_calls(tool_calls)
                
                # Add tool results to conversation
                messages.append(AIMessage(content=response.content))
                messages.append(HumanMessage(content=f"Tool results: {'; '.join(tool_results)}. Please provide a final answer based on these results."))
                
                # Get next response
                response = await asyncio.to_thread(self.chat_with_tools.invoke, messages)
                iteration += 1
            
            # Extract final answer
            final_answer = response.content.strip()
            
            # Clean up the response - remove any tool call artifacts
            if "Tool " in final_answer and "result:" in final_answer:
                # Try to extract just the final answer part
                lines = final_answer.split('\n')
                for line in reversed(lines):
                    if line.strip() and not line.startswith('Tool ') and not 'result:' in line:
                        final_answer = line.strip()
                        break
            
            return final_answer
            
        except Exception as e:
            print(f"Error in answer method: {e}")
            return f"Error processing question: {str(e)}"

    def answer_sync(self, question: str) -> str:
        """Synchronous version of answer method"""
        try:
            return asyncio.run(self.answer(question))
        except Exception as e:
            print(f"Error in sync answer: {e}")
            return f"Error: {str(e)}"

async def process_questions_batch(agent, questions_batch, semaphore):
    """Process a batch of questions with rate limiting"""
    results = []
    
    async def process_single_question(task_id, question):
        async with semaphore:
            try:
                # Check cache first
                cache_key = f"{task_id}_{hash(question)}"
                if cache_key in answer_cache:
                    print(f"Using cached answer for task {task_id}")
                    return task_id, question, answer_cache[cache_key], None
                
                answer = await agent.answer(question)
                
                # Cache the result
                answer_cache[cache_key] = answer
                
                return task_id, question, answer, None
            except Exception as e:
                print(f"Error processing task {task_id}: {e}")
                return task_id, question, None, str(e)
    
    # Create semaphore for rate limiting
    tasks = []
    for item in questions_batch:
        task_id = item.get("task_id")
        question_text = item.get("question")
        if task_id and question_text is not None:
            tasks.append(process_single_question(task_id, question_text))
    
    if tasks:
        results = await asyncio.gather(*tasks, return_exceptions=True)
    
    return results

async def run_agent_async_improved(agent, questions_data):
    """Improved async processing with batching and caching"""
    results_log, answers_payload = [], []
    
    # Create semaphore for rate limiting
    semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
    
    # Process questions in batches
    batch_size = 10
    batches = [questions_data[i:i + batch_size] for i in range(0, len(questions_data), batch_size)]
    
    print(f"Processing {len(questions_data)} questions in {len(batches)} batches...")
    
    for i, batch in enumerate(batches):
        print(f"Processing batch {i+1}/{len(batches)} ({len(batch)} questions)...")
        
        try:
            batch_results = await process_questions_batch(agent, batch, semaphore)
            
            for result in batch_results:
                if isinstance(result, Exception):
                    print(f"Batch processing error: {result}")
                    continue
                
                task_id, question, answer, error = result
                
                if error:
                    print(f"Error in task {task_id}: {error}")
                    results_log.append({
                        "Task ID": task_id, 
                        "Question": question[:100] + "..." if len(question) > 100 else question,
                        "Submitted Answer": f"ERROR: {error}"
                    })
                else:
                    answers_payload.append({"task_id": task_id, "submitted_answer": answer})
                    results_log.append({
                        "Task ID": task_id, 
                        "Question": question[:100] + "..." if len(question) > 100 else question,
                        "Submitted Answer": answer[:200] + "..." if len(answer) > 200 else answer
                    })
            
            # Small delay between batches to be respectful
            if i < len(batches) - 1:
                await asyncio.sleep(1)
                
        except Exception as e:
            print(f"Error processing batch {i+1}: {e}")
            # Continue with next batch
            continue
    
    return results_log, answers_payload

def cache_answers(profile: gr.OAuthProfile | None):
    """Cache answers without submitting"""
    if not profile:
        return "Please log in to Hugging Face first.", None
    
    username = profile.username
    print(f"Caching answers for user: {username}")
    
    # Fetch questions
    api_url = DEFAULT_API_URL
    questions_url = f"{api_url}/questions"
    
    try:
        response = requests.get(questions_url, timeout=15)
        response.raise_for_status()
        questions_data = response.json()
        
        if not questions_data:
            return "No questions found.", None
        
        print(f"Fetched {len(questions_data)} questions for caching.")
        
        # Initialize agent
        agent = ImprovedAgent()
        
        # Process questions
        results_log, answers_payload = asyncio.run(run_agent_async_improved(agent, questions_data))
        
        # Store in global cache with username
        answer_cache[f"user_{username}"] = answers_payload
        
        status = f"Cached {len(answers_payload)} answers for user {username}. Ready to submit!"
        results_df = pd.DataFrame(results_log)
        
        return status, results_df
        
    except Exception as e:
        print(f"Error caching answers: {e}")
        return f"Error caching answers: {e}", None

def submit_cached_answers(profile: gr.OAuthProfile | None):
    """Submit previously cached answers"""
    if not profile:
        return "Please log in to Hugging Face first.", None
    
    username = profile.username
    cache_key = f"user_{username}"
    
    if cache_key not in answer_cache:
        return "No cached answers found. Please run 'Cache Answers' first.", None
    
    answers_payload = answer_cache[cache_key]
    
    if not answers_payload:
        return "No answers to submit.", None
    
    # Get space info
    space_id = os.getenv("SPACE_ID")
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Unknown"
    
    # Submit
    api_url = DEFAULT_API_URL
    submit_url = f"{api_url}/submit"
    
    submission_data = {
        "username": username.strip(),
        "agent_code": agent_code,
        "answers": answers_payload
    }
    
    try:
        print(f"Submitting {len(answers_payload)} cached answers...")
        response = requests.post(submit_url, json=submission_data, timeout=60)
        response.raise_for_status()
        result_data = response.json()
        
        final_status = (
            f"Submission Successful!\n"
            f"User: {result_data.get('username')}\n"
            f"Overall Score: {result_data.get('score', 'N/A')}% "
            f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
            f"Message: {result_data.get('message', 'No message received.')}"
        )
        
        # Clear cache after successful submission
        if cache_key in answer_cache:
            del answer_cache[cache_key]
        
        return final_status, None
        
    except Exception as e:
        print(f"Submission error: {e}")
        return f"Submission failed: {e}", None

def run_and_submit_all(profile: gr.OAuthProfile | None):
    """Original function - now improved with better error handling"""
    if not profile:
        return "Please log in to Hugging Face first.", None

    username = profile.username
    print(f"User logged in: {username}")

    api_url = DEFAULT_API_URL
    questions_url = f"{api_url}/questions"
    submit_url = f"{api_url}/submit"

    # Initialize agent
    try:
        agent = ImprovedAgent()
    except Exception as e:
        print(f"Error initializing agent: {e}")
        return f"Error initializing agent: {e}", None

    # Get space info
    space_id = os.getenv("SPACE_ID")
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Unknown"

    # Fetch questions
    try:
        print(f"Fetching questions from: {questions_url}")
        response = requests.get(questions_url, timeout=15)
        response.raise_for_status()
        questions_data = response.json()
        
        if not questions_data:
            return "No questions found.", None
        
        print(f"Fetched {len(questions_data)} questions.")
    except Exception as e:
        print(f"Error fetching questions: {e}")
        return f"Error fetching questions: {e}", None

    # Process questions
    try:
        results_log, answers_payload = asyncio.run(run_agent_async_improved(agent, questions_data))
    except Exception as e:
        print(f"Error processing questions: {e}")
        return f"Error processing questions: {e}", None

    if not answers_payload:
        return "No answers generated.", pd.DataFrame(results_log) if results_log else None

    # Submit answers
    submission_data = {
        "username": username.strip(),
        "agent_code": agent_code,
        "answers": answers_payload
    }

    try:
        print(f"Submitting {len(answers_payload)} answers...")
        response = requests.post(submit_url, json=submission_data, timeout=60)
        response.raise_for_status()
        result_data = response.json()

        final_status = (
            f"Submission Successful!\n"
            f"User: {result_data.get('username')}\n"
            f"Overall Score: {result_data.get('score', 'N/A')}% "
            f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
            f"Message: {result_data.get('message', 'No message received.')}"
        )

        results_df = pd.DataFrame(results_log)
        return final_status, results_df

    except Exception as e:
        print(f"Submission error: {e}")
        results_df = pd.DataFrame(results_log)
        return f"Submission failed: {e}", results_df

# --- Build Gradio Interface ---
with gr.Blocks(title="Improved Agent Evaluation") as demo:
    gr.Markdown("# Improved Agent Evaluation Runner")
    gr.Markdown(
        """
        **Instructions:**

        1. Log in to your Hugging Face account using the button below.
        2. **Recommended**: Use "Cache Answers" to process all questions first, then "Submit Cached Answers" to submit them.
        3. **Alternative**: Use "Run & Submit All" for the original one-step process.

        **Improvements:**
        - βœ… Async processing with rate limiting
        - βœ… Answer caching for faster resubmissions
        - βœ… Better error handling and recovery
        - βœ… Batch processing to avoid timeouts
        - βœ… Improved tool usage and response parsing

        ---
        """
    )

    gr.LoginButton()

    with gr.Row():
        cache_button = gr.Button("πŸ”„ Cache Answers", variant="secondary")
        submit_button = gr.Button("πŸ“€ Submit Cached Answers", variant="primary")
        run_all_button = gr.Button("πŸš€ Run & Submit All", variant="secondary")

    status_output = gr.Textbox(label="Status", lines=6, interactive=False)
    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)

    # Wire up the buttons
    cache_button.click(
        fn=cache_answers,
        outputs=[status_output, results_table]
    )
    
    submit_button.click(
        fn=submit_cached_answers,
        outputs=[status_output, results_table]
    )
    
    run_all_button.click(
        fn=run_and_submit_all,
        outputs=[status_output, results_table]
    )

if __name__ == "__main__":
    print("\n" + "-"*30 + " Improved App Starting " + "-"*30)
    
    space_host = os.getenv("SPACE_HOST")
    space_id = os.getenv("SPACE_ID")

    if space_host:
        print(f"βœ… SPACE_HOST: {space_host}")
        print(f"   Runtime URL: https://{space_host}.hf.space")
    else:
        print("ℹ️  Running locally - SPACE_HOST not found.")

    if space_id:
        print(f"βœ… SPACE_ID: {space_id}")
        print(f"   Repo URL: https://huggingface.co/spaces/{space_id}")
    else:
        print("ℹ️  SPACE_ID not found.")

    print("-" * 76 + "\n")
    print("Launching Improved Gradio Interface...")
    demo.launch(debug=True, share=False)