wishwakankanamg commited on
Commit
b9539bc
·
1 Parent(s): 6f32e62

print image to path

Browse files
Files changed (3) hide show
  1. __pycache__/agent.cpython-310.pyc +0 -0
  2. agent.py +6 -4
  3. app.py +31 -6
__pycache__/agent.cpython-310.pyc CHANGED
Binary files a/__pycache__/agent.cpython-310.pyc and b/__pycache__/agent.cpython-310.pyc differ
 
agent.py CHANGED
@@ -17,8 +17,8 @@ from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingF
17
  from langchain_anthropic import ChatAnthropic
18
 
19
  ##!other libraries
20
- from IPython.display import Image, display
21
-
22
 
23
  load_dotenv()
24
 
@@ -189,10 +189,12 @@ def build_graph(provider: str = "huggingface"):
189
  compiled_graph = builder.compile() # This line should already be there or be the next line
190
 
191
  try:
192
- display(Image(graph.get_graph().draw_mermaid_png()))
 
193
  except Exception:
194
  print('cant print the graph')
195
  # This requires some extra dependencies and is optional
196
  pass
197
 
198
- return compiled_graph
 
 
17
  from langchain_anthropic import ChatAnthropic
18
 
19
  ##!other libraries
20
+ from PIL import Image
21
+ import graphviz
22
 
23
  load_dotenv()
24
 
 
189
  compiled_graph = builder.compile() # This line should already be there or be the next line
190
 
191
  try:
192
+ dot_path = "graph"
193
+ compiled_graph.get_graph().render(dot_path, format="png", cleanup=True)
194
  except Exception:
195
  print('cant print the graph')
196
  # This requires some extra dependencies and is optional
197
  pass
198
 
199
+ return compiled_graph
200
+
app.py CHANGED
@@ -4,6 +4,7 @@ from huggingface_hub import InferenceClient
4
  from langchain_core.messages import HumanMessage
5
  from langchain.agents import AgentExecutor
6
  from agent import build_graph
 
7
 
8
  """
9
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
@@ -35,18 +36,42 @@ try:
35
  except Exception as e:
36
  print(f"Error instantiating agent: {e}")
37
 
 
 
 
 
 
 
38
  def run_langgraph_agent(user_input: str):
39
  graph = build_graph()
40
  result = graph.invoke({"messages": [HumanMessage(content=user_input)]})
41
  return result["messages"][-1].content if "messages" in result else result
42
 
43
 
44
- demo = gr.Interface(
45
- fn=run_langgraph_agent,
46
- inputs=gr.Textbox(lines=2, placeholder="Enter your message..."),
47
- outputs="text",
48
- title="LangGraph Agent Chat",
49
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  if __name__ == "__main__":
52
  demo.launch()
 
4
  from langchain_core.messages import HumanMessage
5
  from langchain.agents import AgentExecutor
6
  from agent import build_graph
7
+ from PIL import Image
8
 
9
  """
10
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
36
  except Exception as e:
37
  print(f"Error instantiating agent: {e}")
38
 
39
+ def show_graph():
40
+ if not os.path.exists("graph.png"):
41
+ return None
42
+ return Image.open("graph.png")
43
+
44
+
45
  def run_langgraph_agent(user_input: str):
46
  graph = build_graph()
47
  result = graph.invoke({"messages": [HumanMessage(content=user_input)]})
48
  return result["messages"][-1].content if "messages" in result else result
49
 
50
 
51
+ # demo = gr.Interface(
52
+ # fn=run_langgraph_agent,
53
+ # inputs=gr.Textbox(lines=2, placeholder="Enter your message..."),
54
+ # outputs="text",
55
+ # title="LangGraph Agent Chat",
56
+ # )
57
+
58
+ # Create Gradio Blocks for flexible layout
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("# LangGraph Agent Chat + Graph Viewer")
61
+
62
+ with gr.Row():
63
+ with gr.Column(scale=2):
64
+ user_input = gr.Textbox(lines=2, placeholder="Enter your message...")
65
+ submit_btn = gr.Button("Send")
66
+ chat_output = gr.Textbox(label="Agent Reply")
67
+
68
+ with gr.Column(scale=1):
69
+ graph_btn = gr.Button("Show Graph")
70
+ graph_image = gr.Image(label="LangGraph Visualization")
71
+
72
+ # Wire up the buttons to functions
73
+ submit_btn.click(run_langgraph_agent, inputs=user_input, outputs=chat_output)
74
+ graph_btn.click(show_graph, inputs=None, outputs=graph_image)
75
 
76
  if __name__ == "__main__":
77
  demo.launch()