Spaces:
Build error
Build error
Create app.py
Browse files
app.py
CHANGED
|
@@ -11,11 +11,15 @@ from pydub import AudioSegment # Import pydub for audio manipulation
|
|
| 11 |
import tempfile # Import tempfile for temporary file handling
|
| 12 |
import numpy as np # Import numpy for image processing
|
| 13 |
|
|
|
|
| 14 |
|
| 15 |
# Import libraries for SerpAPI
|
| 16 |
-
|
|
|
|
| 17 |
import google.generativeai as genai # Keep the import as the user might add LLM functionality back later
|
| 18 |
|
|
|
|
|
|
|
| 19 |
# Removed the import of google.colab.userdata as it's not available outside Colab
|
| 20 |
# from google.colab import userdata # To access the API key from secrets
|
| 21 |
|
|
@@ -29,9 +33,13 @@ print(f"SERPAPI_API_KEY (first 5 chars): {SERPAPI_API_KEY[:5] if SERPAPI_API_KEY
|
|
| 29 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
| 30 |
print(f"GOOGLE_API_KEY (first 5 chars): {GOOGLE_API_KEY[:5] if GOOGLE_API_KEY else 'None'}...") # Debugging API key
|
| 31 |
|
|
|
|
|
|
|
| 32 |
# --- Define the default API URL ---
|
| 33 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # Updated API URL
|
| 34 |
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# --- Google Generative AI LLM Initialization ---
|
| 37 |
# Keep LLM initialization but handle potential errors and None state
|
|
@@ -58,8 +66,11 @@ else:
|
|
| 58 |
print(f"An error occurred during Google Generative AI initialization: {e}")
|
| 59 |
gemini_model = None # Ensure model is None if initialization fails
|
| 60 |
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# --- Web Search Function (using SerpAPI) ---
|
|
|
|
| 63 |
def web_search(query: str) -> list[dict]:
|
| 64 |
# Removed global gemini_model declaration as it's not used here
|
| 65 |
"""
|
|
@@ -87,14 +98,14 @@ def web_search(query: str) -> list[dict]:
|
|
| 87 |
results = []
|
| 88 |
|
| 89 |
try:
|
| 90 |
-
search = GoogleSearch(params)
|
| 91 |
search_results_dict = search.get_dict() # Get results as a dictionary
|
| 92 |
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
|
| 93 |
|
| 94 |
# Log the full SerpAPI response for debugging if organic_results is missing or empty
|
| 95 |
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"]:
|
| 96 |
print(f"SerpAPI response did not contain organic results or had invalid format. Response: {search_results_dict}")
|
| 97 |
-
|
| 98 |
|
| 99 |
# Extract organic results
|
| 100 |
# Add check that search_results_dict and organic_results are valid
|
|
@@ -110,21 +121,19 @@ def web_search(query: str) -> list[dict]:
|
|
| 110 |
'url': result.get('link'),
|
| 111 |
'snippet': result.get('snippet', 'No snippet available')
|
| 112 |
}
|
| 113 |
-
|
| 114 |
-
else:
|
| 115 |
-
print(f"No 'organic_results' key found or invalid format in SerpAPI response. Response type: {type(search_results_dict)}")
|
| 116 |
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
print(f"An error occurred during SerpAPI web search: {e}")
|
| 120 |
-
|
| 121 |
-
return []
|
| 122 |
|
| 123 |
-
print(f"web_search returning {len(
|
| 124 |
-
return
|
| 125 |
|
| 126 |
|
| 127 |
# --- Basic Agent Definition (Modified to remove LLM dependency for now) ---
|
|
|
|
| 128 |
class BasicAgent:
|
| 129 |
|
| 130 |
def __init__(self):
|
|
@@ -343,13 +352,44 @@ class BasicAgent:
|
|
| 343 |
if needs_search:
|
| 344 |
print(f"Question likely requires search. Searching for: {search_query}")
|
| 345 |
try:
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
except Exception as e:
|
| 350 |
-
print(f"An error occurred during web search: {e}")
|
| 351 |
return f"An error occurred during web search: {e}"
|
| 352 |
|
|
|
|
|
|
|
| 353 |
# --- Use LLM to process search results if available (Removed LLM Synthesis) ---
|
| 354 |
# Check that search_results is a list and is not empty
|
| 355 |
if isinstance(search_results, list) and search_results and gemini_model is not None:
|
|
@@ -613,6 +653,7 @@ def test_audio_processing(audio_source: str) -> str:
|
|
| 613 |
|
| 614 |
|
| 615 |
# Move Gradio interface definition and launch outside the function
|
|
|
|
| 616 |
with gr.Blocks(theme=gr.themes.Soft(), title="Basic Agent Evaluation Runner") as demo:
|
| 617 |
gr.Markdown(
|
| 618 |
"""
|
|
@@ -665,7 +706,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Basic Agent Evaluation Runner") as
|
|
| 665 |
outputs=[audio_test_output]
|
| 666 |
)
|
| 667 |
|
|
|
|
| 668 |
|
| 669 |
# Ensure the app launches when the script is run
|
|
|
|
| 670 |
if __name__ == "__main__":
|
| 671 |
-
|
|
|
|
|
|
|
|
|
| 11 |
import tempfile # Import tempfile for temporary file handling
|
| 12 |
import numpy as np # Import numpy for image processing
|
| 13 |
|
| 14 |
+
print("All libraries imported successfully.") # Debugging print
|
| 15 |
|
| 16 |
# Import libraries for SerpAPI
|
| 17 |
+
# Corrected import: Import GoogleSearch from google_search_results
|
| 18 |
+
from google_search_results import GoogleSearch
|
| 19 |
import google.generativeai as genai # Keep the import as the user might add LLM functionality back later
|
| 20 |
|
| 21 |
+
print("SerpAPI and GenAI libraries imported successfully.") # Debugging print
|
| 22 |
+
|
| 23 |
# Removed the import of google.colab.userdata as it's not available outside Colab
|
| 24 |
# from google.colab import userdata # To access the API key from secrets
|
| 25 |
|
|
|
|
| 33 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
| 34 |
print(f"GOOGLE_API_KEY (first 5 chars): {GOOGLE_API_KEY[:5] if GOOGLE_API_KEY else 'None'}...") # Debugging API key
|
| 35 |
|
| 36 |
+
print("API keys retrieved (or attempted).") # Debugging print
|
| 37 |
+
|
| 38 |
# --- Define the default API URL ---
|
| 39 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # Updated API URL
|
| 40 |
|
| 41 |
+
print(f"DEFAULT_API_URL set to: {DEFAULT_API_URL}") # Debugging print
|
| 42 |
+
|
| 43 |
|
| 44 |
# --- Google Generative AI LLM Initialization ---
|
| 45 |
# Keep LLM initialization but handle potential errors and None state
|
|
|
|
| 66 |
print(f"An error occurred during Google Generative AI initialization: {e}")
|
| 67 |
gemini_model = None # Ensure model is None if initialization fails
|
| 68 |
|
| 69 |
+
print("LLM initialization attempted.") # Debugging print
|
| 70 |
+
|
| 71 |
|
| 72 |
# --- Web Search Function (using SerpAPI) ---
|
| 73 |
+
print("Defining web_search function...") # Debugging print
|
| 74 |
def web_search(query: str) -> list[dict]:
|
| 75 |
# Removed global gemini_model declaration as it's not used here
|
| 76 |
"""
|
|
|
|
| 98 |
results = []
|
| 99 |
|
| 100 |
try:
|
| 101 |
+
search = GoogleSearch(params) # Use GoogleSearch from the correct package
|
| 102 |
search_results_dict = search.get_dict() # Get results as a dictionary
|
| 103 |
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
|
| 104 |
|
| 105 |
# Log the full SerpAPI response for debugging if organic_results is missing or empty
|
| 106 |
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"]:
|
| 107 |
print(f"SerpAPI response did not contain organic results or had invalid format. Response: {search_results_dict}")
|
| 108 |
+
search_results = [] # Ensure search_results is empty if no organic results
|
| 109 |
|
| 110 |
# Extract organic results
|
| 111 |
# Add check that search_results_dict and organic_results are valid
|
|
|
|
| 121 |
'url': result.get('link'),
|
| 122 |
'snippet': result.get('snippet', 'No snippet available')
|
| 123 |
}
|
| 124 |
+
search_results.append(item) # Append to search_results
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
print(f"An error occurred during SerpAPI web search: {e}")
|
| 129 |
+
return f"An error occurred during web search: {e}"
|
|
|
|
| 130 |
|
| 131 |
+
print(f"web_search returning {len(search_results)} results.") # Debugging return count
|
| 132 |
+
return search_results # Always return a list (empty or with results)
|
| 133 |
|
| 134 |
|
| 135 |
# --- Basic Agent Definition (Modified to remove LLM dependency for now) ---
|
| 136 |
+
print("Defining BasicAgent class...") # Debugging print
|
| 137 |
class BasicAgent:
|
| 138 |
|
| 139 |
def __init__(self):
|
|
|
|
| 352 |
if needs_search:
|
| 353 |
print(f"Question likely requires search. Searching for: {search_query}")
|
| 354 |
try:
|
| 355 |
+
params = { # Define params here, before calling GoogleSearch
|
| 356 |
+
"q": search_query,
|
| 357 |
+
"api_key": SERPAPI_API_KEY,
|
| 358 |
+
"engine": "google", # Use Google search engine
|
| 359 |
+
"num": 5 # Number of results to fetch
|
| 360 |
+
}
|
| 361 |
+
search = GoogleSearch(params) # Use GoogleSearch from the correct package
|
| 362 |
+
search_results_dict = search.get_dict() # Get results as a dictionary
|
| 363 |
+
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
|
| 364 |
+
|
| 365 |
+
# Log the full SerpAPI response for debugging if organic_results is missing or empty
|
| 366 |
+
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"]:
|
| 367 |
+
print(f"SerpAPI response did not contain organic results or had invalid format. Response: {search_results_dict}")
|
| 368 |
+
search_results = [] # Ensure search_results is empty if no organic results
|
| 369 |
+
|
| 370 |
+
# Extract organic results
|
| 371 |
+
# Add check that search_results_dict and organic_results are valid
|
| 372 |
+
if isinstance(search_results_dict, dict) and "organic_results" in search_results_dict and isinstance(search_results_dict["organic_results"], list):
|
| 373 |
+
print(f"Found {len(search_results_dict['organic_results'])} organic results.") # Debugging result count
|
| 374 |
+
for result in search_results_dict["organic_results"]:
|
| 375 |
+
# Add check for None or non-dict result item
|
| 376 |
+
if result is None or not isinstance(result, dict):
|
| 377 |
+
print(f"Skipping invalid search result item: {result}")
|
| 378 |
+
continue
|
| 379 |
+
item = {
|
| 380 |
+
'title': result.get('title'),
|
| 381 |
+
'url': result.get('link'),
|
| 382 |
+
'snippet': result.get('snippet', 'No snippet available')
|
| 383 |
+
}
|
| 384 |
+
search_results.append(item) # Append to search_results
|
| 385 |
+
|
| 386 |
+
|
| 387 |
except Exception as e:
|
| 388 |
+
print(f"An error occurred during SerpAPI web search: {e}")
|
| 389 |
return f"An error occurred during web search: {e}"
|
| 390 |
|
| 391 |
+
print(f"web_search returning {len(search_results)} results.") # Debugging return count
|
| 392 |
+
|
| 393 |
# --- Use LLM to process search results if available (Removed LLM Synthesis) ---
|
| 394 |
# Check that search_results is a list and is not empty
|
| 395 |
if isinstance(search_results, list) and search_results and gemini_model is not None:
|
|
|
|
| 653 |
|
| 654 |
|
| 655 |
# Move Gradio interface definition and launch outside the function
|
| 656 |
+
print("Defining Gradio interface...") # Debugging print
|
| 657 |
with gr.Blocks(theme=gr.themes.Soft(), title="Basic Agent Evaluation Runner") as demo:
|
| 658 |
gr.Markdown(
|
| 659 |
"""
|
|
|
|
| 706 |
outputs=[audio_test_output]
|
| 707 |
)
|
| 708 |
|
| 709 |
+
print("Gradio interface defined.") # Debugging print
|
| 710 |
|
| 711 |
# Ensure the app launches when the script is run
|
| 712 |
+
print("Checking if script is run directly...") # Debugging print
|
| 713 |
if __name__ == "__main__":
|
| 714 |
+
print("Launching Gradio demo...") # Debugging print
|
| 715 |
+
demo.launch(server_name="0.0.0.0") # Ensure binding to all interfaces
|
| 716 |
+
print("Script finished.") # Debugging print
|