File size: 13,885 Bytes
10e9b7d
 
eccf8e4
7d65c66
620f572
3c4371f
c275bbd
3164d5a
 
 
7067f57
3164d5a
e80aab9
3db6293
e80aab9
3164d5a
 
 
 
8b49454
3164d5a
31243f4
8b49454
c275bbd
8b49454
 
 
6e735ee
31243f4
8b49454
 
 
 
 
 
6e735ee
 
76bb81b
6e735ee
8b49454
3164d5a
6e735ee
 
 
8b49454
6e735ee
8b49454
6e735ee
 
 
8b49454
 
 
 
6e735ee
8b49454
 
 
6e735ee
8b49454
 
 
6e735ee
 
 
8b49454
 
 
d4b02ec
4021bf3
3164d5a
31243f4
3164d5a
31243f4
3164d5a
 
7e4a06b
31243f4
3164d5a
31243f4
eccf8e4
31243f4
7d65c66
31243f4
3164d5a
31243f4
3164d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
31243f4
7d65c66
3164d5a
31243f4
3164d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31243f4
3164d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
3164d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31243f4
3164d5a
e80aab9
7d65c66
e80aab9
 
3164d5a
31243f4
e80aab9
 
3c4371f
 
 
e80aab9
3164d5a
 
 
 
 
 
 
 
 
 
31243f4
 
3164d5a
e80aab9
3c4371f
e80aab9
 
3c4371f
3164d5a
7d65c66
3164d5a
 
3c4371f
3164d5a
 
7d65c66
3164d5a
e80aab9
3164d5a
 
 
 
 
 
 
 
 
 
 
 
 
0ee0419
e514fd7
3164d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e514fd7
 
e80aab9
 
3164d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
3164d5a
 
 
 
 
 
 
 
 
 
e80aab9
3164d5a
 
 
 
 
e80aab9
3164d5a
 
 
46a7b3e
e80aab9
 
 
3164d5a
 
3c4371f
3164d5a
7d65c66
3c4371f
 
7d65c66
3c4371f
7d65c66
 
3164d5a
7d65c66
 
 
 
 
 
3164d5a
3c4371f
3164d5a
3c4371f
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
import os
import gradio as gr
import requests
import inspect
import time
import pandas as pd
from smolagents import DuckDuckGoSearchTool
import threading
from typing import Dict, List, Optional, Tuple
import json
from huggingface_hub import InferenceClient

# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"

# --- Global Cache for Answers ---
cached_answers = {}
cached_questions = []
processing_status = {"is_processing": False, "progress": 0, "total": 0}

# --- Basic Agent Definition ---
class BasicAgent:
    def __init__(self, debug: bool = False):
        self.search = DuckDuckGoSearchTool()
        self.debug = debug
        if self.debug:
            print("BasicAgent initialized.")

    def __call__(self, question: str) -> str:
        if self.debug:
            print(f"Agent received question: {question}")

        # Early validation
        if not question or not question.strip():
            return "Please provide a valid question."

        try:
            time.sleep(1)
            results = self.search(question)
            
            # Use truthfulness check and early return
            if not results:
                return "No results found for that query."

            # Direct access with get() method chaining
            top = results[0]
            title = top.get("title") or "No title"
            snippet = top.get("snippet", "").strip()
            link = top.get("link", "")

            # Build answer more efficiently
            parts = [f"**{title}**"]
            if snippet:
                parts.append(snippet)
            if link:
                parts.append(f"Source: {link}")
            
            answer = "\n".join(parts)

        except (IndexError, KeyError, AttributeError):
            # More specific exception handling
            answer = "Sorry, I couldn't process the search results properly."
        except Exception as e:
            answer = f"Sorry, I couldn't fetch results due to: {e}"

        if self.debug:
            print(f"Agent returning answer: {answer}")
        
        return answer

