Spaces:
Sleeping
Sleeping
File size: 1,069 Bytes
9d0cb7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | """
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()
|