优化google/upload接口的速度
* 优化google/upload接口的速度 * 切割视频接口新增输出质量选项 --------- Merge request URL: https://g-ldyi2063.coding.net/p/dev/d/modalDeploy/git/merge/4790?initial=true Co-authored-by: shuohigh@gmail.com
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
import os
|
||||
from typing import Annotated, Optional, cast
|
||||
from typing import Annotated, Optional, cast, List
|
||||
|
||||
import modal
|
||||
import sentry_sdk
|
||||
@@ -25,6 +25,7 @@ router = APIRouter(prefix="/google", tags=["Google"])
|
||||
class GoogleAPIKeyHeaders(BaseModel):
|
||||
x_google_api_key: Optional[str] = Field(description="Google API Key")
|
||||
|
||||
|
||||
class BundleHeaders(BaseModel):
|
||||
x_google_api_key: Optional[str] = Field(description="Google API Key")
|
||||
x_trace_id: str = Field(description="Sentry Transaction ID", default=None)
|
||||
@@ -41,8 +42,12 @@ async def upload_file_multipart(file: UploadFile,
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing Google API Key")
|
||||
content_length = file.size
|
||||
content_type = file.content_type
|
||||
if content_type not in ['video/mp4', 'video/mpeg', 'video/mov', 'video/avi', 'video/x-flv', 'video/mpg',
|
||||
'video/webm', 'video/wmv', 'video/3gpp']:
|
||||
# 允许上传的视频
|
||||
google_content_type_allow_list = ['video/mp4', 'video/mpeg', 'video/mov', 'video/avi', 'video/x-flv', 'video/mpg',
|
||||
'video/webm', 'video/wmv', 'video/3gpp', ]
|
||||
# 允许上传的图片
|
||||
google_content_type_allow_list.extend(["image/png", "image/jpeg", "image/webp", "image/heic", "image/heif"])
|
||||
if content_type not in google_content_type_allow_list:
|
||||
raise HTTPException(status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
|
||||
logger.info(f"Uploading name = {file.filename}, size = {content_length}, type = {content_type} to google file")
|
||||
with httpx.Client(timeout=1800) as client:
|
||||
@@ -63,7 +68,7 @@ async def upload_file_multipart(file: UploadFile,
|
||||
|
||||
upload_url = pre_upload_response.headers.get("X-Goog-Upload-Url")
|
||||
|
||||
upload_response = client.post(url=upload_url, content=file.file.read(), headers={
|
||||
upload_response = client.post(url=upload_url, content=file.file, headers={
|
||||
"X-Goog-Upload-Offset": "0",
|
||||
"X-Goog-Upload-Command": "upload, finalize",
|
||||
"Content-Type": content_type
|
||||
@@ -97,6 +102,7 @@ async def delete_file(filename: str, headers: Annotated[GoogleAPIKeyHeaders, Hea
|
||||
response.raise_for_status()
|
||||
return JSONResponse(content=response.json(), status_code=response.status_code)
|
||||
|
||||
|
||||
@router.delete('/delete_all', summary="删除所有已上传的文件/第一页")
|
||||
async def delete_all(headers: Annotated[GoogleAPIKeyHeaders, Header()]):
|
||||
google_api_key = headers.x_google_api_key or os.environ.get("GOOGLE_API_KEY")
|
||||
@@ -116,6 +122,7 @@ async def delete_all(headers: Annotated[GoogleAPIKeyHeaders, Header()]):
|
||||
return JSONResponse(content={"msg": f"删除文件{filename}失败"}, status_code=resp.status_code)
|
||||
return JSONResponse(content=response.json(), status_code=response.status_code)
|
||||
|
||||
|
||||
@router.get('/list', summary="列出已上传的文件")
|
||||
async def list_files(headers: Annotated[GoogleAPIKeyHeaders, Header()]):
|
||||
google_api_key = headers.x_google_api_key or os.environ.get("GOOGLE_API_KEY")
|
||||
@@ -128,20 +135,25 @@ async def list_files(headers: Annotated[GoogleAPIKeyHeaders, Header()]):
|
||||
return JSONResponse(content={}, status_code=response.status_code)
|
||||
return JSONResponse(content=response.json() if len(response.text) > 0 else "", status_code=response.status_code)
|
||||
|
||||
|
||||
@router.post('/inference_gemini', summary="使用Gemini推理hls视频流指定时间段的打点情况")
|
||||
async def inference_gemini(
|
||||
data:GeminiRequest,
|
||||
headers: Annotated[BundleHeaders, Header()],
|
||||
) -> ModalTaskResponse:
|
||||
data: GeminiRequest,
|
||||
headers: Annotated[BundleHeaders, Header()],
|
||||
) -> ModalTaskResponse:
|
||||
google_api_key = headers.x_google_api_key or os.environ.get("GOOGLE_API_KEY")
|
||||
if not google_api_key:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing Google API Key")
|
||||
fn = modal.Function.from_name(config.modal_app_name,"video_hls_slice_inference", environment_name=config.modal_environment)
|
||||
fn = modal.Function.from_name(config.modal_app_name, "video_hls_slice_inference",
|
||||
environment_name=config.modal_environment)
|
||||
fn_call = fn.spawn(data.media_hls_url, google_api_key, data.product_list, data.start_time, data.end_time,
|
||||
SentryTransactionInfo(x_trace_id=sentry_sdk.get_traceparent(), x_baggage=sentry_sdk.get_baggage())
|
||||
if headers.x_trace_id is None else SentryTransactionInfo(x_trace_id=headers.x_trace_id, x_baggage=headers.x_baggage))
|
||||
SentryTransactionInfo(x_trace_id=sentry_sdk.get_traceparent(),
|
||||
x_baggage=sentry_sdk.get_baggage())
|
||||
if headers.x_trace_id is None else SentryTransactionInfo(x_trace_id=headers.x_trace_id,
|
||||
x_baggage=headers.x_baggage))
|
||||
return ModalTaskResponse(success=True, taskId=fn_call.object_id)
|
||||
|
||||
|
||||
@router.get("/inference_gemini/{task_id}", summary="查询Gemini推理hls视频流指定时间段的打点任务")
|
||||
async def gemini_status(task_id: str, response: Response) -> GeminiResultResponse:
|
||||
task_info = await ModalUtils.get_modal_task_status(task_id)
|
||||
@@ -150,10 +162,11 @@ async def gemini_status(task_id: str, response: Response) -> GeminiResultRespons
|
||||
response.headers["x-baggage"] = task_info.transaction.x_baggage
|
||||
try:
|
||||
return GeminiResultResponse(taskId=task_id, status=task_info.status, code=cast(int, task_info.error_code.value),
|
||||
error=task_info.error_reason, result=json.dumps(task_info.results, ensure_ascii=False)
|
||||
error=task_info.error_reason,
|
||||
result=json.dumps(task_info.results, ensure_ascii=False)
|
||||
if task_info.results is not None else "")
|
||||
except Exception as e:
|
||||
logger.exception(f"获取Gemini状态发生错误 {e}")
|
||||
return GeminiResultResponse(taskId=task_id, status=task_info.status, code=cast(int, task_info.error_code.value),
|
||||
error=task_info.error_reason + f"获取Gemini状态发生错误 {e}"
|
||||
if task_info.error_reason else f"获取Gemini状态发生错误 {e}", result="")
|
||||
if task_info.error_reason else f"获取Gemini状态发生错误 {e}", result="")
|
||||
|
||||
Reference in New Issue
Block a user