Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,11 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
-
import inspect
|
5 |
import pandas as pd
|
6 |
-
from agno.agent import Agent
|
7 |
-
from agno.tools.duckduckgo import DuckDuckGoTools
|
8 |
-
from agno.models.nvidia import Nvidia
|
9 |
from dotenv import load_dotenv
|
|
|
10 |
|
|
|
11 |
load_dotenv()
|
12 |
|
13 |
# --- Constants ---
|
@@ -16,52 +14,55 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
16 |
# --- Basic Agent Definition ---
|
17 |
class BasicAgent:
|
18 |
def __init__(self):
|
19 |
-
self.
|
20 |
-
model=
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
def __call__(self, question: str) -> str:
|
|
|
48 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
49 |
-
|
50 |
-
|
51 |
-
)
|
52 |
-
|
53 |
-
return fixed_answer
|
54 |
|
55 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
56 |
"""
|
57 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
58 |
and displays the results.
|
59 |
"""
|
60 |
-
|
61 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
62 |
|
63 |
if profile:
|
64 |
-
username= f"{profile.username}"
|
65 |
print(f"User logged in: {username}")
|
66 |
else:
|
67 |
print("User not logged in.")
|
@@ -71,13 +72,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
71 |
questions_url = f"{api_url}/questions"
|
72 |
submit_url = f"{api_url}/submit"
|
73 |
|
74 |
-
# 1. Instantiate Agent
|
75 |
try:
|
76 |
agent = BasicAgent()
|
77 |
except Exception as e:
|
78 |
print(f"Error instantiating agent: {e}")
|
79 |
return f"Error initializing agent: {e}", None
|
80 |
-
|
81 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
82 |
print(agent_code)
|
83 |
|
@@ -88,16 +89,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
88 |
response.raise_for_status()
|
89 |
questions_data = response.json()
|
90 |
if not questions_data:
|
91 |
-
|
92 |
-
|
93 |
print(f"Fetched {len(questions_data)} questions.")
|
94 |
except requests.exceptions.RequestException as e:
|
95 |
print(f"Error fetching questions: {e}")
|
96 |
return f"Error fetching questions: {e}", None
|
97 |
except requests.exceptions.JSONDecodeError as e:
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
except Exception as e:
|
102 |
print(f"An unexpected error occurred fetching questions: {e}")
|
103 |
return f"An unexpected error occurred fetching questions: {e}", None
|
@@ -117,8 +118,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
117 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
118 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
119 |
except Exception as e:
|
120 |
-
|
121 |
-
|
122 |
|
123 |
if not answers_payload:
|
124 |
print("Agent did not produce any answers to submit.")
|
@@ -202,9 +203,8 @@ with gr.Blocks() as demo:
|
|
202 |
|
203 |
if __name__ == "__main__":
|
204 |
print("\n" + "-"*30 + " 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")
|
208 |
|
209 |
if space_host_startup:
|
210 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
@@ -212,7 +212,7 @@ if __name__ == "__main__":
|
|
212 |
else:
|
213 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
214 |
|
215 |
-
if space_id_startup:
|
216 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
217 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
218 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
4 |
import pandas as pd
|
|
|
|
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
|
8 |
+
# Load environment variables
|
9 |
load_dotenv()
|
10 |
|
11 |
# --- Constants ---
|
|
|
14 |
# --- Basic Agent Definition ---
|
15 |
class BasicAgent:
|
16 |
def __init__(self):
|
17 |
+
self.llm = ChatGoogleGenerativeAI(
|
18 |
+
model="gemini-2.0-flash-001",
|
19 |
+
temperature=0.1,
|
20 |
+
max_tokens=1024,
|
21 |
+
api_key=os.getenv("GOOGLE_API_KEY"),
|
22 |
+
streaming=False # Set to True if you want streaming output
|
23 |
+
)
|
24 |
+
self.instructions = '''
|
25 |
+
## 🚀 Gaia Taskmaster: The Ultimate Agent Efficiency Prompt! 🌍
|
26 |
+
|
27 |
+
You are a high-performance AI agent with a laser focus on completing Gaia tasks with maximum efficiency and precision. Think of yourself as a blend of a master strategist and a productivity guru—always optimizing, always delivering.
|
28 |
+
|
29 |
+
### Operational Guidelines for Every Gaia Task
|
30 |
+
|
31 |
+
- **Use the search tool and all available resources to gather the most current, accurate information.**
|
32 |
+
- **Present solutions with clarity, logical structure, and a results-driven mindset.**
|
33 |
+
- **Structure your responses in clear sections:**
|
34 |
+
- Task Overview
|
35 |
+
- Step-by-step Execution Plan
|
36 |
+
- Key Details, Data, or Code Snippets
|
37 |
+
- Impact Analysis or Next Steps
|
38 |
+
- **Keep responses concise but comprehensive (2-3 paragraphs or bullet points max).**
|
39 |
+
- **Apply best practices for UI stability and code formatting to ensure all outputs are organized, visible, and maintainable.**
|
40 |
+
- **End with a motivating sign-off or call to action, such as:**
|
41 |
+
- "Task completed—ready for the next challenge!"
|
42 |
+
- "Gaia task executed with precision. What’s next?"
|
43 |
+
- "Mission accomplished. Awaiting further instructions!"
|
44 |
+
|
45 |
+
_Remember: Always verify facts, optimize for efficiency, and maintain a focus on clear, actionable results!_
|
46 |
+
'''
|
47 |
+
print("BasicAgent (Gemini via LangChain) initialized.")
|
48 |
+
|
49 |
def __call__(self, question: str) -> str:
|
50 |
+
prompt = f"{self.instructions}\n\n{question}"
|
51 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
52 |
+
response = self.llm.invoke(prompt)
|
53 |
+
answer = response.content.strip() if hasattr(response, "content") else str(response)
|
54 |
+
print(f"Agent returning answer: {answer}")
|
55 |
+
return answer
|
|
|
56 |
|
57 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
58 |
"""
|
59 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
60 |
and displays the results.
|
61 |
"""
|
62 |
+
space_id = os.getenv("SPACE_ID") # For codebase link
|
|
|
63 |
|
64 |
if profile:
|
65 |
+
username = f"{profile.username}"
|
66 |
print(f"User logged in: {username}")
|
67 |
else:
|
68 |
print("User not logged in.")
|
|
|
72 |
questions_url = f"{api_url}/questions"
|
73 |
submit_url = f"{api_url}/submit"
|
74 |
|
75 |
+
# 1. Instantiate Agent
|
76 |
try:
|
77 |
agent = BasicAgent()
|
78 |
except Exception as e:
|
79 |
print(f"Error instantiating agent: {e}")
|
80 |
return f"Error initializing agent: {e}", None
|
81 |
+
|
82 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
83 |
print(agent_code)
|
84 |
|
|
|
89 |
response.raise_for_status()
|
90 |
questions_data = response.json()
|
91 |
if not questions_data:
|
92 |
+
print("Fetched questions list is empty.")
|
93 |
+
return "Fetched questions list is empty or invalid format.", None
|
94 |
print(f"Fetched {len(questions_data)} questions.")
|
95 |
except requests.exceptions.RequestException as e:
|
96 |
print(f"Error fetching questions: {e}")
|
97 |
return f"Error fetching questions: {e}", None
|
98 |
except requests.exceptions.JSONDecodeError as e:
|
99 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
100 |
+
print(f"Response text: {response.text[:500]}")
|
101 |
+
return f"Error decoding server response for questions: {e}", None
|
102 |
except Exception as e:
|
103 |
print(f"An unexpected error occurred fetching questions: {e}")
|
104 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
118 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
119 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
120 |
except Exception as e:
|
121 |
+
print(f"Error running agent on task {task_id}: {e}")
|
122 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
123 |
|
124 |
if not answers_payload:
|
125 |
print("Agent did not produce any answers to submit.")
|
|
|
203 |
|
204 |
if __name__ == "__main__":
|
205 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
206 |
space_host_startup = os.getenv("SPACE_HOST")
|
207 |
+
space_id_startup = os.getenv("SPACE_ID")
|
208 |
|
209 |
if space_host_startup:
|
210 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
212 |
else:
|
213 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
214 |
|
215 |
+
if space_id_startup:
|
216 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
217 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
218 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|