Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,118 +2,94 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
|
|
|
|
|
| 5 |
from langchain import hub
|
| 6 |
-
from langchain.agents import AgentExecutor, create_react_agent
|
| 7 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 8 |
from langchain_huggingface import HuggingFaceEndpoint
|
|
|
|
| 9 |
|
| 10 |
-
# ---
|
| 11 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
class
|
| 15 |
-
|
| 16 |
-
print("Initializing LangChainAgent...")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
self.prompt = hub.pull("hwchase17/react")
|
| 32 |
-
print("Prompt template pulled.")
|
| 33 |
-
|
| 34 |
-
# 4. Create the Agent
|
| 35 |
-
self.agent = create_react_agent(self.llm, self.tools, self.prompt)
|
| 36 |
-
print("Agent created.")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
return str(final_answer)
|
| 64 |
|
| 65 |
-
# --- Main Application Logic (Uses our new agent) ---
|
| 66 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 67 |
"""
|
| 68 |
-
Fetches all questions, runs the
|
| 69 |
and displays the results.
|
| 70 |
"""
|
| 71 |
space_id = os.getenv("SPACE_ID")
|
| 72 |
-
|
| 73 |
-
if profile:
|
| 74 |
-
username = f"{profile.username}"
|
| 75 |
-
print(f"User logged in: {username}")
|
| 76 |
-
else:
|
| 77 |
-
print("User not logged in.")
|
| 78 |
return "Please Login to Hugging Face with the button.", None
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
questions_url = f"{api_url}/questions"
|
| 82 |
-
submit_url = f"{api_url}/submit"
|
| 83 |
-
|
| 84 |
-
# 1. Instantiate Agent
|
| 85 |
try:
|
| 86 |
-
|
| 87 |
except Exception as e:
|
| 88 |
-
print(f"Error instantiating agent: {e}")
|
| 89 |
return f"Error initializing agent: {e}", None
|
| 90 |
|
| 91 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 92 |
-
|
| 93 |
-
|
| 94 |
try:
|
| 95 |
response = requests.get(questions_url, timeout=20)
|
| 96 |
response.raise_for_status()
|
| 97 |
questions_data = response.json()
|
| 98 |
except Exception as e:
|
| 99 |
-
return f"
|
| 100 |
|
| 101 |
-
# 3. Run your Agent
|
| 102 |
-
results_log = []
|
| 103 |
answers_payload = []
|
| 104 |
for item in questions_data:
|
| 105 |
-
task_id = item.get("task_id")
|
| 106 |
-
|
| 107 |
-
submitted_answer = agent(question_text)
|
| 108 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 109 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 110 |
|
| 111 |
-
# 4. Prepare Submission
|
| 112 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 113 |
-
|
| 114 |
-
# 5. Submit
|
| 115 |
try:
|
| 116 |
-
response = requests.post(submit_url, json=submission_data, timeout=
|
| 117 |
response.raise_for_status()
|
| 118 |
result_data = response.json()
|
| 119 |
final_status = (
|
|
@@ -123,32 +99,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 123 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 124 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 125 |
)
|
| 126 |
-
|
| 127 |
-
return final_status, results_df
|
| 128 |
except Exception as e:
|
| 129 |
-
|
| 130 |
-
results_df = pd.DataFrame(results_log)
|
| 131 |
-
return status_message, results_df
|
| 132 |
|
| 133 |
# --- Gradio Interface (Unchanged) ---
|
| 134 |
with gr.Blocks() as demo:
|
| 135 |
-
gr.Markdown("# Agent Evaluation Runner (
|
| 136 |
-
gr.Markdown(
|
| 137 |
-
"""
|
| 138 |
-
**Instructions:**
|
| 139 |
-
1. Log in to your Hugging Face account using the button below.
|
| 140 |
-
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 141 |
-
"""
|
| 142 |
-
)
|
| 143 |
gr.LoginButton()
|
| 144 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 145 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 146 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 147 |
-
|
| 148 |
-
run_button.click(
|
| 149 |
-
fn=run_and_submit_all,
|
| 150 |
-
outputs=[status_output, results_table]
|
| 151 |
-
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
from typing import TypedDict, Annotated, Sequence
|
| 6 |
+
from langchain_core.messages import BaseMessage
|
| 7 |
from langchain import hub
|
|
|
|
| 8 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 9 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 10 |
+
from langgraph.prebuilt import create_agent_executor
|
| 11 |
|
| 12 |
+
# --- Main Application Logic ---
|
|
|
|
| 13 |
|
| 14 |
+
# This is our agent's "memory" or state. It keeps track of the conversation.
|
| 15 |
+
class AgentState(TypedDict):
|
| 16 |
+
messages: Annotated[Sequence[BaseMessage], lambda x, y: x + y]
|
|
|
|
| 17 |
|
| 18 |
+
# This is the modern way to create a robust agent with LangGraph.
|
| 19 |
+
# It automatically wires up the LLM, tools, and state into a graph.
|
| 20 |
+
def create_langgraph_agent():
|
| 21 |
+
print("Initializing LangGraphAgent...")
|
| 22 |
+
|
| 23 |
+
# 1. Set up the LLM (The "Brain")
|
| 24 |
+
llm = HuggingFaceEndpoint(
|
| 25 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 26 |
+
task="conversational",
|
| 27 |
+
max_new_tokens=512,
|
| 28 |
+
do_sample=False,
|
| 29 |
+
)
|
| 30 |
+
print("LLM initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# 2. Define the Tools
|
| 33 |
+
tools = [DuckDuckGoSearchRun()]
|
| 34 |
+
print("Tools initialized.")
|
| 35 |
+
|
| 36 |
+
# 3. Create the agent executor using LangGraph's prebuilt function
|
| 37 |
+
# This is much more robust than the previous AgentExecutor.
|
| 38 |
+
agent_executor = create_agent_executor(llm, tools, checkpointer=None)
|
| 39 |
+
print("LangGraph agent executor created.")
|
| 40 |
+
|
| 41 |
+
return agent_executor
|
| 42 |
|
| 43 |
+
# This function runs the agent for a single question.
|
| 44 |
+
def run_agent(agent_executor, question: str) -> str:
|
| 45 |
+
print(f"Agent received question: {question}")
|
| 46 |
+
try:
|
| 47 |
+
# We invoke the agent with the question in the correct message format
|
| 48 |
+
response = agent_executor.invoke({"messages": [("user", question)]})
|
| 49 |
+
# The final answer is in the last message of the output
|
| 50 |
+
final_answer = response['messages'][-1].content
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Error during agent execution: {e}")
|
| 53 |
+
final_answer = f"Error: Agent failed to execute. {e}"
|
| 54 |
+
|
| 55 |
+
print(f"Agent returning answer: {final_answer}")
|
| 56 |
+
return str(final_answer)
|
|
|
|
| 57 |
|
|
|
|
| 58 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 59 |
"""
|
| 60 |
+
Fetches all questions, runs the LangGraphAgent on them, submits all answers,
|
| 61 |
and displays the results.
|
| 62 |
"""
|
| 63 |
space_id = os.getenv("SPACE_ID")
|
| 64 |
+
if not profile:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
return "Please Login to Hugging Face with the button.", None
|
| 66 |
+
username = f"{profile.username}"
|
| 67 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
+
agent_executor = create_langgraph_agent()
|
| 70 |
except Exception as e:
|
|
|
|
| 71 |
return f"Error initializing agent: {e}", None
|
| 72 |
|
| 73 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 74 |
+
|
| 75 |
+
questions_url = f"https://agents-course-unit4-scoring.hf.space/questions"
|
| 76 |
try:
|
| 77 |
response = requests.get(questions_url, timeout=20)
|
| 78 |
response.raise_for_status()
|
| 79 |
questions_data = response.json()
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"Error fetching questions: {e}", None
|
| 82 |
|
|
|
|
|
|
|
| 83 |
answers_payload = []
|
| 84 |
for item in questions_data:
|
| 85 |
+
task_id, question_text = item.get("task_id"), item.get("question")
|
| 86 |
+
submitted_answer = run_agent(agent_executor, question_text)
|
|
|
|
| 87 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
|
|
|
| 88 |
|
|
|
|
| 89 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 90 |
+
submit_url = f"https://agents-course-unit4-scoring.hf.space/submit"
|
|
|
|
| 91 |
try:
|
| 92 |
+
response = requests.post(submit_url, json=submission_data, timeout=120) # Increased timeout
|
| 93 |
response.raise_for_status()
|
| 94 |
result_data = response.json()
|
| 95 |
final_status = (
|
|
|
|
| 99 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 100 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 101 |
)
|
| 102 |
+
return final_status, pd.DataFrame(answers_payload)
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
+
return f"Error during submission: {e}", pd.DataFrame(answers_payload)
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# --- Gradio Interface (Unchanged) ---
|
| 107 |
with gr.Blocks() as demo:
|
| 108 |
+
gr.Markdown("# Agent Evaluation Runner (LangGraph Version)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
gr.LoginButton()
|
| 110 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 111 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 112 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 113 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
demo.launch()
|