File size: 36,592 Bytes
40373e7
 
434705e
e2e0e7f
434705e
 
 
941edf5
 
 
 
 
934f812
651a0bc
941edf5
 
651a0bc
 
941edf5
 
651a0bc
 
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
651a0bc
 
941edf5
 
 
651a0bc
 
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaca903
651a0bc
 
eaca903
434705e
651a0bc
434705e
941edf5
434705e
 
eaca903
434705e
 
eaca903
434705e
941edf5
 
 
 
 
434705e
941edf5
434705e
 
 
 
 
 
 
 
 
 
 
651a0bc
941edf5
 
 
 
 
 
651a0bc
bf4c445
434705e
941edf5
 
 
 
 
 
 
 
434705e
 
 
 
 
651a0bc
eaca903
 
434705e
 
651a0bc
eaca903
651a0bc
 
eaca903
 
941edf5
651a0bc
434705e
eaca903
476070a
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf4c445
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
621378e
941edf5
 
bf4c445
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
621378e
 
 
941edf5
621378e
941edf5
 
 
621378e
941edf5
 
 
 
 
 
 
 
 
bf4c445
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
941edf5
651a0bc
941edf5
 
651a0bc
 
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40373e7
 
 
 
 
 
313ba70
 
8b55b5a
 
 
 
 
941edf5
8b55b5a
 
 
 
 
 
 
 
b85b22e
8b55b5a
 
63a4dbe
 
8b55b5a
 
941edf5
8b55b5a
 
941edf5
8b55b5a
 
 
 
b85b22e
8b55b5a
 
 
 
941edf5
8b55b5a
 
 
 
941edf5
 
 
8b55b5a
 
 
 
 
 
 
941edf5
 
8b55b5a
 
 
 
 
 
 
 
 
 
941edf5
8b55b5a
941edf5
 
 
 
8b55b5a
 
941edf5
 
8b55b5a
941edf5
8b55b5a
941edf5
 
8b55b5a
941edf5
8b55b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
941edf5
 
 
 
 
8b55b5a
 
941edf5
 
 
 
8b55b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
b85b22e
 
 
 
 
8b55b5a
 
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b85b22e
651a0bc
b85b22e
7bc047f
 
b85b22e
 
 
 
 
 
941edf5
b85b22e
941edf5
 
7bc047f
 
b85b22e
 
 
941edf5
b85b22e
 
 
 
 
 
bf4c445
b85b22e
7bc047f
bf4c445
941edf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651a0bc
941edf5
b85b22e
651a0bc
b85b22e
651a0bc
 
 
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
print("Application script started.") # Debugging print statement

import os
import gradio as gr
import requests
import inspect
import pandas as pd
import cv2 # Import opencv-python for video processing
import speech_recognition as sr # Import SpeechRecognition for audio processing
from pydub import AudioSegment # Import pydub for audio manipulation
import tempfile # Import tempfile for temporary file handling
import numpy as np # Import numpy for image processing

print("All libraries imported successfully.") # Debugging print

# Import libraries for SerpAPI
# Corrected import: Import GoogleSearch from google_search_results
from google_search_results import GoogleSearch
import google.generativeai as genai # Keep the import as the user might add LLM functionality back later

print("SerpAPI and GenAI libraries imported successfully.") # Debugging print

# Removed the import of google.colab.userdata as it's not available outside Colab
# from google.colab import userdata # To access the API key from secrets


# --- Get API Keys from Environment Variables ---
# SERPAPI_API_KEY and GOOGLE_API_KEY should be set as secrets in your Hugging Face Space
SERPAPI_API_KEY = os.getenv('SERPAPI_API_KEY')
print(f"SERPAPI_API_KEY (first 5 chars): {SERPAPI_API_KEY[:5] if SERPAPI_API_KEY else 'None'}...") # Debugging API key

# Access GOOGLE_API_KEY directly from environment variables using os.getenv()
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
print(f"GOOGLE_API_KEY (first 5 chars): {GOOGLE_API_KEY[:5] if GOOGLE_API_KEY else 'None'}...") # Debugging API key

print("API keys retrieved (or attempted).") # Debugging print

