SyedHasanCronosPMC commited on
Commit
de9f6ca
·
verified ·
1 Parent(s): 95ca9a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from langgraph.graph import StateGraph, MessagesState, START, END
4
+ from langgraph.types import Command
5
+ from langchain_core.messages import HumanMessage
6
+ from langgraph.prebuilt import create_react_agent
7
+ from langchain_anthropic import ChatAnthropic
8
+
9
+ # Set Anthropic API key from Hugging Face secret environment
10
+ os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
11
+
12
+ # Initialize Claude 3.5 Sonnet LLM
13
+ llm = ChatAnthropic(model="claude-3-5-sonnet-latest")
14
+
15
+ # System prompt template function
16
+ def make_system_prompt(suffix: str) -> str:
17
+ return (
18
+ "You are a helpful AI assistant, collaborating with other assistants."
19
+ " Use the provided tools to progress towards answering the question."
20
+ " If you are unable to fully answer, that's OK, another assistant with different tools "
21
+ " will help where you left off. Execute what you can to make progress."
22
+ " If you or any of the other assistants have the final answer or deliverable,"
23
+ " prefix your response with FINAL ANSWER so the team knows to stop."
24
+ f"\n{suffix}"
25
+ )
26
+
27
+ # Research agent logic
28
+ def research_node(state: MessagesState) -> Command[str]:
29
+ agent = create_react_agent(
30
+ llm,
31
+ tools=[],
32
+ state_modifier=make_system_prompt("You can only do research.")
33
+ )
34
+ result = agent.invoke(state)
35
+ goto = END if "FINAL ANSWER" in result["messages"][-1].content else "chart_generator"
36
+ result["messages"][-1] = HumanMessage(
37
+ content=result["messages"][-1].content, name="researcher"
38
+ )
39
+ return Command(update={"messages": result["messages"]}, goto=goto)
40
+
41
+ # Chart generator logic
42
+ def chart_node(state: MessagesState) -> Command[str]:
43
+ agent = create_react_agent(
44
+ llm,
45
+ tools=[],
46
+ state_modifier=make_system_prompt("You can only generate charts.")
47
+ )
48
+ result = agent.invoke(state)
49
+ goto = END if "FINAL ANSWER" in result["messages"][-1].content else "researcher"
50
+ result["messages"][-1] = HumanMessage(
51
+ content=result["messages"][-1].content, name="chart_generator"
52
+ )
53
+ return Command(update={"messages": result["messages"]}, goto=goto)
54
+
55
+ # Build the LangGraph workflow
56
+ workflow = StateGraph(MessagesState)
57
+ workflow.add_node("researcher", research_node)
58
+ workflow.add_node("chart_generator", chart_node)
59
+ workflow.add_edge(START, "researcher")
60
+ workflow.add_edge("researcher", "chart_generator")
61
+ workflow.add_edge("chart_generator", END)
62
+ graph = workflow.compile()
63
+
64
+ # Function to execute LangGraph flow
65
+ def run_langgraph(user_input):
66
+ events = graph.stream(
67
+ {"messages": [("user", user_input)]},
68
+ {"recursion_limit": 150}
69
+ )
70
+ output = []
71
+ for event in events:
72
+ output.append(event)
73
+ return output[-1]["messages"][-1].content if output else "No output generated"
74
+
75
+ # Gradio interface logic
76
+ def process_input(user_input):
77
+ return run_langgraph(user_input)
78
+
79
+ # Launch Gradio app
80
+ interface = gr.Interface(
81
+ fn=process_input,
82
+ inputs="text",
83
+ outputs="text",
84
+ title="LangGraph Research Automation",
85
+ description="Enter your research task (e.g., 'Get GDP data for the USA over the past 5 years and create a chart.')"
86
+ )
87
+
88
+ if __name__ == "__main__":
89
+ interface.launch(server_name="0.0.0.0", server_port=7860)