Update app.py
Browse files32 unmodified lines3334353637383940414243444546472 unmodified lines505152535455565758596061626332 unmodified linesagent = graph.compile()# Gradio chatbot handlerdef chat_handler(message, history):# Convert Gradio history to LangChain messagesif history is None:history = []messages = []for user_msg, bot_msg in history:messages.append(HumanMessage(content=user_msg))messages.append(AIMessage(content=bot_msg))# Add new user messagemessages.append(HumanMessage(content=message))2 unmodified linesresult = agent.invoke({"messages": messages})updated_messages = result["messages"]# Convert back to (user, bot) pairsnew_history = []for i in range(0, len(updated_messages) - 1, 2):user_msg = updated_messages[i].contentbot_msg = updated_messages[i+1].contentnew_history.append((user_msg, bot_msg))return new_history, new_history# Gradio UI# chat_interface = gr.ChatInterface(32 unmodified lines3334353637383940414243444546474849505152535455565758596061626364656667686970712 unmodified lines74757677787980818283848586878832 unmodified linesagent = graph.compile()# Gradio chatbot handler# def chat_handler(message, history):# # Convert Gradio history to LangChain messages# if history is None:# history = []# messages = []# for user_msg, bot_msg in history:# messages.append(HumanMessage(content=user_msg))# messages.append(AIMessage(content=bot_msg))# # Add new user message# messages.append(HumanMessage(content=message))# # Run the LangGraph agent# result = agent.invoke({"messages": messages})# updated_messages = result["messages"]# # Convert back to (user, bot) pairs# new_history = []# for i in range(0, len(updated_messages) - 1, 2):# user_msg = updated_messages[i].content# bot_msg = updated_messages[i+1].content# new_history.append((user_msg, bot_msg))# return new_history, new_historydef chat_handler(message, history):# Convert Gradio history to LangChain messagesmessages = []if history is not None:for user_msg, bot_msg in history:messages.append(HumanMessage(content=user_msg))messages.append(AIMessage(content=bot_msg))# Add new user messagemessages.append(HumanMessage(content=message))2 unmodified linesresult = agent.invoke({"messages": messages})updated_messages = result["messages"]# Extract the last AI message (response)last_ai_response = updated_messages[-1].content# Update Gradio-compatible history (list of tuples)history = history if history else []history.append((message, last_ai_response))return last_ai_response, history# Gradio UI# chat_interface = gr.ChatInterface(
app.py+38-13