koler commited on
Commit
b466f4b
·
verified ·
1 Parent(s): cc17ca7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -33,15 +33,39 @@ graph.add_edge("process", END)
33
  agent = graph.compile()
34
 
35
  # Gradio chatbot handler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def chat_handler(message, history):
37
  # Convert Gradio history to LangChain messages
38
- if history is None:
39
- history = []
40
-
41
  messages = []
42
- for user_msg, bot_msg in history:
43
- messages.append(HumanMessage(content=user_msg))
44
- messages.append(AIMessage(content=bot_msg))
 
45
 
46
  # Add new user message
47
  messages.append(HumanMessage(content=message))
@@ -50,14 +74,15 @@ def chat_handler(message, history):
50
  result = agent.invoke({"messages": messages})
51
  updated_messages = result["messages"]
52
 
53
- # Convert back to (user, bot) pairs
54
- new_history = []
55
- for i in range(0, len(updated_messages) - 1, 2):
56
- user_msg = updated_messages[i].content
57
- bot_msg = updated_messages[i+1].content
58
- new_history.append((user_msg, bot_msg))
 
 
59
 
60
- return new_history, new_history
61
 
62
  # Gradio UI
63
  # chat_interface = gr.ChatInterface(
 
33
  agent = graph.compile()
34
 
35
  # Gradio chatbot handler
36
+ # def chat_handler(message, history):
37
+ # # Convert Gradio history to LangChain messages
38
+ # if history is None:
39
+ # history = []
40
+
41
+ # messages = []
42
+ # for user_msg, bot_msg in history:
43
+ # messages.append(HumanMessage(content=user_msg))
44
+ # messages.append(AIMessage(content=bot_msg))
45
+
46
+ # # Add new user message
47
+ # messages.append(HumanMessage(content=message))
48
+
49
+ # # Run the LangGraph agent
50
+ # result = agent.invoke({"messages": messages})
51
+ # updated_messages = result["messages"]
52
+
53
+ # # Convert back to (user, bot) pairs
54
+ # new_history = []
55
+ # for i in range(0, len(updated_messages) - 1, 2):
56
+ # user_msg = updated_messages[i].content
57
+ # bot_msg = updated_messages[i+1].content
58
+ # new_history.append((user_msg, bot_msg))
59
+
60
+ # return new_history, new_history
61
+
62
  def chat_handler(message, history):
63
  # Convert Gradio history to LangChain messages
 
 
 
64
  messages = []
65
+ if history is not None:
66
+ for user_msg, bot_msg in history:
67
+ messages.append(HumanMessage(content=user_msg))
68
+ messages.append(AIMessage(content=bot_msg))
69
 
70
  # Add new user message
71
  messages.append(HumanMessage(content=message))
 
74
  result = agent.invoke({"messages": messages})
75
  updated_messages = result["messages"]
76
 
77
+ # Extract the last AI message (response)
78
+ last_ai_response = updated_messages[-1].content
79
+
80
+ # Update Gradio-compatible history (list of tuples)
81
+ history = history if history else []
82
+ history.append((message, last_ai_response))
83
+
84
+ return last_ai_response, history
85
 
 
86
 
87
  # Gradio UI
88
  # chat_interface = gr.ChatInterface(