def fetch_questions() -> Tuple[str, Optional[pd.DataFrame]]:
    """
    Fetch questions from the API and cache them.
    """
    global cached_questions
    
    api_url = DEFAULT_API_URL
    questions_url = f"{api_url}/questions"
    
    print(f"Fetching questions from: {questions_url}")
    try:
        response = requests.get(questions_url, timeout=15)
        response.raise_for_status()
        questions_data = response.json()
        
        if not questions_data:
            return "Fetched questions list is empty.", None
            
        cached_questions = questions_data
        
        # Create DataFrame for display
        display_data = []
        for item in questions_data:
            display_data.append({
                "Task ID": item.get("task_id", "Unknown"),
                "Question": item.get("question", "")
            })
        
        df = pd.DataFrame(display_data)
        status_msg = f"Successfully fetched {len(questions_data)} questions. Ready to generate answers."
        
        return status_msg, df
        
    except requests.exceptions.RequestException as e:
        return f"Error fetching questions: {e}", None
    except Exception as e:
        return f"An unexpected error occurred: {e}", None

def generate_answers_async(progress_callback=None):
    """
    Generate answers for all cached questions asynchronously.
    """
    global cached_answers, processing_status
    
    if not cached_questions:
        return "No questions available. Please fetch questions first."
    
    processing_status["is_processing"] = True
    processing_status["progress"] = 0
    processing_status["total"] = len(cached_questions)
    
    try:
        agent = BasicAgent()
        cached_answers = {}
        
        for i, item in enumerate(cached_questions):
            if not processing_status["is_processing"]:  # Check if cancelled
                break
                
            task_id = item.get("task_id")
            question_text = item.get("question")
            
            if not task_id or question_text is None:
                continue
                
            try:
                answer = agent(question_text)
                cached_answers[task_id] = {
                    "question": question_text,
                    "answer": answer
                }
            except Exception as e:
                cached_answers[task_id] = {
                    "question": question_text,
                    "answer": f"AGENT ERROR: {e}"
                }
            
            processing_status["progress"] = i + 1
            if progress_callback:
                progress_callback(i + 1, len(cached_questions))
                
    except Exception as e:
        print(f"Error in generate_answers_async: {e}")
    finally:
        processing_status["is_processing"] = False

def start_answer_generation():
    """
    Start the answer generation process in a separate thread.
    """
    if processing_status["is_processing"]:
        return "Answer generation is already in progress.", None
    
    if not cached_questions:
        return "No questions available. Please fetch questions first.", None
    
    # Start generation in background thread
    thread = threading.Thread(target=generate_answers_async)
    thread.daemon = True
    thread.start()
    
    return "Answer generation started. Check progress below.", None

def get_generation_progress():
    """
    Get the current progress of answer generation.
    """
    if not processing_status["is_processing"] and processing_status["progress"] == 0:
        return "Not started", None
    
    if processing_status["is_processing"]:
        progress = processing_status["progress"]
        total = processing_status["total"]
        status_msg = f"Generating answers... {progress}/{total} completed"
        return status_msg, None
    else:
        # Generation completed
        if cached_answers:
            # Create DataFrame with results
            display_data = []
            for task_id, data in cached_answers.items():
                display_data.append({
                    "Task ID": task_id,
                    "Question": data["question"][:100] + "..." if len(data["question"]) > 100 else data["question"],
                    "Generated Answer": data["answer"][:200] + "..." if len(data["answer"]) > 200 else data["answer"]
                })
            
            df = pd.DataFrame(display_data)
            status_msg = f"Answer generation completed! {len(cached_answers)} answers ready for submission."
            return status_msg, df
        else:
            return "Answer generation completed but no answers were generated.", None

def submit_cached_answers(profile: gr.OAuthProfile | None):
    """
    Submit the cached answers to the evaluation API.
    """
    global cached_answers
    
    if not profile:
        return "Please log in to Hugging Face first.", None
    
    if not cached_answers:
        return "No cached answers available. Please generate answers first.", None
    
    username = profile.username
    space_id = os.getenv("SPACE_ID")
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Unknown"
    
    # Prepare submission payload
    answers_payload = []
    for task_id, data in cached_answers.items():
        answers_payload.append({
            "task_id": task_id,
            "submitted_answer": data["answer"]
        })
    
    submission_data = {
        "username": username.strip(),
        "agent_code": agent_code,
        "answers": answers_payload
    }
    
    # Submit to API
    api_url = DEFAULT_API_URL
    submit_url = f"{api_url}/submit"
    
    print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
    
    try:
        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.')}"
        )
        
        # Create results DataFrame
        results_log = []
        for task_id, data in cached_answers.items():
            results_log.append({
                "Task ID": task_id,
                "Question": data["question"],
                "Submitted Answer": data["answer"]
            })
        
        results_df = pd.DataFrame(results_log)
        return final_status, results_df
        
    except requests.exceptions.HTTPError as e:
        error_detail = f"Server responded with status {e.response.status_code}."
        try:
            error_json = e.response.json()
            error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
        except:
            error_detail += f" Response: {e.response.text[:500]}"
        return f"Submission Failed: {error_detail}", None
        
    except requests.exceptions.Timeout:
        return "Submission Failed: The request timed out.", None
        
    except Exception as e:
        return f"Submission Failed: {e}", None

