Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,34 +52,28 @@ builder.add_edge("human_response_display", END)
|
|
| 52 |
SQLITE_DB_PATH = "langgraph_checkpoints.sqlite"
|
| 53 |
|
| 54 |
# --- Checkpointer and Graph Compilation ---
|
| 55 |
-
# Initialize
|
| 56 |
-
|
| 57 |
-
# For SqliteSaver, if from_conn_string is a context manager, you need to use `with`.
|
| 58 |
-
# A simpler approach that generally works better for global scope in web apps:
|
| 59 |
-
# Establish a direct SQLite connection and pass it to SqliteSaver.
|
| 60 |
try:
|
|
|
|
| 61 |
# Use check_same_thread=False for Gradio/web apps that might access the DB from different threads
|
| 62 |
-
# or if the connection is managed globally.
|
| 63 |
conn = sqlite3.connect(SQLITE_DB_PATH, check_same_thread=False)
|
| 64 |
global_memory_saver = SqliteSaver(conn=conn)
|
| 65 |
-
|
| 66 |
-
# SqliteSaver usually does this on first use or you can explicitly call a setup if available.
|
| 67 |
-
# In recent versions, just passing the connection is often enough for setup.
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Error initializing SqliteSaver connection: {e}")
|
| 70 |
-
#
|
| 71 |
-
global_memory_saver = None # Or exit the application
|
| 72 |
|
| 73 |
if global_memory_saver:
|
| 74 |
global_graph = builder.compile(
|
| 75 |
checkpointer=global_memory_saver,
|
| 76 |
interrupt_before=["human_input_interrupt"]
|
| 77 |
)
|
|
|
|
| 78 |
else:
|
| 79 |
print("SqliteSaver not initialized, graph compilation skipped.")
|
| 80 |
global_graph = None # Handle the case where checkpointer couldn't be initialized
|
| 81 |
|
| 82 |
-
|
| 83 |
# --- Gradio UI Logic ---
|
| 84 |
current_thread_id = gr.State("")
|
| 85 |
|
|
@@ -124,6 +118,8 @@ def resume_graph(human_input_from_ui: str, thread_id_state):
|
|
| 124 |
|
| 125 |
current_state_values["human_input"] = human_input_from_ui
|
| 126 |
|
|
|
|
|
|
|
| 127 |
# Use put_state() from the SqliteSaver instance
|
| 128 |
global_memory_saver.put_state(current_state_values, {"configurable": {"thread_id": thread_id_state}})
|
| 129 |
|
|
|
|
| 52 |
SQLITE_DB_PATH = "langgraph_checkpoints.sqlite"
|
| 53 |
|
| 54 |
# --- Checkpointer and Graph Compilation ---
|
| 55 |
+
global_memory_saver = None # Initialize to None
|
| 56 |
+
|
|
|
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
+
# Attempt to connect to SQLite
|
| 59 |
# Use check_same_thread=False for Gradio/web apps that might access the DB from different threads
|
|
|
|
| 60 |
conn = sqlite3.connect(SQLITE_DB_PATH, check_same_thread=False)
|
| 61 |
global_memory_saver = SqliteSaver(conn=conn)
|
| 62 |
+
print(f"SqliteSaver initialized successfully: {type(global_memory_saver)}") # ADD THIS LINE
|
|
|
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
print(f"Error initializing SqliteSaver connection: {e}")
|
| 65 |
+
# If an error occurs here, global_memory_saver will remain None
|
|
|
|
| 66 |
|
| 67 |
if global_memory_saver:
|
| 68 |
global_graph = builder.compile(
|
| 69 |
checkpointer=global_memory_saver,
|
| 70 |
interrupt_before=["human_input_interrupt"]
|
| 71 |
)
|
| 72 |
+
print("Graph compiled successfully.") # ADD THIS LINE
|
| 73 |
else:
|
| 74 |
print("SqliteSaver not initialized, graph compilation skipped.")
|
| 75 |
global_graph = None # Handle the case where checkpointer couldn't be initialized
|
| 76 |
|
|
|
|
| 77 |
# --- Gradio UI Logic ---
|
| 78 |
current_thread_id = gr.State("")
|
| 79 |
|
|
|
|
| 118 |
|
| 119 |
current_state_values["human_input"] = human_input_from_ui
|
| 120 |
|
| 121 |
+
print(f"Type of global_memory_saver before put_state: {type(global_memory_saver)}") # ADD THIS LINE
|
| 122 |
+
|
| 123 |
# Use put_state() from the SqliteSaver instance
|
| 124 |
global_memory_saver.put_state(current_state_values, {"configurable": {"thread_id": thread_id_state}})
|
| 125 |
|