# --- Define the default API URL ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # Updated API URL

print(f"DEFAULT_API_URL set to: {DEFAULT_API_URL}") # Debugging print


# --- Google Generative AI LLM Initialization ---
# Keep LLM initialization but handle potential errors and None state
print("Attempting to initialize Google Generative AI model...") # Debugging print before loading

gemini_model = None # Initialize to None

# The check for GOOGLE_API_KEY and LLM configuration already uses os.getenv()
if not GOOGLE_API_KEY:
    print("Warning: GOOGLE_API_KEY environment variable not set. LLM will not be available.")
else:
    try:
        # Configure the generative AI library
        genai.configure(api_key=GOOGLE_API_KEY)
        print("Google Generative AI configured.")

        # Initialize the Generative Model
        # Using a fast and efficient model like gemini-1.5-flash
        # You can explore other models like 'gemini-1.5-pro' for potentially better results
        gemini_model = genai.GenerativeModel('gemini-1.5-flash')
        print("Gemini model initialized successfully.") # Debugging print after successful init

    except Exception as e:
        print(f"An error occurred during Google Generative AI initialization: {e}")
        gemini_model = None # Ensure model is None if initialization fails

print("LLM initialization attempted.") # Debugging print


# --- Web Search Function (using SerpAPI) ---
print("Defining web_search function...") # Debugging print
def web_search(query: str) -> list[dict]:
    # Removed global gemini_model declaration as it's not used here
    """
    Performs a web search using SerpAPI and returns relevant information.

    Args:
        query: The search query string.

    Returns:
            A list of dictionaries, where each dictionary represents a search result
            with keys 'title', 'snippet', and 'url'. Returns an empty list if no
            results are found or an error occurs.
        """
    print(f"web_search called with query: {query[:50]}...") # Debugging web_search call
    if not SERPAPI_API_KEY:
        print("SerpAPI key not found in environment variables.")
        return []

    params = {
        "q": query,
        "api_key": SERPAPI_API_KEY,
        "engine": "google",  # Use Google search engine
        "num": 5 # Number of results to fetch
    }
    results = []

    try:
        search = GoogleSearch(params) # Use GoogleSearch from the correct package
        search_results_dict = search.get_dict() # Get results as a dictionary
        print(f"SerpAPI raw response keys: {search_results_dict.keys() if isinstance(search_results_dict, dict) else 'Response is not a dictionary'}") # Debugging response keys

        # Log the full SerpAPI response for debugging if organic_results is missing or empty
        if not isinstance(search_results_dict, dict) or "organic_results" not in search_results_dict or not isinstance(search_results_dict["organic_results"], list) or not search_results_dict["organic_results"]:
             print(f"SerpAPI response did not contain organic results or had invalid format. Response: {search_results_dict}")
             search_results = [] # Ensure search_results is empty if no organic results

        # Extract organic results
        # Add check that search_results_dict and organic_results are valid
        if isinstance(search_results_dict, dict) and "organic_results" in search_results_dict and isinstance(search_results_dict["organic_results"], list):
            print(f"Found {len(search_results_dict['organic_results'])} organic results.") # Debugging result count
            for result in search_results_dict["organic_results"]:
                # Add check for None or non-dict result item
                if result is None or not isinstance(result, dict):
                    print(f"Skipping invalid search result item: {result}")
                    continue
                item = {
                    'title': result.get('title'),
                    'url': result.get('link'),
                    'snippet': result.get('snippet', 'No snippet available')
                }
                search_results.append(item) # Append to search_results


    except Exception as e:
        print(f"An error occurred during SerpAPI web search: {e}")
        return f"An error occurred during web search: {e}"

    print(f"web_search returning {len(search_results)} results.") # Debugging return count
    return search_results # Always return a list (empty or with results)


