""" Gradio entrypoint matching 2_lab2.ipynb (memory + tool-calling graph). Hugging Face Spaces: set OPENAI_API_KEY in Space secrets. Optional: SERPER_API_KEY, PUSHOVER_TOKEN, PUSHOVER_USER. """ import os import gradio as gr from dotenv import load_dotenv from langgraph.checkpoint.memory import MemorySaver from graph_lab2 import build_graph load_dotenv() memory = MemorySaver() _graph = None config = {"configurable": {"thread_id": os.environ.get("LAB2_THREAD_ID", "hf-1")}} def _graph_instance(): global _graph if _graph is None: _graph = build_graph(memory) return _graph def chat(user_input: str, history): result = _graph_instance().invoke( {"messages": [{"role": "user", "content": user_input}]}, config=config, ) return result["messages"][-1].content demo = gr.ChatInterface( chat, type="messages", title="Lab 2 — LangGraph tools + memory", description="Search (Serper / fallbacks) and optional Pushover. Same flow as `2_lab2.ipynb`.", ) if __name__ == "__main__": demo.launch()