Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,7 +4,7 @@ from fastapi.responses import FileResponse
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List, Optional
|
| 6 |
import os
|
| 7 |
-
import chat, voice, database
|
| 8 |
import traceback
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
@@ -72,6 +72,14 @@ async def chat_session_endpoint(session_id: str, request: ChatRequest):
|
|
| 72 |
database.add_message(session_id, "user", request.message)
|
| 73 |
database.add_message(session_id, "assistant", response_text)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return {"response": response_text}
|
| 76 |
except Exception as e:
|
| 77 |
traceback.print_exc()
|
|
@@ -98,3 +106,30 @@ async def speak_endpoint(request: VoiceRequest):
|
|
| 98 |
return FileResponse(audio_path, media_type="audio/mpeg", filename="response.mp3")
|
| 99 |
except Exception as e:
|
| 100 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List, Optional
|
| 6 |
import os
|
| 7 |
+
from . import chat, voice, database
|
| 8 |
import traceback
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
|
|
| 72 |
database.add_message(session_id, "user", request.message)
|
| 73 |
database.add_message(session_id, "assistant", response_text)
|
| 74 |
|
| 75 |
+
# If this was the first message (history was empty before this turn), generate a summary
|
| 76 |
+
if not db_history:
|
| 77 |
+
try:
|
| 78 |
+
summary = chat.generate_summary(request.message, response_text)
|
| 79 |
+
database.update_session_name(session_id, summary)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Failed to generate summary: {e}")
|
| 82 |
+
|
| 83 |
return {"response": response_text}
|
| 84 |
except Exception as e:
|
| 85 |
traceback.print_exc()
|
|
|
|
| 106 |
return FileResponse(audio_path, media_type="audio/mpeg", filename="response.mp3")
|
| 107 |
except Exception as e:
|
| 108 |
raise HTTPException(status_code=500, detail=str(e))
|
| 109 |
+
|
| 110 |
+
from fastapi.staticfiles import StaticFiles
|
| 111 |
+
from fastapi.responses import FileResponse
|
| 112 |
+
|
| 113 |
+
# Mount static files if they exist (for production/served build)
|
| 114 |
+
frontend_dist = os.path.join(os.path.dirname(__file__), "../frontend/dist")
|
| 115 |
+
assets_path = os.path.join(frontend_dist, "assets")
|
| 116 |
+
|
| 117 |
+
if os.path.exists(assets_path):
|
| 118 |
+
app.mount("/assets", StaticFiles(directory=assets_path), name="assets")
|
| 119 |
+
|
| 120 |
+
@app.get("/{full_path:path}")
|
| 121 |
+
async def serve_frontend(full_path: str):
|
| 122 |
+
# Only serve if dist exists
|
| 123 |
+
if os.path.exists(frontend_dist):
|
| 124 |
+
# Serve index.html for any other path (SPA routing)
|
| 125 |
+
# Check if file exists in dist, else serve index.html
|
| 126 |
+
target_file = os.path.join(frontend_dist, full_path)
|
| 127 |
+
if full_path and os.path.exists(target_file):
|
| 128 |
+
return FileResponse(target_file)
|
| 129 |
+
return FileResponse(os.path.join(frontend_dist, "index.html"))
|
| 130 |
+
|
| 131 |
+
# If dist doesn't exist, just return a message or 404 for frontend routes
|
| 132 |
+
# This allows backend to run even if frontend isn't built
|
| 133 |
+
if full_path == "":
|
| 134 |
+
return {"message": "Backend is running. Frontend build not found. Use 'npm run dev' for frontend development."}
|
| 135 |
+
raise HTTPException(status_code=404, detail="Frontend build not found")
|