# --- Basic Agent Definition (Modified to remove LLM dependency for now) ---
print("Defining BasicAgent class...") # Debugging print
class BasicAgent:

    def __init__(self):
        print("BasicAgent initialized.") # Debugging print before init
        # Removed global gemini_model declaration as it's not used here
        # global gemini_model # Access global variable
        # if gemini_model is None:
        #      print("Warning: Google Generative AI model not successfully loaded before agent initialization.")
        # else:
        #     print("Google Generative AI model found and ready.") # Debugging print after successful init


    def process_video(self, video_source: str) -> str:
        """
        Processes a video source (file path or URL), extracts frames, and
        performs placeholder visual analysis.

        Args:
            video_source: Path to the video file or a video URL.

        Returns:
            A string summarizing the video processing result or an error message.
        """
        print(f"Processing video source: {video_source}")
        cap = None
        try:
            # Attempt to open the video source
            # Using cv2.CAP_FFMPEG might help with URLs, but requires FFmpeg
            # cap = cv2.VideoCapture(video_source, cv2.CAP_FFMPEG)
            cap = cv2.VideoCapture(video_source)


            # Check if the video was opened successfully
            if not cap.isOpened():
                print(f"Error: Could not open video source {video_source}")
                return f"Error: Could not open video source {video_source}"

            frame_count = 0
            while True:
                # Read a frame from the video
                ret, frame = cap.read()

                # If frame was not read successfully, we've reached the end of the video
                if not ret:
                    print("End of video stream.")
                    break

                frame_count += 1
                # --- Placeholder for visual analysis ---
                # In a real application, you would perform analysis on the 'frame' object here.
                # This could involve object detection, scene recognition, etc.
                # Example placeholder:
                # gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                # Perform analysis on gray_frame

                if frame_count % 100 == 0: # Print progress every 100 frames
                    print(f"Processed {frame_count} frames.")

            print(f"Finished processing video. Total frames extracted: {frame_count}")
            return f"Successfully processed video. Extracted {frame_count} frames."

        except Exception as e:
            print(f"An error occurred during video processing: {e}")
            return f"An error occurred during video processing: {e}"
        finally:
            # Release the video capture object
            if cap:
                cap.release()
                print("Video capture released.")

    def process_audio(self, audio_source: str) -> str:
        """
        Processes an audio source (file path), extracts speech, and performs
        placeholder audio analysis.

        Args:
            audio_source: Path to the audio file.

        Returns:
            A string summarizing the audio processing result or an error message.
        """
        print(f"Processing audio source: {audio_source}")
        recognizer = sr.Recognizer()
        try:
            # Load the audio file
            audio = AudioSegment.from_file(audio_source)
            print(f"Audio loaded. Duration: {len(audio)} ms")

            # Export to a format SpeechRecognition can handle (e.g., WAV)
            with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
                audio.export(fp.name, format="wav")
                temp_wav_file = fp.name
            print(f"Audio exported to temporary WAV: {temp_wav_file}")

            # Use SpeechRecognition to transcribe the audio
            with sr.AudioFile(temp_wav_file) as source:
                print("Reading audio file for transcription...")
                audio_data = recognizer.record(source) # read the entire audio file
                print("Audio data recorded.")

            # Attempt to recognize speech
            try:
                print("Attempting speech recognition...")
                text = recognizer.recognize_google(audio_data) # Using Google Web Speech API
                print(f"Transcription result: {text}")
                return f"Audio processed. Transcription: '{text}'"
            except sr.UnknownValueError:
                print("Speech Recognition could not understand audio")
                return "Audio processed, but could not understand speech."
            except sr.RequestError as e:
                print(f"Could not request results from Google Speech Recognition service; {e}")
                return f"Audio processed, but speech recognition service failed: {e}"
            except Exception as e:
                print(f"An unexpected error occurred during speech recognition: {e}")
                return f"An unexpected error occurred during speech recognition: {e}"

        except Exception as e:
            print(f"An error occurred during audio processing: {e}")
            return f"An error occurred during audio processing: {e}"
        finally:
            # Clean up the temporary WAV file
            if 'temp_wav_file' in locals() and os.path.exists(temp_wav_file):
                os.remove(temp_wav_file)
                print(f"Temporary WAV file removed: {temp_wav_file}")


    def __call__(self, question: str, video_source: str | None = None, audio_source: str | None = None) -> str:
        # Removed global gemini_model declaration as it's not used here
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        print(f"Video source provided: {video_source}")
        print(f"Audio source provided: {audio_source}")

        # --- Check for media processing tasks ---
        media_processing_results = []
        if video_source:
            print("Video source provided. Attempting video processing.")
            video_processing_result = self.process_video(video_source)
            media_processing_results.append(f"Video processing result: {video_processing_result}")

        if audio_source:
            print("Audio source provided. Attempting audio processing.")
            audio_processing_result = self.process_audio(audio_source)
            media_processing_results.append(f"Audio processing result: {audio_processing_result}")

        # If media was processed, return the results for now
        if media_processing_results:
            return "\n".join(media_processing_results)


        # Simple logic to determine if a web search is needed (only if no media source)
        question_lower = question.lower()
        search_keywords = ["what is", "how to", "where is", "who is", "when did", "define", "explain", "tell me about"]
        needs_search = any(keyword in question_lower for keyword in search_keywords) or "?" in question
        print(f"Needs search: {needs_search}") # Debugging search decision

        # --- Analyze question and refine search query ---
        # Simplified search query generation - removed LLM query generation
        search_query = question # Default search query is the original question
        if needs_search:
            print("Analyzing question for keywords and refining search query...")
            # Basic keyword extraction: split by common question words and take the rest
            parts = question_lower.split("what is", 1)
            if len(parts) > 1:
                search_query = parts[1].strip()
            else:
                parts = question_lower.split("how to", 1)
                if len(parts) > 1:
                    search_query = parts[1].strip()
                else:
                     parts = question_lower.split("where is", 1)
                     if len(parts) > 1:
                         search_query = parts[1].strip()
                     else:
                         parts = question_lower.split("who is", 1)
                         if len(parts) > 1:
                             search_query = parts[1].strip()
                         else:
                              parts = question_lower.split("when did", 1)
                              if len(parts) > 1:
                                  search_query = parts[1].strip()
                              else:
                                   parts = question_lower.split("define", 1)
                                   if len(parts) > 1:
                                       search_query = parts[1].strip()
                                   else:
                                        parts = question_lower.split("explain", 1)
                                        if len(parts) > 1:
                                            search_query = parts[1].strip()
                                        else:
                                             parts = question_lower.split("tell me about", 1)
                                             if len(parts) > 1:
                                                 search_query = parts[1].strip()
                                             else:
                                                 # If no specific question keyword found, use the whole question
                                                 search_query = question_lower.strip()


            # Optional: Add quotation marks for multi-word phrases if identified
            # This simple approach just uses the extracted part as is.
            # A more complex approach would identify multi-word entities (e.g., "New York City")
            # and wrap them in quotes.

            # Optional: Add contextual terms
            # Example: If "musician" or "band" is in the question, add "discography"
            if any(word in question_lower for word in ["musician", "band", "artist", "singer"]):
                 search_query += " discography"
            elif any(word in question_lower for word in ["movie", "film", "actor", "actress"]):
                 search_query += " plot summary"
            elif any(word in question_lower for word in ["book", "author", "novel"]):
                 search_query += " plot summary"


            print(f"Final search query used: {search_query}") # Debugging final query

            search_results = [] # Initialize search_results to an empty list before the try block
            if needs_search:
                print(f"Question likely requires search. Searching for: {search_query}")
                try:
                    params = { # Define params here, before calling GoogleSearch
                        "q": search_query,
                        "api_key": SERPAPI_API_KEY,
                        "engine": "google",  # Use Google search engine
                        "num": 5 # Number of results to fetch
                    }
                    search = GoogleSearch(params) # Use GoogleSearch from the correct package
                    search_results_dict = search.get_dict() # Get results as a dictionary
                    print(f"SerpAPI raw response keys: {search_results_dict.keys() if isinstance(search_results_dict, dict) else 'Response is not a dictionary'}") # Debugging response keys

                    # Log the full SerpAPI response for debugging if organic_results is missing or empty
                    if not isinstance(search_results_dict, dict) or "organic_results" not in search_results_dict or not isinstance(search_results_dict["organic_results"], list) or not search_results_dict["organic_results"]:
                         print(f"SerpAPI response did not contain organic results or had invalid format. Response: {search_results_dict}")
                         search_results = [] # Ensure search_results is empty if no organic results

                    # Extract organic results
                    # Add check that search_results_dict and organic_results are valid
                    if isinstance(search_results_dict, dict) and "organic_results" in search_results_dict and isinstance(search_results_dict["organic_results"], list):
                        print(f"Found {len(search_results_dict['organic_results'])} organic results.") # Debugging result count
                        for result in search_results_dict["organic_results"]:
                            # Add check for None or non-dict result item
                            if result is None or not isinstance(result, dict):
                                print(f"Skipping invalid search result item: {result}")
                                continue
                            item = {
                                'title': result.get('title'),
                                'url': result.get('link'),
                                'snippet': result.get('snippet', 'No snippet available')
                            }
                            search_results.append(item) # Append to search_results


                except Exception as e:
                    print(f"An error occurred during SerpAPI web search: {e}")
                    return f"An error occurred during web search: {e}"

                print(f"web_search returning {len(search_results)} results.") # Debugging return count

                # --- Use LLM to process search results if available (Removed LLM Synthesis) ---
                # Check that search_results is a list and is not empty
                if isinstance(search_results, list) and search_results and gemini_model is not None:
                    print("Using Google LLM to process search results.") # Debugging print before LLM call

                    # Format search results for the LLM
                    context = ""
                    for i, result in enumerate(search_results[:5]): # Use top 5 results for context
                        # Add check for None or non-dict result item before accessing keys
                        if result is None or not isinstance(result, dict):
                            print(f"Skipping invalid result at index {i} in LLM context formatting: {result}")
                            continue
                        context += f"Source {i+1}:\n"
                        if result.get('title'):
                             context += f"Title: {result['title']}\n"
                        if result.get('snippet'):
                             context += f"Snippet: {result['snippet']}\n"
                        if result.get('url'):
                             context += f"URL: {result['url']}\n"
                        context += "---\n" # Separator

                    # Refined prompt for the LLM
                    prompt = f"""Carefully read the following search results and answer the user's question based *only* on the information provided in these results.
If the search results do not contain sufficient information to fully answer the question, explicitly state that you could not find enough information in the provided results.
Do not use any outside knowledge. Structure your answer clearly and concisely.

Question: {question}

Search Results:
{context}

Answer:"""

                    print(f"LLM Prompt (first 500 chars):\n{prompt[:500]}...") # Debugging prompt

                    try:
                        # Generate content using the Gemini model
                        response = gemini_model.generate_content(prompt)
                        generated_text = response.text # Get the generated text

                        # Add check for empty or whitespace generated text
                        if generated_text and generated_text.strip():
                             llm_answer = generated_text.strip()
                             print(f"LLM generated text (first 100 chars): {generated_text[:100]}...") # Debugging raw output
                             print(f"Agent returning LLM-based answer (first 100 chars): {llm_answer[:100]}...") # Debugging final answer
                             return llm_answer
                        else:
                             print("LLM generated empty or whitespace answer.")
                             return "I couldn't generate a specific answer based on the search results."


                    except Exception as llm_e:
                         print(f"An error occurred during LLM generation: {llm_e}")
                         return f"An error occurred while processing search results with the LLM: {llm_e}"

                # Fallback if search results are empty or not a list, or LLM is None
                elif isinstance(search_results, list) and search_results: # Search results exist and is a list, but LLM is not available or failed
                     print("Google Generative AI model not loaded or search results empty or LLM failed. Cannot use LLM for synthesis.")
                     # Return the old style answer if LLM is not available, but only if search results exist
                     print("Returning basic answer based on search results (LLM not available).")
                     answer_parts = []
                     for i, result in enumerate(search_results[:3]):
                        # Add check for None or non-dict result item before accessing keys
                        if result is None or not isinstance(result, dict):
                            print(f"Skipping invalid result at index {i} in basic answer formatting: {result}")
                            continue
                        if result.get('snippet'):
                           # Limit snippet length to avoid overly long responses
                           snippet = result['snippet']
                           if len(snippet) > 200:
                               snippet = snippet[:200] + "..."
                           answer_parts.append(f"Snippet {i+1}: {snippet}")
                        elif result.get('title'):
                             answer_parts.append(f"Result {i+1} Title: {result['title']}")
                     if answer_parts:
                         return "Based on web search (LLM not available):\n" + "\n".join(answer_parts)
                     else:
                         # Fallback if no useful snippets/titles found in search results
                         print("No useful snippets/titles found in search results.")
                         return "I couldn't find useful information in the search results (LLM not available)."
                else: # search_results is None or not a list, or empty
                    print(f"Web search returned no results or results in invalid format. Type: {type(search_results)}")
                    return "I couldn't find any relevant information on the web for your question."

            else: # needs_search is True but no search results were returned (this case is now covered by the try-except around web_search)
                 # This else block should ideally not be reached if needs_search is True and web_search is called
                 print("Question required search, but no search was performed or it failed.")
                 return "I couldn't perform a web search for your question."


        else:
            # If no search is needed, return a default or simple response
            print("Question does not appear to require search. Returning fixed answer.")
            fixed_answer = "How can I help you?"
            return fixed_answer


