Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -19,6 +21,57 @@ class BasicAgent:
|
|
| 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 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -40,7 +93,8 @@ 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
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import DuckDuckGoSearchTool
|
| 7 |
+
from tools import hub_stats_tool
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
|
|
| 21 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 22 |
return fixed_answer
|
| 23 |
|
| 24 |
+
class NewAgent:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
print("NewAgent initialized.")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Initialize the web search tool
|
| 30 |
+
search_tool = DuckDuckGoSearchTool()
|
| 31 |
+
# Initialize the Hub stats tool
|
| 32 |
+
hub_stats_tool = HubStatsTool()
|
| 33 |
+
# Generate the chat interface, including the tools
|
| 34 |
+
llm = HuggingFaceEndpoint(
|
| 35 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 36 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 37 |
+
)
|
| 38 |
+
chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 39 |
+
tools = [search_tool,
|
| 40 |
+
# weather_info_tool,
|
| 41 |
+
hub_stats_tool]
|
| 42 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 43 |
+
# Generate the AgentState and Agent graph
|
| 44 |
+
class AgentState(TypedDict):
|
| 45 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 46 |
+
def assistant(state: AgentState):
|
| 47 |
+
return {
|
| 48 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 49 |
+
}
|
| 50 |
+
## The graph
|
| 51 |
+
builder = StateGraph(AgentState)
|
| 52 |
+
# Define nodes: these do the work
|
| 53 |
+
builder.add_node("assistant", assistant)
|
| 54 |
+
builder.add_node("tools", ToolNode(tools))
|
| 55 |
+
# Define edges: these determine how the control flow moves
|
| 56 |
+
builder.add_edge(START, "assistant")
|
| 57 |
+
builder.add_conditional_edges(
|
| 58 |
+
"assistant",
|
| 59 |
+
# If the latest message requires a tool, route to tools
|
| 60 |
+
# Otherwise, provide a direct response
|
| 61 |
+
tools_condition,
|
| 62 |
+
)
|
| 63 |
+
builder.add_edge("tools", "assistant")
|
| 64 |
+
alfred = builder.compile()
|
| 65 |
+
|
| 66 |
+
def __call__(self, question: str) -> str:
|
| 67 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 68 |
+
# fixed_answer = "This is a default answer."
|
| 69 |
+
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 70 |
+
# return fixed_answer
|
| 71 |
+
messages = [HumanMessage(content=question)]
|
| 72 |
+
response = alfred.invoke({"messages": messages})
|
| 73 |
+
|
| 74 |
+
|
| 75 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 76 |
"""
|
| 77 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 93 |
|
| 94 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 95 |
try:
|
| 96 |
+
# agent = BasicAgent()
|
| 97 |
+
agent = NewAgent()
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error instantiating agent: {e}")
|
| 100 |
return f"Error initializing agent: {e}", None
|