Update app.py
Browse files5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041055 unmodified linesimport gradio as grimport os# Load environmentload_dotenv(find_dotenv())# Define state schemaclass AgentState(TypedDict):messages: List[Union[HumanMessage, AIMessage]]# Initialize LLMllm = ChatGoogleGenerativeAI(model="models/gemini-1.5-flash-latest",temperature=1)# Define LangGraph nodedef process(state: AgentState) -> AgentState:response = llm.invoke(state["messages"])state["messages"].append(AIMessage(content=response.content))return state# Build the graphgraph = StateGraph(AgentState)graph.add_node("process", process)graph.add_edge(START, "process")graph.add_edge("process", END)agent = 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))# Run the LangGraph agentresult = 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(# fn=chat_handler,# title="LangGraph Gemini Chatbot",# theme="soft",# retry_btn=None,# undo_btn=None,# additional_inputs=[],# )chat_interface = gr.ChatInterface(fn=chat_handler,title="LangGraph Gemini Chatbot",theme="soft")# Launch appif __name__ == "__main__":chat_interface.launch()5 unmodified lines678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565 unmodified linesimport gradio as grimport os# Load environment variablesload_dotenv(find_dotenv())# Define Agent Stateclass AgentState(TypedDict):messages: List[Union[HumanMessage, AIMessage]]# Initialize Gemini modelllm = ChatGoogleGenerativeAI(model="models/gemini-1.5-flash-latest",temperature=1)# Processing nodedef process(state: AgentState) -> AgentState:response = llm.invoke(state["messages"])state["messages"].append(AIMessage(content=response.content))print(f"\nAI: {response.content}")return state# LangGraph setupgraph = StateGraph(AgentState)graph.add_node("process", process)graph.add_edge(START, "process")graph.add_edge("process", END)agent = graph.compile()# Maintain global conversation historyconversation_history = []# Gradio handlerdef chat(user_input):global conversation_historyconversation_history.append(HumanMessage(content=user_input))result = agent.invoke({"messages": conversation_history})conversation_history = result["messages"]return result["messages"][-1].content# Gradio UIiface = gr.Interface(fn=chat,inputs=gr.Textbox(placeholder="Enter your message"),outputs="text",title="LangGraph Gemini Chatbot")if __name__ == "__main__":iface.launch()
app.py+21-70