susmitsil commited on
Commit
4db4c21
·
verified ·
1 Parent(s): af67476
Files changed (1) hide show
  1. app.py +16 -132
app.py CHANGED
@@ -3,148 +3,32 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- from main_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
- model_id="mistralai/Mistral-7B-Instruct-v0.1",
86
- api_key=hf_token,
87
- temperature=temperature,
88
- executor_type="local",
89
- verbose=verbose
90
- )
91
- else:
92
- self.gaia_agent = GAIAAgent(
93
- model_type="OpenAIServerModel",
94
- model_id="grok-3-latest",
95
- api_key=xai_key,
96
- api_base=os.environ.get("XAI_API_BASE", "https://api.x.ai/v1"),
97
- temperature=temperature,
98
- executor_type="local",
99
- verbose=verbose
100
- )
101
- except ImportError as ie:
102
- # Handle OpenAI module errors specifically
103
- if "openai" in str(ie).lower() and hf_token:
104
- print(f"OpenAI module error: {ie}. Falling back to HfApiModel.")
105
- self.gaia_agent = GAIAAgent(
106
- model_type="HfApiModel",
107
- # model_id="mistralai/Mistral-7B-Instruct-v0.2",
108
- model_id="mistralai/Mistral-7B-Instruct-v0.1",
109
- api_key=hf_token,
110
- temperature=temperature,
111
- executor_type="local",
112
- verbose=verbose
113
- )
114
- print(f"Using HfApiModel with model_id: mistralai/Mistral-7B-Instruct-v0.2 (fallback)")
115
- else:
116
- raise
117
- else:
118
- # No API keys available, log the error
119
- print("ERROR: No API keys found. Please set at least one of these environment variables:")
120
- print("- HUGGINGFACEHUB_API_TOKEN or HF_API_TOKEN")
121
- print("- OPENAI_API_KEY")
122
- print("- XAI_API_KEY")
123
- self.gaia_agent = None
124
- print("WARNING: No API keys found. Agent will not be able to answer questions.")
125
-
126
- except Exception as e:
127
- print(f"Error initializing GAIAAgent: {e}")
128
- self.gaia_agent = None
129
- print("WARNING: Failed to initialize agent. Falling back to basic responses.")
130
 
 
 
 
 
131
  def __call__(self, question: str) -> str:
132
  print(f"Agent received question (first 50 chars): {question[:50]}...")
133
-
134
- # Check if we have a functioning GAIA agent
135
- if self.gaia_agent:
136
- try:
137
- # Process the question using the GAIA agent
138
- answer = self.gaia_agent.answer_question(question)
139
- print(f"Agent generated answer: {answer[:50]}..." if len(answer) > 50 else f"Agent generated answer: {answer}")
140
- return answer
141
- except Exception as e:
142
- print(f"Error processing question: {e}")
143
- # Fall back to a simple response on error
144
- return "An error occurred while processing your question. Please check the agent logs for details."
145
- else:
146
- # We don't have a valid agent, provide a basic response
147
- return "The agent is not properly initialized. Please check your API keys and configuration."
148
 
149
  def run_and_submit_all( profile: gr.OAuthProfile | None):
150
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from gemini_agent import GeminiAgent
7
 
8
+ # Constants
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
 
 
11
  class BasicAgent:
12
  def __init__(self):
13
+ print("Initializing the BasicAgent")
14
 
15
+ # Load environment variables
16
+ load_dotenv()
17
 
18
+ # Get Gemini API key
19
+ api_key = os.getenv('GOOGLE_API_KEY')
20
+ if not api_key:
21
+ raise ValueError("GOOGLE_API_KEY environment variable not set.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # Initialize GeminiAgent
24
+ self.agent = GeminiAgent(api_key=api_key)
25
+ print("GeminiAgent initialized successfully")
26
+
27
  def __call__(self, question: str) -> str:
28
  print(f"Agent received question (first 50 chars): {question[:50]}...")
29
+ final_answer = self.agent.run(question)
30
+ print(f"Agent returning fixed answer: {final_answer}")
31
+ return final_answer
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def run_and_submit_all( profile: gr.OAuthProfile | None):
34
  """