Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +28 -8
app/main.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from typing import List
|
| 5 |
import os
|
|
@@ -22,16 +23,30 @@ app = FastAPI(
|
|
| 22 |
@app.on_event("startup")
|
| 23 |
async def startup_event():
|
| 24 |
"""Display clickable link on startup"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
print("\n" + "="*70)
|
| 26 |
print("🚀 Google Docs Knowledge Chatbot is running!")
|
| 27 |
print("="*70)
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
print("="*70)
|
| 31 |
print("\n💡 Quick Tips:")
|
| 32 |
print(" • Click 'Index All Documents' to get started")
|
| 33 |
print(" • Make sure your Google Drive folder is shared")
|
| 34 |
-
|
|
|
|
| 35 |
print("\n" + "="*70 + "\n")
|
| 36 |
|
| 37 |
# Add CORS middleware
|
|
@@ -55,9 +70,18 @@ llm_service = LLMService(settings.groq_api_key)
|
|
| 55 |
# Create data directory
|
| 56 |
os.makedirs(settings.vector_store_path, exist_ok=True)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
@app.get("/")
|
| 60 |
async def root():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
"""Health check endpoint"""
|
| 62 |
return {
|
| 63 |
"status": "running",
|
|
@@ -574,8 +598,4 @@ async def clear_index():
|
|
| 574 |
except HTTPException:
|
| 575 |
raise
|
| 576 |
except Exception as e:
|
| 577 |
-
raise HTTPException(status_code=500, detail=f"Error clearing index: {str(e)}")
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
# Serve frontend
|
| 581 |
-
app.mount("/static", StaticFiles(directory="frontend"), name="static")
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from typing import List
|
| 6 |
import os
|
|
|
|
| 23 |
@app.on_event("startup")
|
| 24 |
async def startup_event():
|
| 25 |
"""Display clickable link on startup"""
|
| 26 |
+
import os
|
| 27 |
+
|
| 28 |
+
# Detect if running on HuggingFace Spaces
|
| 29 |
+
space_id = os.getenv("SPACE_ID")
|
| 30 |
+
|
| 31 |
print("\n" + "="*70)
|
| 32 |
print("🚀 Google Docs Knowledge Chatbot is running!")
|
| 33 |
print("="*70)
|
| 34 |
+
|
| 35 |
+
if space_id:
|
| 36 |
+
# Running on HuggingFace Spaces
|
| 37 |
+
print("\n📱 Application deployed on HuggingFace Spaces")
|
| 38 |
+
print(f" Space ID: {space_id}")
|
| 39 |
+
else:
|
| 40 |
+
# Running locally
|
| 41 |
+
print("\n📱 Access the application here:")
|
| 42 |
+
print("\n 👉 \033[94m\033[4mhttp://localhost:8000\033[0m\n")
|
| 43 |
+
|
| 44 |
print("="*70)
|
| 45 |
print("\n💡 Quick Tips:")
|
| 46 |
print(" • Click 'Index All Documents' to get started")
|
| 47 |
print(" • Make sure your Google Drive folder is shared")
|
| 48 |
+
if not space_id:
|
| 49 |
+
print(" • Press CTRL+C to stop the server")
|
| 50 |
print("\n" + "="*70 + "\n")
|
| 51 |
|
| 52 |
# Add CORS middleware
|
|
|
|
| 70 |
# Create data directory
|
| 71 |
os.makedirs(settings.vector_store_path, exist_ok=True)
|
| 72 |
|
| 73 |
+
# Mount static files BEFORE defining routes
|
| 74 |
+
app.mount("/static", StaticFiles(directory="frontend"), name="static")
|
| 75 |
+
|
| 76 |
|
| 77 |
@app.get("/")
|
| 78 |
async def root():
|
| 79 |
+
"""Serve the frontend HTML"""
|
| 80 |
+
return FileResponse("frontend/index.html")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
@app.get("/api/status")
|
| 84 |
+
async def api_status():
|
| 85 |
"""Health check endpoint"""
|
| 86 |
return {
|
| 87 |
"status": "running",
|
|
|
|
| 598 |
except HTTPException:
|
| 599 |
raise
|
| 600 |
except Exception as e:
|
| 601 |
+
raise HTTPException(status_code=500, detail=f"Error clearing index: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|