errchh
commited on
Commit
·
d0faccd
1
Parent(s):
4a21a58
fix import agent to app
Browse files
agent.py
CHANGED
|
@@ -135,16 +135,6 @@ with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
|
| 135 |
sys_msg = SystemMessage(content=system_prompt)
|
| 136 |
|
| 137 |
|
| 138 |
-
# generate the chat interface and tools
|
| 139 |
-
llm = HuggingFaceEndpoint(
|
| 140 |
-
repo_id = "microsoft/Phi-4-reasoning-plus",
|
| 141 |
-
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
chat = ChatHuggingFace(llm=llm, verbose=False)
|
| 146 |
-
|
| 147 |
-
|
| 148 |
tools = [
|
| 149 |
add,
|
| 150 |
subtract,
|
|
@@ -157,42 +147,51 @@ tools = [
|
|
| 157 |
]
|
| 158 |
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
class AgentState(TypedDict):
|
| 165 |
-
messages: Annotated[list[AnyMessage], add_messages]
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 170 |
-
}
|
| 171 |
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
| 175 |
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
# define nodes
|
| 178 |
-
builder.add_node("assistant", assistant)
|
| 179 |
-
builder.add_node("tools", ToolNode(tools))
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
builder.add_edge(START, "assistant")
|
| 184 |
-
builder.add_conditional_edges(
|
| 185 |
-
"assistant",
|
| 186 |
-
# If the latest message requires a tool, route to tools
|
| 187 |
-
# Otherwise, provide a direct response
|
| 188 |
-
tools_condition,
|
| 189 |
-
)
|
| 190 |
-
builder.add_edge("tools", "assistant")
|
| 191 |
-
graph = builder.compile()
|
| 192 |
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
| 195 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
|
|
|
| 196 |
messages = [HumanMessage(content=question)]
|
| 197 |
messages = graph.invoke({"messages": messages})
|
| 198 |
for m in messages["messages"]:
|
|
|
|
| 135 |
sys_msg = SystemMessage(content=system_prompt)
|
| 136 |
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
tools = [
|
| 139 |
add,
|
| 140 |
subtract,
|
|
|
|
| 147 |
]
|
| 148 |
|
| 149 |
|
| 150 |
+
# build graph function
|
| 151 |
+
def build_graph():
|
| 152 |
+
# llm
|
| 153 |
+
llm = HuggingFaceEndpoint(
|
| 154 |
+
repo_id = "microsoft/Phi-4-reasoning-plus",
|
| 155 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 156 |
+
)
|
| 157 |
|
| 158 |
+
chat = ChatHuggingFace(llm=llm, verbose=False)
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
# bind tools to llm
|
| 161 |
+
chat_with_tools = chat.bind_tools(tools)
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# generate AgentState and Agent graph
|
| 164 |
+
class AgentState(TypedDict):
|
| 165 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 166 |
|
| 167 |
+
def assistant(state: AgentState):
|
| 168 |
+
return {
|
| 169 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 170 |
+
}
|
| 171 |
|
| 172 |
+
# build graph
|
| 173 |
+
builder = StateGraph(AgentState)
|
| 174 |
|
| 175 |
+
# define nodes
|
| 176 |
+
builder.add_node("assistant", assistant)
|
| 177 |
+
builder.add_node("tools", ToolNode(tools))
|
| 178 |
|
| 179 |
+
# define edges
|
| 180 |
+
builder.add_edge(START, "assistant")
|
| 181 |
+
builder.add_conditional_edges(
|
| 182 |
+
"assistant",
|
| 183 |
+
# If the latest message requires a tool, route to tools
|
| 184 |
+
# Otherwise, provide a direct response
|
| 185 |
+
tools_condition,
|
| 186 |
+
)
|
| 187 |
+
builder.add_edge("tools", "assistant")
|
| 188 |
|
| 189 |
+
return builder.compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 194 |
+
graph = build_graph()
|
| 195 |
messages = [HumanMessage(content=question)]
|
| 196 |
messages = graph.invoke({"messages": messages})
|
| 197 |
for m in messages["messages"]:
|
app.py
CHANGED
|
@@ -4,6 +4,9 @@ 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"
|
|
@@ -45,7 +48,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 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/
|
| 49 |
print(agent_code)
|
| 50 |
|
| 51 |
# 2. Fetch Questions
|
|
@@ -91,7 +94,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 91 |
print("Agent did not produce any answers to submit.")
|
| 92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
|
| 94 |
-
# 4. Prepare Submission
|
| 95 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
print(status_update)
|
|
@@ -193,4 +196,4 @@ if __name__ == "__main__":
|
|
| 193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
+
from agent import build_graph
|
| 9 |
+
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 48 |
print(f"Error instantiating agent: {e}")
|
| 49 |
return f"Error initializing agent: {e}", None
|
| 50 |
# 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)
|
| 51 |
+
agent_code = f"https://huggingface.co/spaces/errchh/hfagentscourse-finalassignment/tree/main"
|
| 52 |
print(agent_code)
|
| 53 |
|
| 54 |
# 2. Fetch Questions
|
|
|
|
| 94 |
print("Agent did not produce any answers to submit.")
|
| 95 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 96 |
|
| 97 |
+
# 4. Prepare Submission
|
| 98 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 99 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 100 |
print(status_update)
|
|
|
|
| 196 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 197 |
|
| 198 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 199 |
+
demo.launch(debug=True, share=False)
|