def run_and_submit_all( profile: gr.OAuthProfile | None, other_arg=None): # Modified to accept 2 arguments
    """
    Fetches all questions, runs the BasicAgent on them, submits all answers,
    and displays the results.
    """
    print("run_and_submit_all function started.") # Debugging print at function start
    # --- Determine HF Space Runtime URL and Repo URL ---
    space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code

    if profile:
        username= f"{profile.username}"
        print(f"User logged in: {username}")
    else:
        print("User not logged in.")
        return "Please Login to Hugging Face with the button.", None

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

    # 1. Instantiate Agent ( modify this part to create your agent)
    print("Attempting to instantiate BasicAgent...") # Debugging print before instantiation
    try:
        agent = BasicAgent()
        print("BasicAgent instantiated successfully.") # Debugging print after instantiation
    except Exception as e:
        print(f"Error instantiating agent: {e}")
        return f"Error initializing agent: {e}", None
    # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
    print(agent_code)

    # 2. Fetch Questions
    print(f"Fetching questions from: {questions_url}")
    questions_data = None # Initialize to None
    try:
        response = requests.get(questions_url, timeout=15)
        response.raise_for_status()
        questions_data = response.json()
        # Add check for empty or non-list questions_data immediately after fetching
        if not isinstance(questions_data, list) or not questions_data:
             print(f"Fetched questions_data is empty or not a list. Type: {type(questions_data)}")
             return "Fetched questions list is empty or invalid format.", None
        print(f"Fetched {len(questions_data)} questions.")
    except requests.exceptions.RequestException as e:
        print(f"Error fetching questions: {e}")
        return f"Error fetching questions: {e}", None
    except requests.exceptions.JSONDecodeError as e:
         print(f"Error decoding JSON response from questions endpoint: {e}")
         # Print the response text for debugging if JSON decoding fails
         print(f"Response text: {response.text[:500] if 'response' in locals() else 'No response object'}")
         return f"Error decoding server response for questions: {e}", None
    except Exception as e:
        print(f"An unexpected error occurred fetching questions: {e}")
        return f"An unexpected error occurred fetching questions: {e}", None


    # 3. Run your Agent
    results_log = []
    answers_payload = []
    print(f"Running agent on {len(questions_data)} questions...")
    # The check that questions_data is a list is now done immediately after fetching
    for item in questions_data:
        # Add check for None or non-dict item before accessing keys
        if item is None or not isinstance(item, dict):
            print(f"Skipping invalid item in questions_data: {item}")
            continue
        task_id = item.get("task_id")
        question_text = item.get("question")
        if not task_id or not isinstance(task_id, (str, int)) or not question_text or not isinstance(question_text, str):
            print(f"Skipping item with missing or invalid task_id or question: {item}")
            continue
        print(f"Processing Task ID: {task_id}") # Debugging task ID
        try:
            # Here, we only pass the question text for now, as the API doesn't support video input
            # The video processing logic is added but not triggered by this function
            submitted_answer = agent(question_text)
            print(f"Agent returned answer for {task_id}: {submitted_answer[:50]}...") # Debugging returned answer
            answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
            results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
        except Exception as e:
             print(f"Error running agent on task {task_id}: {e}")
             results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})

    if not answers_payload:
        print("Agent did not produce any answers to submit.")
        return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)

    # 4. Prepare Submission
    submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
    status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
    print(status_update)

    # 5. 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.')}"
        )
        print("Submission successful.")
        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)}"
            status_message = f"Submission Failed: {error_detail}"
            print(status_message)
            # If submission fails, also return the results log so the user can see what was attempted
            results_df = pd.DataFrame(results_log)
            return status_message, results_df
        except requests.exceptions.JSONDecodeError:
            error_detail += f" Response: {e.response.text[:500]}"
            status_message = f"Submission Failed: {error_detail}"
            print(status_message)
            results_df = pd.DataFrame(results_log)
            return status_message, results_df
        status_message = f"Submission Failed: {error_detail}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except requests.exceptions.Timeout:
        status_message = "Submission Failed: The request timed out."
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except requests.exceptions.RequestException as e:
        status_message = f"Submission Failed: Network error - {e}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except Exception as e:
        status_message = f"An unexpected error occurred during submission: {e}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df


