Spaces:
Runtime error
Runtime error
Update app.py
Browse filesupdated with langgraph
app.py
CHANGED
|
@@ -3,6 +3,11 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -14,74 +19,92 @@ from smolagent import Agent, Tool
|
|
| 14 |
from duckduckgo_search import ddg
|
| 15 |
import sympy
|
| 16 |
import re
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
try:
|
| 34 |
-
expression = sympy.sympify(input)
|
| 35 |
-
result = expression.evalf()
|
| 36 |
-
return str(result)
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return f"Math Error: {e}"
|
| 39 |
|
| 40 |
-
class
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
try:
|
| 46 |
-
local_vars = {}
|
| 47 |
-
exec(f"result = {input}", {}, local_vars)
|
| 48 |
-
return str(local_vars["result"])
|
| 49 |
-
except Exception as e:
|
| 50 |
-
return f"Code Error: {e}"
|
| 51 |
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
if any(
|
| 55 |
return "math"
|
| 56 |
-
elif any(
|
| 57 |
return "code"
|
| 58 |
-
elif any(
|
| 59 |
return "search"
|
| 60 |
else:
|
| 61 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
class BasicAgent:
|
| 64 |
def __init__(self):
|
| 65 |
-
|
| 66 |
-
tools=[
|
| 67 |
-
WikipediaSearchTool(),
|
| 68 |
-
MathSolverTool(),
|
| 69 |
-
CodeExecutionTool(),
|
| 70 |
-
]
|
| 71 |
-
)
|
| 72 |
|
| 73 |
def __call__(self, question: str) -> str:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
return MathSolverTool().use(question)
|
| 79 |
-
elif category == "code":
|
| 80 |
-
return CodeExecutionTool().use(question)
|
| 81 |
-
elif category == "search":
|
| 82 |
-
return WikipediaSearchTool().use(question)
|
| 83 |
-
else:
|
| 84 |
-
return self.agent.run(question)
|
| 85 |
|
| 86 |
|
| 87 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import sympy
|
| 7 |
+
import re
|
| 8 |
+
from duckduckgo_search import ddg
|
| 9 |
+
from langgraph.graph import StateGraph, END
|
| 10 |
+
from typing import TypedDict
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
|
|
| 19 |
from duckduckgo_search import ddg
|
| 20 |
import sympy
|
| 21 |
import re
|
| 22 |
+
# --- Tools ---
|
| 23 |
|
| 24 |
+
def wikipedia_search_tool(input: str) -> str:
|
| 25 |
+
results = ddg(input, max_results=3)
|
| 26 |
+
if results:
|
| 27 |
+
return results[0]["body"]
|
| 28 |
+
return "No information found."
|
| 29 |
|
| 30 |
+
def math_solver_tool(input: str) -> str:
|
| 31 |
+
try:
|
| 32 |
+
expr = sympy.sympify(input)
|
| 33 |
+
return str(expr.evalf())
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Math Error: {e}"
|
| 36 |
|
| 37 |
+
def code_execution_tool(input: str) -> str:
|
| 38 |
+
try:
|
| 39 |
+
local_vars = {}
|
| 40 |
+
exec(f"result = {input}", {}, local_vars)
|
| 41 |
+
return str(local_vars["result"])
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Code Error: {e}"
|
| 44 |
|
| 45 |
+
# --- State definition ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
class AgentState(TypedDict):
|
| 48 |
+
question: str
|
| 49 |
+
response: str
|
| 50 |
|
| 51 |
+
# --- Routing logic ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
def route_question(state: AgentState) -> str:
|
| 54 |
+
q = state["question"].lower()
|
| 55 |
+
if any(k in q for k in ["solve", "calculate", "evaluate", "math", "=", "^"]):
|
| 56 |
return "math"
|
| 57 |
+
elif any(k in q for k in ["code", "python", "return", "function"]):
|
| 58 |
return "code"
|
| 59 |
+
elif any(k in q for k in ["what", "who", "when", "where", "why", "capital", "invented"]):
|
| 60 |
return "search"
|
| 61 |
else:
|
| 62 |
+
return "search"
|
| 63 |
+
|
| 64 |
+
# --- LangGraph graph setup ---
|
| 65 |
+
|
| 66 |
+
builder = StateGraph(AgentState)
|
| 67 |
+
|
| 68 |
+
@builder.node()
|
| 69 |
+
def math_node(state: AgentState) -> AgentState:
|
| 70 |
+
return {"question": state["question"], "response": math_solver_tool(state["question"])}
|
| 71 |
+
|
| 72 |
+
@builder.node()
|
| 73 |
+
def code_node(state: AgentState) -> AgentState:
|
| 74 |
+
return {"question": state["question"], "response": code_execution_tool(state["question"])}
|
| 75 |
+
|
| 76 |
+
@builder.node()
|
| 77 |
+
def search_node(state: AgentState) -> AgentState:
|
| 78 |
+
return {"question": state["question"], "response": wikipedia_search_tool(state["question"])}
|
| 79 |
+
|
| 80 |
+
@builder.multi_choice()
|
| 81 |
+
def router(state: AgentState) -> str:
|
| 82 |
+
return route_question(state)
|
| 83 |
+
|
| 84 |
+
builder.set_entry_point("router", router)
|
| 85 |
+
builder.add_node("math", math_node)
|
| 86 |
+
builder.add_node("code", code_node)
|
| 87 |
+
builder.add_node("search", search_node)
|
| 88 |
+
builder.add_conditional_edges("router", {
|
| 89 |
+
"math": "math",
|
| 90 |
+
"code": "code",
|
| 91 |
+
"search": "search"
|
| 92 |
+
})
|
| 93 |
+
builder.add_edge("math", END)
|
| 94 |
+
builder.add_edge("code", END)
|
| 95 |
+
builder.add_edge("search", END)
|
| 96 |
+
|
| 97 |
+
app_graph = builder.compile()
|
| 98 |
|
| 99 |
class BasicAgent:
|
| 100 |
def __init__(self):
|
| 101 |
+
print("LangGraph Agent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def __call__(self, question: str) -> str:
|
| 104 |
+
state = {"question": question, "response": ""}
|
| 105 |
+
result = app_graph.invoke(state)
|
| 106 |
+
return result["response"]
|
| 107 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|