sivakorn-su
commited on
Commit
·
37f4934
1
Parent(s):
ce543e3
improve route
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ import asyncio
|
|
| 11 |
import logging
|
| 12 |
|
| 13 |
from config import UPLOAD_FOLDER, SUPABASE_URL, SUPABASE_KEY
|
| 14 |
-
from models import pipelines, models, model_lock, load_model_bundle
|
| 15 |
from utils import (
|
| 16 |
save_uploaded_file,
|
| 17 |
extract_and_normalize_audio,
|
|
@@ -27,6 +27,7 @@ from utils import (
|
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
|
| 29 |
app = FastAPI()
|
|
|
|
| 30 |
origins = [o.strip() for o in (os.getenv("CORS_ORIGINS", "").split(",")) if o.strip()] or ["*"]
|
| 31 |
app.add_middleware(
|
| 32 |
CORSMiddleware,
|
|
@@ -44,17 +45,36 @@ app.add_middleware(
|
|
| 44 |
@app.on_event("startup")
|
| 45 |
async def startup_event():
|
| 46 |
async with model_lock:
|
| 47 |
-
if
|
| 48 |
-
logger.info("
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
@app.get("/")
|
| 60 |
async def check_api():
|
|
@@ -91,12 +111,13 @@ async def check_env():
|
|
| 91 |
"openai_key_exists": bool(os.environ.get("OPENAI_API_KEY")),
|
| 92 |
}
|
| 93 |
|
| 94 |
-
@app.post('/video')
|
| 95 |
-
async def upload_video(file: UploadFile = File(...)):
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
import os
|
| 101 |
from config import together_api_key
|
| 102 |
video_path = save_uploaded_file(file)
|
|
@@ -131,8 +152,7 @@ async def upload_video(file: UploadFile = File(...)):
|
|
| 131 |
"total_sentence": len(df_merged['text']),
|
| 132 |
"summaries": summaries,
|
| 133 |
})
|
| 134 |
-
|
| 135 |
-
@app.post("/upload_video")(upload_video)
|
| 136 |
|
| 137 |
@app.post("/sumarize/")
|
| 138 |
async def sumarize(array: list = Body(...)):
|
|
|
|
| 11 |
import logging
|
| 12 |
|
| 13 |
from config import UPLOAD_FOLDER, SUPABASE_URL, SUPABASE_KEY
|
| 14 |
+
from models import pipelines, models, model_lock, load_model_bundle, overlap_pipeline
|
| 15 |
from utils import (
|
| 16 |
save_uploaded_file,
|
| 17 |
extract_and_normalize_audio,
|
|
|
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
|
| 29 |
app = FastAPI()
|
| 30 |
+
app.router.redirect_slashes = False
|
| 31 |
origins = [o.strip() for o in (os.getenv("CORS_ORIGINS", "").split(",")) if o.strip()] or ["*"]
|
| 32 |
app.add_middleware(
|
| 33 |
CORSMiddleware,
|
|
|
|
| 45 |
@app.on_event("startup")
|
| 46 |
async def startup_event():
|
| 47 |
async with model_lock:
|
| 48 |
+
if pipelines and models and overlap_pipeline is not None:
|
| 49 |
+
logger.info("ℹ️ Models already loaded. Skipping startup load.")
|
| 50 |
+
return
|
| 51 |
+
|
| 52 |
+
logger.info("🔁 Loading models at startup...")
|
| 53 |
+
try:
|
| 54 |
+
pipeline, model, op = await load_model_bundle()
|
| 55 |
+
except Exception as e:
|
| 56 |
+
logger.exception(f"❌ Model loading failed: {e}")
|
| 57 |
+
import sys; sys.exit(1)
|
| 58 |
+
|
| 59 |
+
# idempotent set (load_model_bundle ก็ทำแล้ว แต่นี่กันไว้เผื่อเรียกจากที่อื่น)
|
| 60 |
+
if not pipelines:
|
| 61 |
+
pipelines.append(pipeline)
|
| 62 |
+
else:
|
| 63 |
+
pipelines[0] = pipeline
|
| 64 |
+
|
| 65 |
+
if not models:
|
| 66 |
+
models.append(model)
|
| 67 |
+
else:
|
| 68 |
+
models[0] = model
|
| 69 |
+
|
| 70 |
+
# ตั้ง overlap global ถ้าเพิ่งได้มา
|
| 71 |
+
from models import overlap_pipeline as _op_global # alias อ้างอิง global
|
| 72 |
+
if op is not None and _op_global is None:
|
| 73 |
+
# เซ็ตผ่านโมดูลเพื่อชัดเจนว่าเป็น global เดียวกัน
|
| 74 |
+
import models as _m
|
| 75 |
+
_m.overlap_pipeline = op
|
| 76 |
+
|
| 77 |
+
logger.info("✅ Models loaded successfully at startup")
|
| 78 |
|
| 79 |
@app.get("/")
|
| 80 |
async def check_api():
|
|
|
|
| 111 |
"openai_key_exists": bool(os.environ.get("OPENAI_API_KEY")),
|
| 112 |
}
|
| 113 |
|
| 114 |
+
# @app.post('/video')
|
| 115 |
+
# async def upload_video(file: UploadFile = File(...)):
|
| 116 |
+
# video_path = save_uploaded_file(file)
|
| 117 |
+
# return video_path
|
| 118 |
+
@app.api_route("/upload_video", methods=["POST"])
|
| 119 |
+
@app.api_route("/upload_video/", methods=["POST"])
|
| 120 |
+
def upload_video(file: UploadFile = File(...)):
|
| 121 |
import os
|
| 122 |
from config import together_api_key
|
| 123 |
video_path = save_uploaded_file(file)
|
|
|
|
| 152 |
"total_sentence": len(df_merged['text']),
|
| 153 |
"summaries": summaries,
|
| 154 |
})
|
| 155 |
+
|
|
|
|
| 156 |
|
| 157 |
@app.post("/sumarize/")
|
| 158 |
async def sumarize(array: list = Body(...)):
|