Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import pandas as pd
|
|
| 6 |
from smolagents import DuckDuckGoSearchTool, HfApiModel, Tool, CodeAgent
|
| 7 |
from langchain_community.tools import WikipediaQueryRun
|
| 8 |
from langchain_community.utilities import WikipediaAPIWrapper
|
|
|
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
|
@@ -14,23 +15,25 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
-
def __init__(self):
|
| 18 |
-
search_tool = DuckDuckGoSearchTool()
|
| 19 |
-
wiki_tool = Tool.from_langchain(WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()))
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
self.agent = CodeAgent(
|
| 22 |
model=model,
|
| 23 |
-
tools=[search_tool, wiki_tool],
|
| 24 |
)
|
| 25 |
-
self.system_prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. The question: "
|
| 26 |
print("BasicAgent initialized.")
|
| 27 |
def __call__(self, question: str) -> str:
|
| 28 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 29 |
answer = self.agent.run(self.system_prompt + question)
|
| 30 |
print(f"Agent returning answer: {answer}")
|
| 31 |
return answer
|
| 32 |
|
| 33 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 34 |
"""
|
| 35 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 36 |
and displays the results.
|
|
@@ -51,7 +54,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 51 |
|
| 52 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 53 |
try:
|
| 54 |
-
agent = BasicAgent()
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error instantiating agent: {e}")
|
| 57 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 6 |
from smolagents import DuckDuckGoSearchTool, HfApiModel, Tool, CodeAgent
|
| 7 |
from langchain_community.tools import WikipediaQueryRun
|
| 8 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 9 |
+
from hf_hub import login
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
|
|
| 15 |
# --- Basic Agent Definition ---
|
| 16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 17 |
class BasicAgent:
|
| 18 |
+
def __init__(self, oauth_token: gr.OAuthToken | None):
|
| 19 |
+
self.search_tool = DuckDuckGoSearchTool()
|
| 20 |
+
self.wiki_tool = Tool.from_langchain(WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()))
|
| 21 |
+
self.system_prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. The question: "
|
| 22 |
+
model = HfApiModel(
|
| 23 |
+
token = oauth_token.token
|
| 24 |
+
)
|
| 25 |
self.agent = CodeAgent(
|
| 26 |
model=model,
|
| 27 |
+
tools=[self.search_tool, self.wiki_tool],
|
| 28 |
)
|
|
|
|
| 29 |
print("BasicAgent initialized.")
|
| 30 |
def __call__(self, question: str) -> str:
|
| 31 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 32 |
answer = self.agent.run(self.system_prompt + question)
|
| 33 |
print(f"Agent returning answer: {answer}")
|
| 34 |
return answer
|
| 35 |
|
| 36 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None):
|
| 37 |
"""
|
| 38 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 39 |
and displays the results.
|
|
|
|
| 54 |
|
| 55 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 56 |
try:
|
| 57 |
+
agent = BasicAgent(oauth_token)
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error instantiating agent: {e}")
|
| 60 |
return f"Error initializing agent: {e}", None
|