Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,155 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
-
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
from typing import TypedDict, Annotated
|
| 16 |
+
|
| 17 |
+
from duckduckgo_search import DDGS
|
| 18 |
+
|
| 19 |
+
from langchain.tools import tool
|
| 20 |
+
from langchain_core.messages import AnyMessage, HumanMessage
|
| 21 |
+
from langgraph.graph import START, StateGraph
|
| 22 |
+
from langgraph.graph.message import add_messages
|
| 23 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 24 |
+
|
| 25 |
+
from langchain_huggingface import (
|
| 26 |
+
HuggingFaceEndpoint,
|
| 27 |
+
ChatHuggingFace,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# =====================================================
|
| 31 |
+
# HF TOKEN FROM SPACE SECRET
|
| 32 |
+
# =====================================================
|
| 33 |
+
|
| 34 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
| 35 |
+
|
| 36 |
+
# =====================================================
|
| 37 |
+
# WEB SEARCH TOOL
|
| 38 |
+
# =====================================================
|
| 39 |
+
|
| 40 |
+
@tool
|
| 41 |
+
def web_search(query: str) -> str:
|
| 42 |
+
"""
|
| 43 |
+
Search the web and return useful snippets.
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
with DDGS() as ddgs:
|
| 48 |
+
results = list(ddgs.text(query, max_results=5))
|
| 49 |
+
|
| 50 |
+
if not results:
|
| 51 |
+
return "No results found."
|
| 52 |
+
|
| 53 |
+
output = []
|
| 54 |
+
|
| 55 |
+
for r in results:
|
| 56 |
+
output.append(
|
| 57 |
+
f"""
|
| 58 |
+
Title: {r.get("title","")}
|
| 59 |
+
Snippet: {r.get("body","")}
|
| 60 |
+
URL: {r.get("href","")}
|
| 61 |
+
"""
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return "\n".join(output)
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Search error: {e}"
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# =====================================================
|
| 71 |
+
# LLM
|
| 72 |
+
# =====================================================
|
| 73 |
+
|
| 74 |
+
llm = HuggingFaceEndpoint(
|
| 75 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 76 |
+
huggingfacehub_api_token=HF_TOKEN,
|
| 77 |
+
temperature=0.0,
|
| 78 |
+
max_new_tokens=1024,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
chat = ChatHuggingFace(
|
| 82 |
+
llm=llm,
|
| 83 |
+
verbose=False,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
tools = [web_search]
|
| 87 |
+
|
| 88 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 89 |
+
|
| 90 |
+
# =====================================================
|
| 91 |
+
# STATE
|
| 92 |
+
# =====================================================
|
| 93 |
+
|
| 94 |
+
class AgentState(TypedDict):
|
| 95 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 96 |
+
|
| 97 |
+
# =====================================================
|
| 98 |
+
# ASSISTANT NODE
|
| 99 |
+
# =====================================================
|
| 100 |
+
|
| 101 |
+
def assistant(state: AgentState):
|
| 102 |
+
|
| 103 |
+
response = chat_with_tools.invoke(
|
| 104 |
+
state["messages"]
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
return {
|
| 108 |
+
"messages": [response]
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
# =====================================================
|
| 112 |
+
# GRAPH
|
| 113 |
+
# =====================================================
|
| 114 |
+
|
| 115 |
+
builder = StateGraph(AgentState)
|
| 116 |
+
|
| 117 |
+
builder.add_node("assistant", assistant)
|
| 118 |
+
builder.add_node("tools", ToolNode(tools))
|
| 119 |
+
|
| 120 |
+
builder.add_edge(START, "assistant")
|
| 121 |
+
|
| 122 |
+
builder.add_conditional_edges(
|
| 123 |
+
"assistant",
|
| 124 |
+
tools_condition
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
builder.add_edge(
|
| 128 |
+
"tools",
|
| 129 |
+
"assistant"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
graph = builder.compile()
|
| 133 |
+
|
| 134 |
+
# =====================================================
|
| 135 |
+
# BASIC AGENT FOR HF COURSE
|
| 136 |
+
# =====================================================
|
| 137 |
+
|
| 138 |
class BasicAgent:
|
| 139 |
+
|
| 140 |
def __init__(self):
|
| 141 |
+
self.graph = graph
|
| 142 |
+
|
| 143 |
def __call__(self, question: str) -> str:
|
| 144 |
+
|
| 145 |
+
try:
|
| 146 |
+
|
| 147 |
+
result = self.graph.invoke(
|
| 148 |
+
{
|
| 149 |
+
"messages": [
|
| 150 |
+
HumanMessage(content=question)
|
| 151 |
+
]
|
| 152 |
+
},
|
| 153 |
+
config={
|
| 154 |
+
"recursion_limit": 10
|
| 155 |
+
}
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
return result["messages"][-1].content
|
| 159 |
+
|
| 160 |
+
except Exception as e:
|
| 161 |
+
return f"Agent error: {e}"
|
| 162 |
|
| 163 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 164 |
"""
|