Spaces:
Build error
Build error
Create app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
|
| 20 |
print(f"GOOGLE_API_KEY (first 5 chars): {GOOGLE_API_KEY[:5] if GOOGLE_API_KEY else 'None'}...") # Debugging API key
|
| 21 |
|
| 22 |
# --- Define the default API URL ---
|
| 23 |
-
DEFAULT_API_URL = "https://
|
| 24 |
|
| 25 |
|
| 26 |
# --- Google Generative AI LLM Initialization ---
|
|
@@ -78,7 +78,6 @@ def web_search(query: str) -> list[dict]:
|
|
| 78 |
search = GoogleSearch(params)
|
| 79 |
search_results_dict = search.get_dict() # Get results as a dictionary
|
| 80 |
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
|
| 81 |
-
print(f"SerpAPI full response (first 500 chars): {str(search_results_dict)[:500]}...") # DEBUG: Print full SerpAPI response
|
| 82 |
|
| 83 |
# Extract organic results
|
| 84 |
# Add check that search_results_dict and organic_results are valid
|
|
@@ -98,7 +97,7 @@ def web_search(query: str) -> list[dict]:
|
|
| 98 |
else:
|
| 99 |
print(f"No 'organic_results' key found or invalid format in SerpAPI response. Response type: {type(search_results_dict)}")
|
| 100 |
# Print the whole response if no organic_results are found for debugging
|
| 101 |
-
print(f"SerpAPI response (no organic results
|
| 102 |
|
| 103 |
|
| 104 |
except Exception as e:
|
|
@@ -107,7 +106,6 @@ def web_search(query: str) -> list[dict]:
|
|
| 107 |
return []
|
| 108 |
|
| 109 |
print(f"web_search returning {len(results)} results.") # Debugging return count
|
| 110 |
-
print(f"web_search results content (first 2 items): {results[:2]}") # DEBUG: Print content of results
|
| 111 |
return results # Always return a list (empty or with results)
|
| 112 |
|
| 113 |
|
|
@@ -422,16 +420,11 @@ Answer:"""
|
|
| 422 |
# Return the old style answer if LLM is not available, but only if search results exist
|
| 423 |
print("Returning basic answer based on search results (LLM not available).")
|
| 424 |
answer_parts = []
|
| 425 |
-
# DEBUG: Check what's in search_results before formatting
|
| 426 |
-
print(f"DEBUG: search_results in fallback (first item): {search_results[0] if search_results else 'empty'}")
|
| 427 |
-
|
| 428 |
for i, result in enumerate(search_results[:3]):
|
| 429 |
# Add check for None or non-dict result item before accessing keys
|
| 430 |
if result is None or not isinstance(result, dict):
|
| 431 |
print(f"Skipping invalid result at index {i} in basic answer formatting: {result}")
|
| 432 |
continue
|
| 433 |
-
# The issue is likely here: if 'snippet' is missing, it might be using the question as a default
|
| 434 |
-
# Ensure we only use actual snippets or titles from the search results
|
| 435 |
if result.get('snippet'):
|
| 436 |
# Limit snippet length to avoid overly long responses
|
| 437 |
snippet = result['snippet']
|
|
@@ -440,7 +433,6 @@ Answer:"""
|
|
| 440 |
answer_parts.append(f"Snippet {i+1}: {snippet}")
|
| 441 |
elif result.get('title'):
|
| 442 |
answer_parts.append(f"Result {i+1} Title: {result['title']}")
|
| 443 |
-
# If neither snippet nor title, skip this result for the answer_parts
|
| 444 |
if answer_parts:
|
| 445 |
return "Based on web search (LLM not available):\n" + "\n".join(answer_parts)
|
| 446 |
else:
|
|
@@ -464,10 +456,6 @@ Answer:"""
|
|
| 464 |
return fixed_answer
|
| 465 |
|
| 466 |
|
| 467 |
-
# --- Gradio Interface Setup ---
|
| 468 |
-
# Define the Gradio interface
|
| 469 |
-
|
| 470 |
-
# MOVIDO PARA CIMA: A função run_and_submit_all precisa ser definida antes de ser usada no Gradio.
|
| 471 |
def run_and_submit_all( profile: gr.OAuthProfile | None, other_arg=None): # Modified to accept 2 arguments
|
| 472 |
"""
|
| 473 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -482,10 +470,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, other_arg=None): # Modi
|
|
| 482 |
print(f"User logged in: {username}")
|
| 483 |
else:
|
| 484 |
print("User not logged in.")
|
| 485 |
-
|
| 486 |
-
username = "colab_test_user" # <--- Adicione esta linha para simular um usuário no Colab
|
| 487 |
-
print(f"User not logged in, simulating for Colab: {username}")
|
| 488 |
-
# return "Please Login to Hugging Face with the button.", None # Comente esta linha para testar no Colab
|
| 489 |
|
| 490 |
api_url = DEFAULT_API_URL
|
| 491 |
questions_url = f"{api_url}/questions"
|
|
@@ -500,7 +485,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, other_arg=None): # Modi
|
|
| 500 |
print(f"Error instantiating agent: {e}")
|
| 501 |
return f"Error initializing agent: {e}", None
|
| 502 |
# 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)
|
| 503 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 504 |
print(agent_code)
|
| 505 |
|
| 506 |
# 2. Fetch Questions
|
|
@@ -610,34 +595,50 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, other_arg=None): # Modi
|
|
| 610 |
print(status_message)
|
| 611 |
results_df = pd.DataFrame(results_log)
|
| 612 |
return status_message, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
|
| 615 |
-
|
|
|
|
| 616 |
gr.Markdown(
|
| 617 |
"""
|
| 618 |
-
#
|
| 619 |
-
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
"""
|
| 622 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
submit_button = gr.Button("Executar Agente e Submeter Respostas")
|
| 630 |
-
with gr.Column():
|
| 631 |
-
status_output = gr.Textbox(label="Status da Execução", interactive=False)
|
| 632 |
-
results_dataframe = gr.Dataframe(label="Resultados Detalhados", interactive=False, headers=["Task ID", "Question", "Submitted Answer"])
|
| 633 |
-
|
| 634 |
-
# Conectar o botão à função run_and_submit_all
|
| 635 |
-
# O componente gr.Login() fornece o 'profile' automaticamente quando o usuário faz login
|
| 636 |
-
submit_button.click(
|
| 637 |
-
fn=run_and_submit_all,
|
| 638 |
-
inputs=[], # Removido gr.Login(every=None)
|
| 639 |
-
outputs=[status_output, results_dataframe]
|
| 640 |
)
|
|
|
|
|
|
|
|
|
|
| 641 |
|
| 642 |
-
#
|
| 643 |
-
|
|
|
|
|
|
| 20 |
print(f"GOOGLE_API_KEY (first 5 chars): {GOOGLE_API_KEY[:5] if GOOGLE_API_KEY else 'None'}...") # Debugging API key
|
| 21 |
|
| 22 |
# --- Define the default API URL ---
|
| 23 |
+
DEFAULT_API_URL = "https://agent-challenge.hf.space/agent_challenge" # Or the correct API URL if different
|
| 24 |
|
| 25 |
|
| 26 |
# --- Google Generative AI LLM Initialization ---
|
|
|
|
| 78 |
search = GoogleSearch(params)
|
| 79 |
search_results_dict = search.get_dict() # Get results as a dictionary
|
| 80 |
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
|
|
|
|
| 81 |
|
| 82 |
# Extract organic results
|
| 83 |
# Add check that search_results_dict and organic_results are valid
|
|
|
|
| 97 |
else:
|
| 98 |
print(f"No 'organic_results' key found or invalid format in SerpAPI response. Response type: {type(search_results_dict)}")
|
| 99 |
# Print the whole response if no organic_results are found for debugging
|
| 100 |
+
# print(f"SerpAPI response (no organic results): {search_results_dict}")
|
| 101 |
|
| 102 |
|
| 103 |
except Exception as e:
|
|
|
|
| 106 |
return []
|
| 107 |
|
| 108 |
print(f"web_search returning {len(results)} results.") # Debugging return count
|
|
|
|
| 109 |
return results # Always return a list (empty or with results)
|
| 110 |
|
| 111 |
|
|
|
|
| 420 |
# Return the old style answer if LLM is not available, but only if search results exist
|
| 421 |
print("Returning basic answer based on search results (LLM not available).")
|
| 422 |
answer_parts = []
|
|
|
|
|
|
|
|
|
|
| 423 |
for i, result in enumerate(search_results[:3]):
|
| 424 |
# Add check for None or non-dict result item before accessing keys
|
| 425 |
if result is None or not isinstance(result, dict):
|
| 426 |
print(f"Skipping invalid result at index {i} in basic answer formatting: {result}")
|
| 427 |
continue
|
|
|
|
|
|
|
| 428 |
if result.get('snippet'):
|
| 429 |
# Limit snippet length to avoid overly long responses
|
| 430 |
snippet = result['snippet']
|
|
|
|
| 433 |
answer_parts.append(f"Snippet {i+1}: {snippet}")
|
| 434 |
elif result.get('title'):
|
| 435 |
answer_parts.append(f"Result {i+1} Title: {result['title']}")
|
|
|
|
| 436 |
if answer_parts:
|
| 437 |
return "Based on web search (LLM not available):\n" + "\n".join(answer_parts)
|
| 438 |
else:
|
|
|
|
| 456 |
return fixed_answer
|
| 457 |
|
| 458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
def run_and_submit_all( profile: gr.OAuthProfile | None, other_arg=None): # Modified to accept 2 arguments
|
| 460 |
"""
|
| 461 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 470 |
print(f"User logged in: {username}")
|
| 471 |
else:
|
| 472 |
print("User not logged in.")
|
| 473 |
+
return "Please Login to Hugging Face with the button.", None
|
|
|
|
|
|
|
|
|
|
| 474 |
|
| 475 |
api_url = DEFAULT_API_URL
|
| 476 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 485 |
print(f"Error instantiating agent: {e}")
|
| 486 |
return f"Error initializing agent: {e}", None
|
| 487 |
# 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)
|
| 488 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 489 |
print(agent_code)
|
| 490 |
|
| 491 |
# 2. Fetch Questions
|
|
|
|
| 595 |
print(status_message)
|
| 596 |
results_df = pd.DataFrame(results_log)
|
| 597 |
return status_message, results_df
|
| 598 |
+
except Exception as e:
|
| 599 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 600 |
+
print(status_message)
|
| 601 |
+
results_df = pd.DataFrame(results_log)
|
| 602 |
+
return status_message, results_df
|
| 603 |
|
| 604 |
|
| 605 |
+
# Move Gradio interface definition and launch outside the function
|
| 606 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Basic Agent Evaluation Runner") as demo:
|
| 607 |
gr.Markdown(
|
| 608 |
"""
|
| 609 |
+
# Basic Agent Evaluation Runner
|
| 610 |
+
This application fetches a set of questions from a scoring API,
|
| 611 |
+
runs your custom agent against each question, and submits the answers for scoring.
|
| 612 |
+
|
| 613 |
+
**Instructions:**
|
| 614 |
+
1. Ensure your agent logic is defined in the `BasicAgent` class above.
|
| 615 |
+
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).**
|
| 616 |
+
3. Log in to Hugging Face using the button below.
|
| 617 |
+
4. Click the "Run Evaluation & Submit All Answers" button.
|
| 618 |
+
5. The application will fetch questions, run your agent, submit answers, and display the results below.
|
| 619 |
"""
|
| 620 |
)
|
| 621 |
+
login_btn = gr.LoginButton()
|
| 622 |
+
|
| 623 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 624 |
+
run_button.interactive = True # Re-enable the button
|
| 625 |
+
|
| 626 |
+
status_output = gr.Textbox(label="Run Status", interactive=False, lines=5)
|
| 627 |
+
results_output = gr.DataFrame(label="Evaluation Results")
|
| 628 |
+
|
| 629 |
+
# Add Gradio components for video input
|
| 630 |
+
video_input = gr.Video(label="Upload Video or Paste URL (Optional)")
|
| 631 |
|
| 632 |
+
run_button.click(
|
| 633 |
+
run_and_submit_all,
|
| 634 |
+
# Pass the video_input to the function
|
| 635 |
+
inputs=[login_btn], # Modified to exclude video_input for now as run_and_submit_all doesn't use it
|
| 636 |
+
outputs=[status_output, results_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
)
|
| 638 |
+
# Add a separate button or modify the existing one to handle video processing
|
| 639 |
+
# For this subtask, we are just adding the video processing capability to the agent,
|
| 640 |
+
# not fully integrating it into the Gradio submission flow yet.
|
| 641 |
|
| 642 |
+
# Ensure the app launches when the script is run
|
| 643 |
+
if __name__ == "__main__":
|
| 644 |
+
demo.launch(server_name="0.0.0.0") # Ensure binding to all interfaces
|