# Function to call process_video directly for testing
def test_video_processing(video_source: str) -> str:
    print(f"Testing video processing with source: {video_source}")
    try:
        agent = BasicAgent()
        return agent.process_video(video_source)
    except Exception as e:
        return f"Error during video processing test: {e}"

# Function to call process_audio directly for testing
def test_audio_processing(audio_source: str) -> str:
    print(f"Testing audio processing with source: {audio_source}")
    try:
        agent = BasicAgent()
        return agent.process_audio(audio_source)
    except Exception as e:
        return f"Error during audio processing test: {e}"


# Move Gradio interface definition and launch outside the function
print("Defining Gradio interface...") # Debugging print
with gr.Blocks(theme=gr.themes.Soft(), title="Basic Agent Evaluation Runner") as demo:
    gr.Markdown(
        """
        # Basic Agent Evaluation Runner
        This application fetches a set of questions from a scoring API,
        runs your custom agent against each question, and submits the answers for scoring.

        **Instructions:**
        1. Ensure your agent logic is defined in the `BasicAgent` class above.
        2. **Get a SerpAPI key and a Google AI API key and add them as environment variables in your runtime environment (e.g., as secrets in your Hugging Face Space settings).**
        3. Log in to Hugging Face using the button below.
        4. Click the "Run Evaluation & Submit All Answers" button to run on predefined questions.
        5. Use the "Test Video Processing" and "Test Audio Processing" sections to test media analysis.
        """
    )
    login_btn = gr.LoginButton()

    run_button = gr.Button("Run Evaluation & Submit All Answers")
    run_button.interactive = True # Re-enable the button

    status_output = gr.Textbox(label="Run Status", interactive=False, lines=5)
    results_output = gr.DataFrame(label="Evaluation Results")

    run_button.click(
        run_and_submit_all,
        inputs=[login_btn], # Pass the profile from the login button
        outputs=[status_output, results_output]
    )

    gr.Markdown("---") # Separator
    gr.Markdown("## Test Media Processing")

    video_test_input = gr.Video(label="Upload Video or Paste URL")
    video_test_button = gr.Button("Test Video Processing")
    video_test_output = gr.Textbox(label="Video Processing Result", interactive=False)

    video_test_button.click(
        test_video_processing,
        inputs=[video_test_input],
        outputs=[video_test_output]
    )

    audio_test_input = gr.Audio(label="Upload Audio or Paste URL")
    audio_test_button = gr.Button("Test Audio Processing")
    audio_test_output = gr.Textbox(label="Audio Processing Result", interactive=False)

    audio_test_button.click(
        test_audio_processing,
        inputs=[audio_test_input],
        outputs=[audio_test_output]
    )

print("Gradio interface defined.") # Debugging print

# Ensure the app launches when the script is run
print("Checking if script is run directly...") # Debugging print
if __name__ == "__main__":
    print("Launching Gradio demo...") # Debugging print
    demo.launch(server_name="0.0.0.0") # Ensure binding to all interfaces
print("Script finished.") # Debugging print