pratikshahp commited on
Commit
267129f
·
verified ·
1 Parent(s): b6ed368

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -1,10 +1,9 @@
 
 
1
  from langgraph.graph import StateGraph, MessagesState, START, END
2
  from langgraph.checkpoint.memory import MemorySaver
3
  from langgraph.prebuilt import ToolNode
4
- from langchain_core.messages import HumanMessage
5
- from langchain_huggingface import HuggingFaceEndpoint
6
  from langchain_huggingface import ChatHuggingFace
7
- from langchain_core.tools import tool
8
  from typing import Literal
9
  import os
10
 
@@ -18,10 +17,7 @@ llm = ChatHuggingFace(
18
  max_new_tokens=150,
19
  )
20
 
21
- # Bind tools to the chat model
22
-
23
  # Define tools for travel planning
24
- @tool
25
  def search_destination(query: str):
26
  """Search for travel destinations based on preferences."""
27
  if "beach" in query.lower():
@@ -30,12 +26,10 @@ def search_destination(query: str):
30
  return "Destinations: Patagonia, Kilimanjaro, Swiss Alps"
31
  return "Popular Destinations: Paris, New York, Tokyo"
32
 
33
- @tool
34
  def fetch_flights(destination: str):
35
  """Fetch flight options to the destination."""
36
  return f"Flights available to {destination}: Option 1 - $500, Option 2 - $700."
37
 
38
- @tool
39
  def fetch_hotels(destination: str):
40
  """Fetch hotel recommendations for the destination."""
41
  return f"Hotels in {destination}: Hotel A ($200/night), Hotel B ($150/night)."
@@ -77,15 +71,37 @@ checkpointer = MemorySaver()
77
  # Compile the graph
78
  app = workflow.compile(checkpointer=checkpointer)
79
 
80
- # Use the graph
81
- initial_state = {"messages": [HumanMessage(content="I want a beach vacation.")]}
 
 
82
 
83
- final_state = app.invoke(initial_state)
84
- print(final_state["messages"][-1].content)
85
 
86
- # Continue the conversation
87
- next_state = app.invoke(
88
- {"messages": [HumanMessage(content="Tell me about flights to Bali.")]},
89
- config={"configurable": {"thread_id": 123}} # Reuse thread_id to maintain context
90
- )
91
- print(next_state["messages"][-1].content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain_core.messages import HumanMessage
3
  from langgraph.graph import StateGraph, MessagesState, START, END
4
  from langgraph.checkpoint.memory import MemorySaver
5
  from langgraph.prebuilt import ToolNode
 
 
6
  from langchain_huggingface import ChatHuggingFace
 
7
  from typing import Literal
8
  import os
9
 
 
17
  max_new_tokens=150,
18
  )
19
 
 
 
20
  # Define tools for travel planning
 
21
  def search_destination(query: str):
22
  """Search for travel destinations based on preferences."""
23
  if "beach" in query.lower():
 
26
  return "Destinations: Patagonia, Kilimanjaro, Swiss Alps"
27
  return "Popular Destinations: Paris, New York, Tokyo"
28
 
 
29
  def fetch_flights(destination: str):
30
  """Fetch flight options to the destination."""
31
  return f"Flights available to {destination}: Option 1 - $500, Option 2 - $700."
32
 
 
33
  def fetch_hotels(destination: str):
34
  """Fetch hotel recommendations for the destination."""
35
  return f"Hotels in {destination}: Hotel A ($200/night), Hotel B ($150/night)."
 
71
  # Compile the graph
72
  app = workflow.compile(checkpointer=checkpointer)
73
 
74
+ # Function to process user input and generate response
75
+ def chat(user_input, history):
76
+ # Prepare the initial state with the user's message
77
+ initial_state = {"messages": [HumanMessage(content=user_input)]}
78
 
79
+ # Invoke the workflow
80
+ final_state = app.invoke(initial_state)
81
 
82
+ # Extract the response content
83
+ response = final_state["messages"][-1].content
84
+
85
+ # Append the user input and response to history
86
+ history.append((user_input, response))
87
+
88
+ return history, history
89
+
90
+ # Create the Gradio interface
91
+ with gr.Blocks() as demo:
92
+ chatbot = gr.Chatbot()
93
+ with gr.Row():
94
+ with gr.Column():
95
+ user_input = gr.Textbox(
96
+ show_label=False,
97
+ placeholder="Enter your message...",
98
+ ).style(container=False)
99
+ with gr.Column():
100
+ submit_btn = gr.Button("Send")
101
+
102
+ # Set up the event handler for the submit button
103
+ submit_btn.click(chat, [user_input, chatbot], [chatbot, chatbot])
104
+ user_input.submit(chat, [user_input, chatbot], [chatbot, chatbot])
105
+
106
+ # Launch the Gradio app
107
+ demo.launch()