| """API routes - OpenAI compatible endpoints""" |
| from fastapi import APIRouter, Depends, HTTPException |
| from fastapi.responses import StreamingResponse, JSONResponse |
| from datetime import datetime |
| from typing import List |
| import json |
| import re |
| from ..core.auth import verify_api_key_header |
| from ..core.models import ChatCompletionRequest |
| from ..services.generation_handler import GenerationHandler, MODEL_CONFIG |
|
|
| router = APIRouter() |
|
|
| |
| generation_handler: GenerationHandler = None |
|
|
| def set_generation_handler(handler: GenerationHandler): |
| """Set generation handler instance""" |
| global generation_handler |
| generation_handler = handler |
|
|
| def _extract_remix_id(text: str) -> str: |
| """Extract remix ID from text |
| |
| Supports two formats: |
| 1. Full URL: https://sora.chatgpt.com/p/s_68e3a06dcd888191b150971da152c1f5 |
| 2. Short ID: s_68e3a06dcd888191b150971da152c1f5 |
| |
| Args: |
| text: Text to search for remix ID |
| |
| Returns: |
| Remix ID (s_[a-f0-9]{32}) or empty string if not found |
| """ |
| if not text: |
| return "" |
|
|
| |
| match = re.search(r's_[a-f0-9]{32}', text) |
| if match: |
| return match.group(0) |
|
|
| return "" |
|
|
| @router.get("/v1/models") |
| async def list_models(api_key: str = Depends(verify_api_key_header)): |
| """List available models""" |
| models = [] |
|
|
| for model_id, config in MODEL_CONFIG.items(): |
| description = f"{config['type'].capitalize()} generation" |
| if config['type'] == 'image': |
| description += f" - {config['width']}x{config['height']}" |
| elif config['type'] == 'video': |
| description += f" - {config['orientation']}" |
| elif config['type'] == 'prompt_enhance': |
| description += f" - {config['expansion_level']} ({config['duration_s']}s)" |
|
|
| models.append({ |
| "id": model_id, |
| "object": "model", |
| "owned_by": "sora2api", |
| "description": description |
| }) |
|
|
| return { |
| "object": "list", |
| "data": models |
| } |
|
|
| @router.post("/v1/chat/completions") |
| async def create_chat_completion( |
| request: ChatCompletionRequest, |
| api_key: str = Depends(verify_api_key_header) |
| ): |
| """Create chat completion (unified endpoint for image and video generation)""" |
| try: |
| |
| if not request.messages: |
| raise HTTPException(status_code=400, detail="Messages cannot be empty") |
|
|
| last_message = request.messages[-1] |
| content = last_message.content |
|
|
| |
| prompt = "" |
| image_data = request.image |
| video_data = request.video |
| remix_target_id = request.remix_target_id |
|
|
| if isinstance(content, str): |
| |
| prompt = content |
| |
| if not remix_target_id: |
| remix_target_id = _extract_remix_id(prompt) |
| elif isinstance(content, list): |
| |
| for item in content: |
| if isinstance(item, dict): |
| if item.get("type") == "text": |
| prompt = item.get("text", "") |
| |
| if not remix_target_id: |
| remix_target_id = _extract_remix_id(prompt) |
| elif item.get("type") == "image_url": |
| |
| image_url = item.get("image_url", {}) |
| url = image_url.get("url", "") |
| if url.startswith("data:image"): |
| |
| if "base64," in url: |
| image_data = url.split("base64,", 1)[1] |
| else: |
| image_data = url |
| elif item.get("type") == "video_url": |
| |
| video_url = item.get("video_url", {}) |
| url = video_url.get("url", "") |
| if url.startswith("data:video") or url.startswith("data:application"): |
| |
| if "base64," in url: |
| video_data = url.split("base64,", 1)[1] |
| else: |
| video_data = url |
| else: |
| |
| video_data = url |
| else: |
| raise HTTPException(status_code=400, detail="Invalid content format") |
|
|
| |
| if request.model not in MODEL_CONFIG: |
| raise HTTPException(status_code=400, detail=f"Invalid model: {request.model}") |
|
|
| |
| model_config = MODEL_CONFIG[request.model] |
| is_video_model = model_config["type"] == "video" |
|
|
| |
| if is_video_model and (video_data or remix_target_id): |
| if not request.stream: |
| |
| result = None |
| async for chunk in generation_handler.handle_generation( |
| model=request.model, |
| prompt=prompt, |
| image=image_data, |
| video=video_data, |
| remix_target_id=remix_target_id, |
| stream=False |
| ): |
| result = chunk |
|
|
| if result: |
| return JSONResponse(content=json.loads(result)) |
| else: |
| return JSONResponse( |
| status_code=500, |
| content={ |
| "error": { |
| "message": "Availability check failed", |
| "type": "server_error", |
| "param": None, |
| "code": None |
| } |
| } |
| ) |
|
|
| |
| if request.stream: |
| async def generate(): |
| try: |
| async for chunk in generation_handler.handle_generation( |
| model=request.model, |
| prompt=prompt, |
| image=image_data, |
| video=video_data, |
| remix_target_id=remix_target_id, |
| stream=True |
| ): |
| yield chunk |
| except Exception as e: |
| |
| error_data = None |
| try: |
| error_data = json.loads(str(e)) |
| except: |
| pass |
|
|
| |
| if error_data and isinstance(error_data, dict) and "error" in error_data: |
| |
| error_response = error_data |
| else: |
| |
| error_response = { |
| "error": { |
| "message": str(e), |
| "type": "server_error", |
| "param": None, |
| "code": None |
| } |
| } |
| error_chunk = f'data: {json.dumps(error_response)}\n\n' |
| yield error_chunk |
| yield 'data: [DONE]\n\n' |
|
|
| return StreamingResponse( |
| generate(), |
| media_type="text/event-stream", |
| headers={ |
| "Cache-Control": "no-cache", |
| "Connection": "keep-alive", |
| "X-Accel-Buffering": "no" |
| } |
| ) |
| else: |
| |
| result = None |
| async for chunk in generation_handler.handle_generation( |
| model=request.model, |
| prompt=prompt, |
| image=image_data, |
| video=video_data, |
| remix_target_id=remix_target_id, |
| stream=False |
| ): |
| result = chunk |
|
|
| if result: |
| return JSONResponse(content=json.loads(result)) |
| else: |
| |
| return JSONResponse( |
| status_code=500, |
| content={ |
| "error": { |
| "message": "Availability check failed", |
| "type": "server_error", |
| "param": None, |
| "code": None |
| } |
| } |
| ) |
|
|
| except Exception as e: |
| |
| return JSONResponse( |
| status_code=500, |
| content={ |
| "error": { |
| "message": str(e), |
| "type": "server_error", |
| "param": None, |
| "code": None |
| } |
| } |
| ) |
|
|