Aditya0619 commited on
Commit
19d65d2
·
verified ·
1 Parent(s): 335e3b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -26
app.py CHANGED
@@ -3,21 +3,46 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
- # --- Basic Agent Definition ---
12
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
14
- def __init__(self):
15
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -40,10 +65,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
- agent = BasicAgent()
44
  except Exception as e:
45
- print(f"Error instantiating agent: {e}")
46
- return f"Error initializing agent: {e}", None
47
  # 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)
48
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
  print(agent_code)
@@ -142,29 +167,33 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
142
 
143
  # --- Build Gradio Interface using Blocks ---
144
  with gr.Blocks() as demo:
145
- gr.Markdown("# Basic Agent Evaluation Runner")
146
  gr.Markdown(
147
  """
 
 
 
 
 
 
148
  **Instructions:**
149
-
150
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
151
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
152
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
153
-
154
  ---
155
- **Disclaimers:**
156
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
157
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
158
  """
159
  )
160
 
161
  gr.LoginButton()
162
 
163
- run_button = gr.Button("Run Evaluation & Submit All Answers")
164
 
165
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
166
  # Removed max_rows=10 from DataFrame constructor
167
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
168
 
169
  run_button.click(
170
  fn=run_and_submit_all,
@@ -172,7 +201,7 @@ with gr.Blocks() as demo:
172
  )
173
 
174
  if __name__ == "__main__":
175
- print("\n" + "-"*30 + " App Starting " + "-"*30)
176
  # Check for SPACE_HOST and SPACE_ID at startup for information
177
  space_host_startup = os.getenv("SPACE_HOST")
178
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
@@ -190,7 +219,7 @@ if __name__ == "__main__":
190
  else:
191
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
192
 
193
- print("-"*(60 + len(" App Starting ")) + "\n")
194
 
195
- print("Launching Gradio Interface for Basic Agent Evaluation...")
196
  demo.launch(debug=True, share=False)
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from crewai_agent import CrewAIAgent
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
+ # --- CrewAI Agent Definition ---
13
+ # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
14
+ class CrewAIBasicAgent:
15
+ """Wrapper class for CrewAI agent to match the expected interface."""
16
+
17
+ def __init__(self, provider: str = "google"):
18
+ print(f"CrewAI Agent initialized with provider: {provider}")
19
+ try:
20
+ self.agent = CrewAIAgent(provider=provider)
21
+ print("CrewAI Agent successfully initialized.")
22
+ except Exception as e:
23
+ print(f"Error initializing CrewAI Agent: {e}")
24
+ # Fallback to simple agent
25
+ self.agent = None
26
+
27
  def __call__(self, question: str) -> str:
28
+ """Process question using CrewAI agent."""
29
+ print(f"CrewAI Agent received question (first 50 chars): {question[:50]}...")
30
+
31
+ if self.agent is None:
32
+ # Fallback response if agent failed to initialize
33
+ fallback_answer = "CrewAI agent initialization failed. Using fallback response."
34
+ print(f"Using fallback answer: {fallback_answer}")
35
+ return fallback_answer
36
+
37
+ try:
38
+ # Use the CrewAI agent to process the question
39
+ answer = self.agent(question)
40
+ print(f"CrewAI Agent generated answer (first 100 chars): {answer[:100]}...")
41
+ return answer
42
+ except Exception as e:
43
+ error_msg = f"Error in CrewAI agent processing: {str(e)}"
44
+ print(error_msg)
45
+ return error_msg
46
 
47
  def run_and_submit_all( profile: gr.OAuthProfile | None):
48
  """
 
65
 
66
  # 1. Instantiate Agent ( modify this part to create your agent)
67
  try:
68
+ agent = CrewAIBasicAgent(provider="google") # Using CrewAI agent instead of BasicAgent
69
  except Exception as e:
70
+ print(f"Error instantiating CrewAI agent: {e}")
71
+ return f"Error initializing CrewAI agent: {e}", None
72
  # 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)
73
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
74
  print(agent_code)
 
167
 
168
  # --- Build Gradio Interface using Blocks ---
169
  with gr.Blocks() as demo:
170
+ gr.Markdown("# CrewAI Multi-Purpose Agent Evaluation Runner")
171
  gr.Markdown(
172
  """
173
+ **CrewAI Agent Features:**
174
+ - **Multi-Agent Architecture**: Specialized agents for different types of tasks
175
+ - **Research Capabilities**: Search Wikipedia, ArXiv papers, and web sources
176
+ - **Mathematical Operations**: Perform calculations and solve mathematical problems
177
+ - **Intelligent Task Routing**: Automatically selects the best agent for each question
178
+
179
  **Instructions:**
180
+ 1. Please clone this space, then modify the code to define your CrewAI agent's logic, tools, and packages
181
+ 2. Add your API keys to the `.env` file (Google, Tavily, etc.)
182
+ 3. Log in to your Hugging Face account using the button below
183
+ 4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your CrewAI agent, and see the score
 
184
  ---
185
+ **Note:** The CrewAI agent uses multiple specialized agents working together to provide comprehensive answers.
186
+ This may take longer than a basic agent but provides much more intelligent responses.
 
187
  """
188
  )
189
 
190
  gr.LoginButton()
191
 
192
+ run_button = gr.Button("Run CrewAI Evaluation & Submit All Answers")
193
 
194
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
195
  # Removed max_rows=10 from DataFrame constructor
196
+ results_table = gr.DataFrame(label="Questions and CrewAI Agent Answers", wrap=True)
197
 
198
  run_button.click(
199
  fn=run_and_submit_all,
 
201
  )
202
 
203
  if __name__ == "__main__":
204
+ print("\n" + "-"*30 + " CrewAI App Starting " + "-"*30)
205
  # Check for SPACE_HOST and SPACE_ID at startup for information
206
  space_host_startup = os.getenv("SPACE_HOST")
207
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
 
219
  else:
220
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
221
 
222
+ print("-"*(60 + len(" CrewAI App Starting ")) + "\n")
223
 
224
+ print("Launching Gradio Interface for CrewAI Multi-Purpose Agent...")
225
  demo.launch(debug=True, share=False)