Spaces:
Sleeping
Sleeping
updated
Browse files
app.py
CHANGED
@@ -3,35 +3,146 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# (Keep Constants as is)
|
11 |
# --- Constants ---
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
-
|
15 |
# --- Basic Agent Definition ---
|
16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
17 |
class BasicAgent:
|
18 |
def __init__(self):
|
19 |
-
print("
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
# Initialize GeminiAgent
|
27 |
-
self.agent = GeminiAgent(api_key=api_key)
|
28 |
-
print("GeminiAgent initialized successfully")
|
29 |
-
|
30 |
def __call__(self, question: str) -> str:
|
31 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
37 |
"""
|
@@ -55,6 +166,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
55 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
56 |
try:
|
57 |
agent = BasicAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
except Exception as e:
|
59 |
print(f"Error instantiating agent: {e}")
|
60 |
return f"Error initializing agent: {e}", None
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from core_agent import GAIAAgent
|
7 |
|
8 |
+
# Debug function to show available environment variables
|
9 |
+
def debug_environment():
|
10 |
+
"""Print available environment variables related to API keys (with values hidden)"""
|
11 |
+
debug_vars = [
|
12 |
+
"HF_API_TOKEN", "HUGGINGFACEHUB_API_TOKEN",
|
13 |
+
"OPENAI_API_KEY", "XAI_API_KEY",
|
14 |
+
"AGENT_MODEL_TYPE", "AGENT_MODEL_ID",
|
15 |
+
"AGENT_TEMPERATURE", "AGENT_VERBOSE"
|
16 |
+
]
|
17 |
+
|
18 |
+
print("=== DEBUG: Environment Variables ===")
|
19 |
+
for var in debug_vars:
|
20 |
+
if os.environ.get(var):
|
21 |
+
# Hide actual values for security
|
22 |
+
print(f"{var}: [SET]")
|
23 |
+
else:
|
24 |
+
print(f"{var}: [NOT SET]")
|
25 |
+
print("===================================")
|
26 |
|
27 |
# (Keep Constants as is)
|
28 |
# --- Constants ---
|
29 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
30 |
|
|
|
31 |
# --- Basic Agent Definition ---
|
32 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
33 |
class BasicAgent:
|
34 |
def __init__(self):
|
35 |
+
print("BasicAgent initialized.")
|
36 |
+
|
37 |
+
# Call debug function to show available environment variables
|
38 |
+
debug_environment()
|
39 |
+
|
40 |
+
# Initialize the GAIAAgent with local execution
|
41 |
+
try:
|
42 |
+
# Load environment variables if dotenv is available
|
43 |
+
try:
|
44 |
+
import dotenv
|
45 |
+
dotenv.load_dotenv()
|
46 |
+
print("Loaded environment variables from .env file")
|
47 |
+
except ImportError:
|
48 |
+
print("python-dotenv not installed, continuing with environment as is")
|
49 |
+
|
50 |
+
# Try to load API keys from environment
|
51 |
+
# Check both HF_API_TOKEN and HUGGINGFACEHUB_API_TOKEN (HF Spaces uses HF_API_TOKEN)
|
52 |
+
hf_token = os.environ.get("HF_API_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
53 |
+
openai_key = os.environ.get("OPENAI_API_KEY")
|
54 |
+
xai_key = os.environ.get("XAI_API_KEY")
|
55 |
+
|
56 |
+
# If we have at least one API key, use a model-based approach
|
57 |
+
if hf_token:
|
58 |
+
# Default model parameters - read directly from environment
|
59 |
+
model_type = os.environ.get("AGENT_MODEL_TYPE", "OpenAIServerModel")
|
60 |
+
model_id = os.environ.get("AGENT_MODEL_ID", "gpt-4o")
|
61 |
+
temperature = float(os.environ.get("AGENT_TEMPERATURE", "0.2"))
|
62 |
+
verbose = os.environ.get("AGENT_VERBOSE", "false").lower() == "true"
|
63 |
+
|
64 |
+
print(f"Agent config - Model Type: {model_type}, Model ID: {model_id}")
|
65 |
+
|
66 |
+
try:
|
67 |
+
if model_type == "HfApiModel" and hf_token:
|
68 |
+
# Use Hugging Face API
|
69 |
+
self.gaia_agent = GAIAAgent(
|
70 |
+
model_type="HfApiModel",
|
71 |
+
model_id=model_id,
|
72 |
+
api_key=hf_token,
|
73 |
+
temperature=temperature,
|
74 |
+
executor_type="local",
|
75 |
+
verbose=verbose
|
76 |
+
)
|
77 |
+
print(f"Using HfApiModel with model_id: {model_id}")
|
78 |
+
else:
|
79 |
+
# Fallback to using whatever token we have
|
80 |
+
print("WARNING: Using fallback initialization with available token")
|
81 |
+
if hf_token:
|
82 |
+
self.gaia_agent = GAIAAgent(
|
83 |
+
model_type="HfApiModel",
|
84 |
+
model_id="mistralai/Mistral-7B-Instruct-v0.2",
|
85 |
+
api_key=hf_token,
|
86 |
+
temperature=temperature,
|
87 |
+
executor_type="local",
|
88 |
+
verbose=verbose
|
89 |
+
)
|
90 |
+
else:
|
91 |
+
self.gaia_agent = GAIAAgent(
|
92 |
+
model_type="OpenAIServerModel",
|
93 |
+
model_id="grok-3-latest",
|
94 |
+
api_key=xai_key,
|
95 |
+
api_base=os.environ.get("XAI_API_BASE", "https://api.x.ai/v1"),
|
96 |
+
temperature=temperature,
|
97 |
+
executor_type="local",
|
98 |
+
verbose=verbose
|
99 |
+
)
|
100 |
+
except ImportError as ie:
|
101 |
+
# Handle OpenAI module errors specifically
|
102 |
+
if "openai" in str(ie).lower() and hf_token:
|
103 |
+
print(f"OpenAI module error: {ie}. Falling back to HfApiModel.")
|
104 |
+
self.gaia_agent = GAIAAgent(
|
105 |
+
model_type="HfApiModel",
|
106 |
+
model_id="mistralai/Mistral-7B-Instruct-v0.2",
|
107 |
+
api_key=hf_token,
|
108 |
+
temperature=temperature,
|
109 |
+
executor_type="local",
|
110 |
+
verbose=verbose
|
111 |
+
)
|
112 |
+
print(f"Using HfApiModel with model_id: mistralai/Mistral-7B-Instruct-v0.2 (fallback)")
|
113 |
+
else:
|
114 |
+
raise
|
115 |
+
else:
|
116 |
+
# No API keys available, log the error
|
117 |
+
print("ERROR: No API keys found. Please set at least one of these environment variables:")
|
118 |
+
print("- HUGGINGFACEHUB_API_TOKEN or HF_API_TOKEN")
|
119 |
+
print("- OPENAI_API_KEY")
|
120 |
+
print("- XAI_API_KEY")
|
121 |
+
self.gaia_agent = None
|
122 |
+
print("WARNING: No API keys found. Agent will not be able to answer questions.")
|
123 |
+
|
124 |
+
except Exception as e:
|
125 |
+
print(f"Error initializing GAIAAgent: {e}")
|
126 |
+
self.gaia_agent = None
|
127 |
+
print("WARNING: Failed to initialize agent. Falling back to basic responses.")
|
128 |
|
|
|
|
|
|
|
|
|
129 |
def __call__(self, question: str) -> str:
|
130 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
131 |
+
|
132 |
+
# Check if we have a functioning GAIA agent
|
133 |
+
if self.gaia_agent:
|
134 |
+
try:
|
135 |
+
# Process the question using the GAIA agent
|
136 |
+
answer = self.gaia_agent.answer_question(question)
|
137 |
+
print(f"Agent generated answer: {answer[:50]}..." if len(answer) > 50 else f"Agent generated answer: {answer}")
|
138 |
+
return answer
|
139 |
+
except Exception as e:
|
140 |
+
print(f"Error processing question: {e}")
|
141 |
+
# Fall back to a simple response on error
|
142 |
+
return "An error occurred while processing your question. Please check the agent logs for details."
|
143 |
+
else:
|
144 |
+
# We don't have a valid agent, provide a basic response
|
145 |
+
return "The agent is not properly initialized. Please check your API keys and configuration."
|
146 |
|
147 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
148 |
"""
|
|
|
166 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
167 |
try:
|
168 |
agent = BasicAgent()
|
169 |
+
|
170 |
+
# Check if agent is properly initialized
|
171 |
+
if not agent.gaia_agent:
|
172 |
+
print("ERROR: Agent was not properly initialized")
|
173 |
+
return "ERROR: Agent was not properly initialized. Check the logs for details on missing API keys or configuration.", None
|
174 |
+
|
175 |
except Exception as e:
|
176 |
print(f"Error instantiating agent: {e}")
|
177 |
return f"Error initializing agent: {e}", None
|