seawolf2357 commited on
Commit
cb9c2ae
·
verified ·
1 Parent(s): 81953d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -9
app.py CHANGED
@@ -8,6 +8,7 @@ import openai
8
  from dotenv import load_dotenv
9
  from fastapi import FastAPI, Request, UploadFile, File, Form
10
  from fastapi.responses import HTMLResponse, StreamingResponse
 
11
  from fastrtc import (
12
  AdditionalOutputs,
13
  AsyncStreamHandler,
@@ -1493,10 +1494,42 @@ class PersonalAssistantDB:
1493
  )
1494
  """)
1495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1496
  # Create indexes for better performance
1497
  await db.execute("CREATE INDEX IF NOT EXISTS idx_memories_category ON user_memories(category)")
1498
  await db.execute("CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id)")
1499
- await db.execute("CREATE INDEX IF NOT EXISTS idx_memories_document ON user_memories(is_document)")
 
 
 
 
 
1500
 
1501
  await db.commit()
1502
 
@@ -2318,14 +2351,10 @@ stream = Stream(
2318
  time_limit=300 if get_space() else None,
2319
  )
2320
 
2321
- app = FastAPI()
2322
-
2323
- # Mount stream
2324
- stream.mount(app)
2325
-
2326
- # Initialize database on startup
2327
- @app.on_event("startup")
2328
- async def startup_event():
2329
  try:
2330
  await PersonalAssistantDB.init()
2331
  print(f"Database initialized at: {DB_PATH}")
@@ -2346,6 +2375,16 @@ async def startup_event():
2346
  # Try to create directory if it doesn't exist
2347
  os.makedirs(PERSISTENT_DIR, exist_ok=True)
2348
  await PersonalAssistantDB.init()
 
 
 
 
 
 
 
 
 
 
2349
 
2350
  # Intercept offer to capture settings
2351
  @app.post("/webrtc/offer", include_in_schema=False)
 
8
  from dotenv import load_dotenv
9
  from fastapi import FastAPI, Request, UploadFile, File, Form
10
  from fastapi.responses import HTMLResponse, StreamingResponse
11
+ from contextlib import asynccontextmanager
12
  from fastrtc import (
13
  AdditionalOutputs,
14
  AsyncStreamHandler,
 
1494
  )
1495
  """)
1496
 
1497
+ # Check if table exists before trying to check columns
1498
+ cursor = await db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user_memories'")
1499
+ table_exists = await cursor.fetchone()
1500
+
1501
+ if table_exists:
1502
+ # Check if columns exist and add them if they don't (for migration)
1503
+ cursor = await db.execute("PRAGMA table_info(user_memories)")
1504
+ columns = await cursor.fetchall()
1505
+ column_names = [col[1] for col in columns]
1506
+
1507
+ if 'is_document' not in column_names:
1508
+ try:
1509
+ await db.execute("ALTER TABLE user_memories ADD COLUMN is_document BOOLEAN DEFAULT 0")
1510
+ print("Added is_document column to user_memories table")
1511
+ except Exception as e:
1512
+ print(f"Error adding is_document column: {e}")
1513
+
1514
+ if 'document_name' not in column_names:
1515
+ try:
1516
+ await db.execute("ALTER TABLE user_memories ADD COLUMN document_name TEXT")
1517
+ print("Added document_name column to user_memories table")
1518
+ except Exception as e:
1519
+ print(f"Error adding document_name column: {e}")
1520
+
1521
+ # Commit the schema changes before creating indexes
1522
+ await db.commit()
1523
+
1524
  # Create indexes for better performance
1525
  await db.execute("CREATE INDEX IF NOT EXISTS idx_memories_category ON user_memories(category)")
1526
  await db.execute("CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id)")
1527
+
1528
+ # Create index on is_document column
1529
+ try:
1530
+ await db.execute("CREATE INDEX IF NOT EXISTS idx_memories_document ON user_memories(is_document)")
1531
+ except Exception as e:
1532
+ print(f"Could not create index on is_document: {e}")
1533
 
1534
  await db.commit()
1535
 
 
2351
  time_limit=300 if get_space() else None,
2352
  )
2353
 
2354
+ # Lifespan event handler
2355
+ @asynccontextmanager
2356
+ async def lifespan(app: FastAPI):
2357
+ # Startup
 
 
 
 
2358
  try:
2359
  await PersonalAssistantDB.init()
2360
  print(f"Database initialized at: {DB_PATH}")
 
2375
  # Try to create directory if it doesn't exist
2376
  os.makedirs(PERSISTENT_DIR, exist_ok=True)
2377
  await PersonalAssistantDB.init()
2378
+
2379
+ yield # Application runs here
2380
+
2381
+ # Shutdown (if needed)
2382
+ print("Application shutting down...")
2383
+
2384
+ app = FastAPI(lifespan=lifespan)
2385
+
2386
+ # Mount stream
2387
+ stream.mount(app)
2388
 
2389
  # Intercept offer to capture settings
2390
  @app.post("/webrtc/offer", include_in_schema=False)