def clear_cache():
    """
    Clear all cached data.
    """
    global cached_answers, cached_questions, processing_status
    cached_answers = {}
    cached_questions = []
    processing_status = {"is_processing": False, "progress": 0, "total": 0}
    return "Cache cleared successfully.", None

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

        1. **Clone and Modify**: Clone this space and modify the agent logic as needed.
        2. **Login**: Log in to your Hugging Face account.
        3. **Fetch Questions**: Load all questions from the evaluation API.
        4. **Generate Answers**: Create answers for all questions (runs in background).
        5. **Review Results**: Check the generated answers before submission.
        6. **Submit**: Submit your answers when ready.

        **Benefits of this approach:**
        - ✅ Faster user feedback (separate steps)
        - ✅ Ability to review answers before submission
        - ✅ Progress tracking during answer generation
        - ✅ Cache management for multiple runs
        
        ---
        """
    )

    with gr.Row():
        gr.LoginButton()
        clear_btn = gr.Button("Clear Cache", variant="secondary")

    with gr.Tab("Step 1: Fetch Questions"):
        gr.Markdown("### Fetch Questions from API")
        fetch_btn = gr.Button("Fetch Questions", variant="primary")
        fetch_status = gr.Textbox(label="Fetch Status", lines=2, interactive=False)
        questions_table = gr.DataFrame(label="Available Questions", wrap=True)
        
        fetch_btn.click(
            fn=fetch_questions,
            outputs=[fetch_status, questions_table]
        )

    with gr.Tab("Step 2: Generate Answers"):
        gr.Markdown("### Generate Answers (Background Processing)")
        
        with gr.Row():
            generate_btn = gr.Button("Start Answer Generation", variant="primary")
            refresh_btn = gr.Button("Refresh Progress", variant="secondary")
        
        generation_status = gr.Textbox(label="Generation Status", lines=2, interactive=False)
        answers_preview = gr.DataFrame(label="Generated Answers Preview", wrap=True)
        
        generate_btn.click(
            fn=start_answer_generation,
            outputs=[generation_status, answers_preview]
        )
        
        refresh_btn.click(
            fn=get_generation_progress,
            outputs=[generation_status, answers_preview]
        )

    with gr.Tab("Step 3: Submit Results"):
        gr.Markdown("### Submit Generated Answers")
        submit_btn = gr.Button("Submit Cached Answers", variant="primary")
        submission_status = gr.Textbox(label="Submission Status", lines=5, interactive=False)
        final_results = gr.DataFrame(label="Final Submission Results", wrap=True)
        
        submit_btn.click(
            fn=submit_cached_answers,
            outputs=[submission_status, final_results]
        )

    # Clear cache functionality
    clear_btn.click(
        fn=clear_cache,
        outputs=[fetch_status, questions_table]
    )

    # Auto-refresh progress every 5 seconds when generation is active
    demo.load(
        fn=get_generation_progress,
        outputs=[generation_status, answers_preview]
    )

if __name__ == "__main__":
    print("\n" + "-"*30 + " Enhanced App Starting " + "-"*30)
    
    space_host_startup = os.getenv("SPACE_HOST")
    space_id_startup = os.getenv("SPACE_ID")

    if space_host_startup:
        print(f"✅ SPACE_HOST found: {space_host_startup}")
        print(f"   Runtime URL should be: https://{space_host_startup}.hf.space")
    else:
        print("ℹ️  SPACE_HOST environment variable not found (running locally?).")

    if space_id_startup:
        print(f"✅ SPACE_ID found: {space_id_startup}")
        print(f"   Repo URL: https://huggingface.co/spaces/{space_id_startup}")
        print(f"   Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
    else:
        print("ℹ️  SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")

    print("-"*(60 + len(" Enhanced App Starting ")) + "\n")

    print("Launching Enhanced Gradio Interface...")
    demo.launch(debug=True, share=False)