Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import requests
|
|
| 4 |
import pandas as pd
|
| 5 |
from typing import TypedDict, Annotated, Sequence
|
| 6 |
import operator
|
| 7 |
-
from langchain_core.messages import BaseMessage, HumanMessage
|
| 8 |
from langchain.agents import AgentExecutor
|
| 9 |
from langchain_experimental.tools import PythonREPLTool
|
| 10 |
from langchain_community.tools.youtube.search import YouTubeSearchTool
|
|
@@ -21,10 +21,10 @@ class AgentState(TypedDict):
|
|
| 21 |
|
| 22 |
# Agentin rakentajafunktio
|
| 23 |
def create_langgraph_agent():
|
| 24 |
-
print("Initializing Advanced LangGraph Agent
|
| 25 |
|
| 26 |
# 1. System prompt GAIA-tyyliin
|
| 27 |
-
|
| 28 |
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 29 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 30 |
|
|
@@ -33,28 +33,18 @@ If you are asked for a number, don't use comma to write your number neither use
|
|
| 33 |
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.
|
| 34 |
If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
|
| 35 |
"""
|
| 36 |
-
llm = ChatOpenAI(model="gpt-4o", temperature=0, system_message=system_prompt)
|
| 37 |
|
| 38 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
| 39 |
tools = [
|
| 40 |
TavilySearchResults(max_results=3),
|
| 41 |
PythonREPLTool(),
|
| 42 |
YouTubeSearchTool(),
|
| 43 |
]
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
try:
|
| 47 |
-
from langchain_community.tools.playwright.utils import create_sync_playwright_browser
|
| 48 |
-
from langchain_community.agent_toolkits.playwright.toolkit import PlayWrightBrowserToolkit
|
| 49 |
-
|
| 50 |
-
sync_browser = create_sync_playwright_browser()
|
| 51 |
-
browser_toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=sync_browser)
|
| 52 |
-
tools.extend(browser_toolkit.get_tools())
|
| 53 |
-
print("Playwright tools loaded.")
|
| 54 |
-
except Exception as e:
|
| 55 |
-
print("Playwright not available, skipping browser tools:", e)
|
| 56 |
-
|
| 57 |
-
# 4. Valinnainen FileManagement toolkit
|
| 58 |
try:
|
| 59 |
from langchain_community.agent_toolkits.file_management.toolkit import FileManagementToolkit
|
| 60 |
file_toolkit = FileManagementToolkit(root_dir=".")
|
|
@@ -63,20 +53,21 @@ If you are asked for a comma separated list, apply the above rules depending on
|
|
| 63 |
except Exception as e:
|
| 64 |
print("FileManagement toolkit unavailable:", e)
|
| 65 |
|
| 66 |
-
# Bind tools
|
| 67 |
llm_with_tools = llm.bind_tools(tools)
|
| 68 |
print("LLM and tools initialized.")
|
| 69 |
|
| 70 |
-
#
|
| 71 |
def agent_node(state):
|
| 72 |
-
print("Calling agent node
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
tool_node = ToolNode(tools)
|
| 77 |
-
print("Graph nodes defined.")
|
| 78 |
|
| 79 |
-
#
|
| 80 |
graph = StateGraph(AgentState)
|
| 81 |
graph.add_node("agent", agent_node)
|
| 82 |
graph.add_node("tools", tool_node)
|
|
@@ -84,8 +75,7 @@ If you are asked for a comma separated list, apply the above rules depending on
|
|
| 84 |
graph.add_conditional_edges("agent", tools_condition)
|
| 85 |
graph.add_edge("tools", "agent")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
app = graph.compile() # rekursion raja määritellään invoke-kutsussa
|
| 89 |
print("LangGraph agent compiled and ready.")
|
| 90 |
return app
|
| 91 |
|
|
@@ -110,15 +100,16 @@ def run_agent(agent_executor, question: str) -> str:
|
|
| 110 |
print(f"Agent returning answer: {final_answer}")
|
| 111 |
return str(final_answer)
|
| 112 |
|
| 113 |
-
# Evaluaation ajaminen
|
|
|
|
| 114 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 115 |
space_id = os.getenv("SPACE_ID")
|
| 116 |
if not profile:
|
| 117 |
-
return "Please
|
| 118 |
username = f"{profile.username}"
|
| 119 |
|
| 120 |
if not os.getenv("TAVILY_API_KEY") or not os.getenv("OPENAI_API_KEY"):
|
| 121 |
-
return "
|
| 122 |
|
| 123 |
try:
|
| 124 |
agent_executor = create_langgraph_agent()
|
|
@@ -126,7 +117,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 126 |
return f"Error initializing agent: {e}", None
|
| 127 |
|
| 128 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 129 |
-
questions_url =
|
|
|
|
| 130 |
try:
|
| 131 |
response = requests.get(questions_url, timeout=20)
|
| 132 |
response.raise_for_status()
|
|
@@ -141,8 +133,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 141 |
submitted_answer = run_agent(agent_executor, question_text)
|
| 142 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 143 |
|
| 144 |
-
submission_data = {
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
try:
|
| 147 |
response = requests.post(submit_url, json=submission_data, timeout=240)
|
| 148 |
response.raise_for_status()
|
|
@@ -152,7 +149,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 152 |
f"User: {result_data.get('username')}\n"
|
| 153 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 154 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 155 |
-
f"Message: {result_data.get('message', 'No message received.')
|
| 156 |
)
|
| 157 |
return final_status, pd.DataFrame(answers_payload)
|
| 158 |
except Exception as e:
|
|
@@ -160,12 +157,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 160 |
|
| 161 |
# Gradio-käyttöliittymä
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
-
gr.Markdown("# Agent Evaluation Runner (
|
| 164 |
gr.LoginButton()
|
| 165 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 166 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
|
| 168 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 169 |
|
| 170 |
if __name__ == "__main__":
|
| 171 |
-
demo.launch()
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from typing import TypedDict, Annotated, Sequence
|
| 6 |
import operator
|
| 7 |
+
from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage
|
| 8 |
from langchain.agents import AgentExecutor
|
| 9 |
from langchain_experimental.tools import PythonREPLTool
|
| 10 |
from langchain_community.tools.youtube.search import YouTubeSearchTool
|
|
|
|
| 21 |
|
| 22 |
# Agentin rakentajafunktio
|
| 23 |
def create_langgraph_agent():
|
| 24 |
+
print("Initializing Advanced LangGraph Agent…")
|
| 25 |
|
| 26 |
# 1. System prompt GAIA-tyyliin
|
| 27 |
+
SYSTEM_PROMPT = """
|
| 28 |
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 29 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 30 |
|
|
|
|
| 33 |
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.
|
| 34 |
If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
|
| 35 |
"""
|
|
|
|
| 36 |
|
| 37 |
+
# 2. LLM (ei system_message-parametria -> annetaan prompt SystemMessage-na)
|
| 38 |
+
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 39 |
+
|
| 40 |
+
# 3. Perustyökalut
|
| 41 |
tools = [
|
| 42 |
TavilySearchResults(max_results=3),
|
| 43 |
PythonREPLTool(),
|
| 44 |
YouTubeSearchTool(),
|
| 45 |
]
|
| 46 |
|
| 47 |
+
# 4. Valinnainen FileManagement toolkit (kevyt, yleensä saatavilla)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
from langchain_community.agent_toolkits.file_management.toolkit import FileManagementToolkit
|
| 50 |
file_toolkit = FileManagementToolkit(root_dir=".")
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
print("FileManagement toolkit unavailable:", e)
|
| 55 |
|
| 56 |
+
# 5. Bind tools
|
| 57 |
llm_with_tools = llm.bind_tools(tools)
|
| 58 |
print("LLM and tools initialized.")
|
| 59 |
|
| 60 |
+
# 6. Agent-solmu (lisää system prompt joka kierroksella)
|
| 61 |
def agent_node(state):
|
| 62 |
+
print("Calling agent node…")
|
| 63 |
+
full_msgs = [SystemMessage(content=SYSTEM_PROMPT)] + list(state["messages"])
|
| 64 |
+
reply = llm_with_tools.invoke(full_msgs)
|
| 65 |
+
return {"messages": [reply]}
|
| 66 |
|
| 67 |
+
# 7. Työkalusolmu
|
| 68 |
tool_node = ToolNode(tools)
|
|
|
|
| 69 |
|
| 70 |
+
# 8. Rakenna graafi
|
| 71 |
graph = StateGraph(AgentState)
|
| 72 |
graph.add_node("agent", agent_node)
|
| 73 |
graph.add_node("tools", tool_node)
|
|
|
|
| 75 |
graph.add_conditional_edges("agent", tools_condition)
|
| 76 |
graph.add_edge("tools", "agent")
|
| 77 |
|
| 78 |
+
app = graph.compile()
|
|
|
|
| 79 |
print("LangGraph agent compiled and ready.")
|
| 80 |
return app
|
| 81 |
|
|
|
|
| 100 |
print(f"Agent returning answer: {final_answer}")
|
| 101 |
return str(final_answer)
|
| 102 |
|
| 103 |
+
# Evaluaation ajaminen ja tulosten lähetys
|
| 104 |
+
|
| 105 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 106 |
space_id = os.getenv("SPACE_ID")
|
| 107 |
if not profile:
|
| 108 |
+
return "Please login to Hugging Face.", None
|
| 109 |
username = f"{profile.username}"
|
| 110 |
|
| 111 |
if not os.getenv("TAVILY_API_KEY") or not os.getenv("OPENAI_API_KEY"):
|
| 112 |
+
return "API keys missing (TAVILY_API_KEY, OPENAI_API_KEY)", None
|
| 113 |
|
| 114 |
try:
|
| 115 |
agent_executor = create_langgraph_agent()
|
|
|
|
| 117 |
return f"Error initializing agent: {e}", None
|
| 118 |
|
| 119 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 120 |
+
questions_url = "https://agents-course-unit4-scoring.hf.space/questions"
|
| 121 |
+
|
| 122 |
try:
|
| 123 |
response = requests.get(questions_url, timeout=20)
|
| 124 |
response.raise_for_status()
|
|
|
|
| 133 |
submitted_answer = run_agent(agent_executor, question_text)
|
| 134 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 135 |
|
| 136 |
+
submission_data = {
|
| 137 |
+
"username": username.strip(),
|
| 138 |
+
"agent_code": agent_code,
|
| 139 |
+
"answers": answers_payload,
|
| 140 |
+
}
|
| 141 |
+
submit_url = "https://agents-course-unit4-scoring.hf.space/submit"
|
| 142 |
+
|
| 143 |
try:
|
| 144 |
response = requests.post(submit_url, json=submission_data, timeout=240)
|
| 145 |
response.raise_for_status()
|
|
|
|
| 149 |
f"User: {result_data.get('username')}\n"
|
| 150 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 151 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 152 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 153 |
)
|
| 154 |
return final_status, pd.DataFrame(answers_payload)
|
| 155 |
except Exception as e:
|
|
|
|
| 157 |
|
| 158 |
# Gradio-käyttöliittymä
|
| 159 |
with gr.Blocks() as demo:
|
| 160 |
+
gr.Markdown("# Agent Evaluation Runner (GAIA Prompt)")
|
| 161 |
gr.LoginButton()
|
| 162 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 163 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 164 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 165 |
+
|
| 166 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 167 |
|
| 168 |
if __name__ == "__main__":
|
| 169 |
+
demo.launch()
|