Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import matplotlib.pyplot as plt
|
4 |
-
import pandas as pd
|
5 |
from langgraph.graph import StateGraph
|
6 |
-
from langgraph.prebuilt import create_react_agent
|
7 |
from langgraph.types import Command
|
8 |
from langchain_core.messages import HumanMessage
|
9 |
from langchain_anthropic import ChatAnthropic
|
10 |
|
11 |
-
# Set
|
12 |
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
|
13 |
|
14 |
-
# Claude 3.5 Sonnet
|
15 |
llm = ChatAnthropic(model="claude-3-5-sonnet-latest")
|
16 |
|
17 |
-
#
|
18 |
def make_system_prompt(suffix: str) -> str:
|
19 |
return (
|
20 |
-
"You are a helpful AI assistant, collaborating with other assistants."
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
24 |
-
"
|
25 |
-
"
|
26 |
-
f"
|
27 |
)
|
28 |
|
29 |
-
#
|
30 |
-
def research_node(state):
|
31 |
agent = create_react_agent(
|
32 |
llm,
|
33 |
tools=[],
|
34 |
-
|
35 |
)
|
36 |
result = agent.invoke(state)
|
37 |
goto = "chart_generator" if "FINAL ANSWER" not in result["messages"][-1].content else "__end__"
|
38 |
-
result["messages"][-1] =
|
39 |
-
content=result["messages"][-1].content,
|
40 |
-
name="researcher"
|
41 |
-
)
|
42 |
return Command(update={"messages": result["messages"]}, goto=goto)
|
43 |
|
44 |
-
#
|
45 |
-
def chart_node(state):
|
46 |
agent = create_react_agent(
|
47 |
llm,
|
48 |
tools=[],
|
49 |
-
|
50 |
)
|
51 |
result = agent.invoke(state)
|
52 |
-
result["messages"][-1] =
|
53 |
-
content=result["messages"][-1].content,
|
54 |
-
name="chart_generator"
|
55 |
-
)
|
56 |
return Command(update={"messages": result["messages"]}, goto="__end__")
|
57 |
|
58 |
-
# LangGraph
|
59 |
-
workflow = StateGraph(
|
60 |
workflow.add_node("researcher", research_node)
|
61 |
workflow.add_node("chart_generator", chart_node)
|
62 |
workflow.set_entry_point("researcher")
|
@@ -65,22 +58,19 @@ workflow.add_edge("researcher", "chart_generator")
|
|
65 |
graph = workflow.compile()
|
66 |
|
67 |
# LangGraph runner
|
68 |
-
def run_langgraph(
|
69 |
try:
|
70 |
-
events = graph.stream({"messages": [(
|
71 |
-
|
72 |
-
|
73 |
-
output.append(event)
|
74 |
-
|
75 |
-
final_response = output[-1]["messages"][-1].content
|
76 |
|
77 |
-
if "FINAL ANSWER" in
|
78 |
-
# Simulated chart
|
79 |
years = [2020, 2021, 2022, 2023, 2024]
|
80 |
gdp = [21.4, 22.0, 23.1, 24.8, 26.2]
|
81 |
|
82 |
plt.figure()
|
83 |
-
plt.plot(years, gdp, marker=
|
84 |
plt.title("USA GDP Over Last 5 Years")
|
85 |
plt.xlabel("Year")
|
86 |
plt.ylabel("GDP in Trillions USD")
|
@@ -90,11 +80,11 @@ def run_langgraph(input_text):
|
|
90 |
|
91 |
return "Chart generated based on FINAL ANSWER.", "gdp_chart.png"
|
92 |
else:
|
93 |
-
return
|
94 |
except Exception as e:
|
95 |
return f"Error: {str(e)}", None
|
96 |
|
97 |
-
# Gradio
|
98 |
def process_input(user_input):
|
99 |
return run_langgraph(user_input)
|
100 |
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import matplotlib.pyplot as plt
|
|
|
4 |
from langgraph.graph import StateGraph
|
5 |
+
from langgraph.prebuilt import MessagesState, create_react_agent
|
6 |
from langgraph.types import Command
|
7 |
from langchain_core.messages import HumanMessage
|
8 |
from langchain_anthropic import ChatAnthropic
|
9 |
|
10 |
+
# Set API key (ensure you add this as a secret in HF Spaces)
|
11 |
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
|
12 |
|
13 |
+
# Load Claude 3.5 Sonnet model
|
14 |
llm = ChatAnthropic(model="claude-3-5-sonnet-latest")
|
15 |
|
16 |
+
# System prompt constructor
|
17 |
def make_system_prompt(suffix: str) -> str:
|
18 |
return (
|
19 |
+
"You are a helpful AI assistant, collaborating with other assistants. "
|
20 |
+
"Use the provided tools to progress towards answering the question. "
|
21 |
+
"If you are unable to fully answer, that's OK—another assistant with different tools "
|
22 |
+
"will help where you left off. Execute what you can to make progress. "
|
23 |
+
"If you or any of the other assistants have the final answer or deliverable, "
|
24 |
+
"prefix your response with FINAL ANSWER so the team knows to stop.\n"
|
25 |
+
f"{suffix}"
|
26 |
)
|
27 |
|
28 |
+
# Research phase
|
29 |
+
def research_node(state: MessagesState) -> Command[str]:
|
30 |
agent = create_react_agent(
|
31 |
llm,
|
32 |
tools=[],
|
33 |
+
system_message=make_system_prompt("You can only do research.")
|
34 |
)
|
35 |
result = agent.invoke(state)
|
36 |
goto = "chart_generator" if "FINAL ANSWER" not in result["messages"][-1].content else "__end__"
|
37 |
+
result["messages"][-1].name = "researcher"
|
|
|
|
|
|
|
38 |
return Command(update={"messages": result["messages"]}, goto=goto)
|
39 |
|
40 |
+
# Chart generation phase
|
41 |
+
def chart_node(state: MessagesState) -> Command[str]:
|
42 |
agent = create_react_agent(
|
43 |
llm,
|
44 |
tools=[],
|
45 |
+
system_message=make_system_prompt("You can only generate charts.")
|
46 |
)
|
47 |
result = agent.invoke(state)
|
48 |
+
result["messages"][-1].name = "chart_generator"
|
|
|
|
|
|
|
49 |
return Command(update={"messages": result["messages"]}, goto="__end__")
|
50 |
|
51 |
+
# Build LangGraph
|
52 |
+
workflow = StateGraph(MessagesState)
|
53 |
workflow.add_node("researcher", research_node)
|
54 |
workflow.add_node("chart_generator", chart_node)
|
55 |
workflow.set_entry_point("researcher")
|
|
|
58 |
graph = workflow.compile()
|
59 |
|
60 |
# LangGraph runner
|
61 |
+
def run_langgraph(user_input):
|
62 |
try:
|
63 |
+
events = graph.stream({"messages": [HumanMessage(content=user_input)]})
|
64 |
+
outputs = list(events)
|
65 |
+
final_message = outputs[-1]["messages"][-1].content
|
|
|
|
|
|
|
66 |
|
67 |
+
if "FINAL ANSWER" in final_message:
|
68 |
+
# Simulated chart (you can later parse dynamic values if needed)
|
69 |
years = [2020, 2021, 2022, 2023, 2024]
|
70 |
gdp = [21.4, 22.0, 23.1, 24.8, 26.2]
|
71 |
|
72 |
plt.figure()
|
73 |
+
plt.plot(years, gdp, marker='o')
|
74 |
plt.title("USA GDP Over Last 5 Years")
|
75 |
plt.xlabel("Year")
|
76 |
plt.ylabel("GDP in Trillions USD")
|
|
|
80 |
|
81 |
return "Chart generated based on FINAL ANSWER.", "gdp_chart.png"
|
82 |
else:
|
83 |
+
return final_message, None
|
84 |
except Exception as e:
|
85 |
return f"Error: {str(e)}", None
|
86 |
|
87 |
+
# Gradio UI
|
88 |
def process_input(user_input):
|
89 |
return run_langgraph(user_input)
|
90 |
|