Update app.py
Browse files
app.py
CHANGED
|
@@ -9,61 +9,27 @@ from langchain_core.prompts import PromptTemplate
|
|
| 9 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
from langchain_community.llms import HuggingFaceHub
|
| 11 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
|
|
| 12 |
|
| 13 |
-
HUGGINGFACEHUB_API_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
| 16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 17 |
# --- Basic Agent Definition ---
|
| 18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 19 |
-
# Prompt
|
| 20 |
-
template = '''Answer the following questions as best you can. You have access to the following tools:
|
| 21 |
-
|
| 22 |
-
{tools}
|
| 23 |
-
|
| 24 |
-
Use the following format:
|
| 25 |
-
|
| 26 |
-
Question: the input question you must answer
|
| 27 |
-
Thought: you should always think about what to do
|
| 28 |
-
Action: the action to take, should be one of [{tool_names}]
|
| 29 |
-
Action Input: the input to the action
|
| 30 |
-
Observation: the result of the action
|
| 31 |
-
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 32 |
-
Thought: I now know the final answer
|
| 33 |
-
Final Answer: the final answer to the original input question
|
| 34 |
-
|
| 35 |
-
Begin!
|
| 36 |
-
Question: {input}
|
| 37 |
-
Thought:{agent_scratchpad}'''
|
| 38 |
-
prompt = PromptTemplate.from_template(template)
|
| 39 |
-
|
| 40 |
-
# Tools
|
| 41 |
-
search_tool = DuckDuckGoSearchRun()
|
| 42 |
-
tools = [search_tool]
|
| 43 |
-
|
| 44 |
-
# LLM
|
| 45 |
-
llm = HuggingFaceEndpoint(
|
| 46 |
-
repo_id="deepseek-ai/DeepSeek-R1-0528",
|
| 47 |
-
task="text-generation",
|
| 48 |
-
provider="hyperbolic", # set your provider here
|
| 49 |
-
# provider="nebius",
|
| 50 |
-
# provider="together",
|
| 51 |
-
)
|
| 52 |
-
model = ChatHuggingFace(llm=llm)
|
| 53 |
|
| 54 |
class BasicAgent:
|
| 55 |
def __init__(self):
|
| 56 |
-
self.agent =
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
response =
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return response
|
| 65 |
|
| 66 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 67 |
"""
|
| 68 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 69 |
and displays the results.
|
|
@@ -84,7 +50,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 84 |
|
| 85 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 86 |
try:
|
| 87 |
-
agent = BasicAgent()
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Error instantiating agent: {e}")
|
| 90 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 9 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
from langchain_community.llms import HuggingFaceHub
|
| 11 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 12 |
+
from agent import alfred
|
| 13 |
|
| 14 |
+
#HUGGINGFACEHUB_API_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
# --- Basic Agent Definition ---
|
| 19 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
class BasicAgent:
|
| 22 |
def __init__(self):
|
| 23 |
+
self.agent = alfred
|
| 24 |
+
async def __call__(self, question: str) -> str:
|
| 25 |
+
state_dict = await alfred.ainvoke({"messages": [HumanMessage(content = question)]})
|
| 26 |
+
response = state_dict["messages"][-1]
|
| 27 |
+
try:
|
| 28 |
+
return response.content
|
| 29 |
+
except:
|
| 30 |
+
return "Sorry, I can't answer to this question."
|
|
|
|
| 31 |
|
| 32 |
+
async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 33 |
"""
|
| 34 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 35 |
and displays the results.
|
|
|
|
| 50 |
|
| 51 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 52 |
try:
|
| 53 |
+
agent = await BasicAgent()
|
| 54 |
except Exception as e:
|
| 55 |
print(f"Error instantiating agent: {e}")
|
| 56 |
return f"Error initializing agent: {e}", None
|