Spaces:
Sleeping
Sleeping
Update backend/ai/main.py
Browse files- backend/ai/main.py +72 -23
backend/ai/main.py
CHANGED
|
@@ -1,38 +1,87 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import HTMLResponse, FileResponse
|
| 3 |
-
from fastapi.templating import Jinja2Templates
|
| 4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
| 11 |
-
allow_origins=["*"],
|
| 12 |
allow_credentials=True,
|
| 13 |
allow_methods=["*"],
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return templates.TemplateResponse("base.html", {"request": request})
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
@app.get("/
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
@app.
|
| 37 |
-
async def
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
# ===== Thêm backend/ai vào Python path =====
|
| 9 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
+
sys.path.append(BASE_DIR)
|
| 11 |
|
| 12 |
+
try:
|
| 13 |
+
from core.logic_flow import agent
|
| 14 |
+
from core.database import db
|
| 15 |
+
except ImportError as e:
|
| 16 |
+
print("❌ LỖI IMPORT: Không tìm thấy core.logic_flow hoặc core.database")
|
| 17 |
+
print(f"Chi tiết lỗi: {e}")
|
| 18 |
+
agent = None
|
| 19 |
+
class FakeDB:
|
| 20 |
+
def get_all_calls(self): return []
|
| 21 |
+
def update_call_rating(self, cid, s, n): pass
|
| 22 |
+
db = FakeDB()
|
| 23 |
|
| 24 |
+
# ===== FastAPI app =====
|
| 25 |
+
app = FastAPI(title="PINE-AI MVP")
|
| 26 |
+
|
| 27 |
+
# ===== CORS =====
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
| 30 |
+
allow_origins=["*"],
|
| 31 |
allow_credentials=True,
|
| 32 |
allow_methods=["*"],
|
| 33 |
allow_headers=["*"],
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# ===== Mount frontend =====
|
| 37 |
+
FRONTEND_DIR = os.path.join(os.path.dirname(BASE_DIR), "frontend")
|
| 38 |
+
|
| 39 |
+
# ===== Root redirect =====
|
| 40 |
+
@app.get("/")
|
| 41 |
+
def root_redirect():
|
| 42 |
+
return RedirectResponse(url="/desktop")
|
| 43 |
+
|
| 44 |
+
# ===== Tab routes =====
|
| 45 |
+
@app.get("/desktop")
|
| 46 |
+
def desktop():
|
| 47 |
+
return FileResponse(os.path.join(FRONTEND_DIR, "call_desktop.html"))
|
| 48 |
|
| 49 |
+
@app.get("/mobile")
|
| 50 |
+
def mobile():
|
| 51 |
+
return FileResponse(os.path.join(FRONTEND_DIR, "call_mobile.html"))
|
| 52 |
|
| 53 |
+
@app.get("/dashboard")
|
| 54 |
+
def dashboard():
|
| 55 |
+
return FileResponse(os.path.join(FRONTEND_DIR, "dashboard.html"))
|
|
|
|
| 56 |
|
| 57 |
+
# ===== API endpoint cho Dashboard =====
|
| 58 |
+
@app.get("/api/dashboard-stats")
|
| 59 |
+
def dashboard_stats():
|
| 60 |
+
try:
|
| 61 |
+
calls_data = db.get_all_calls()
|
| 62 |
+
return JSONResponse({"status": "success", "data": calls_data})
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
|
| 65 |
|
| 66 |
+
# ===== API endpoints cho call =====
|
| 67 |
+
@app.post("/start-call")
|
| 68 |
+
async def start_call(customer_id: str = Form(...)):
|
| 69 |
+
if not agent:
|
| 70 |
+
return {"error": "Chưa có logic_flow.py"}
|
| 71 |
+
return StreamingResponse(
|
| 72 |
+
agent.process_stream(customer_id, None),
|
| 73 |
+
media_type="application/x-ndjson"
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
@app.post("/chat-voice")
|
| 77 |
+
async def chat_voice(
|
| 78 |
+
customer_id: str = Form(...),
|
| 79 |
+
file: UploadFile = File(...)
|
| 80 |
+
):
|
| 81 |
+
if not agent:
|
| 82 |
+
return {"error": "Chưa có logic_flow.py"}
|
| 83 |
+
audio_bytes = await file.read()
|
| 84 |
+
return StreamingResponse(
|
| 85 |
+
agent.process_stream(customer_id, audio_bytes),
|
| 86 |
+
media_type="application/x-ndjson"
|
| 87 |
+
)
|