|
| 1 | +from fastapi import APIRouter, Query, HTTPException |
| 2 | +from fastapi.responses import JSONResponse |
| 3 | +import aiohttp |
| 4 | +import logging |
| 5 | +from api_core.kiro import process_video_translation |
| 6 | +from api_core.schemas import PredictionResponse, TranslationResponse, ErrorResponse |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | +router = APIRouter() |
| 11 | + |
| 12 | +# Remote prediction endpoints |
| 13 | +REMOTE_API_URL = "http://100.71.0.60:8082" |
| 14 | +REMOTE_2_API_URL = "http://100.102.136.67:8082" |
| 15 | + |
| 16 | +@router.get("/GPU/predict", response_model=PredictionResponse, responses={500: {"model": ErrorResponse}}) |
| 17 | +async def remote_predict(video_url: str = Query(..., description="URL of the video to predict")): |
| 18 | + try: |
| 19 | + async with aiohttp.ClientSession() as session: |
| 20 | + async with session.get(f"{REMOTE_API_URL}/predict", params={"video_url": video_url}) as response: |
| 21 | + if response.status == 200: |
| 22 | + data = await response.json() |
| 23 | + return PredictionResponse(**data) |
| 24 | + else: |
| 25 | + error_text = await response.text() |
| 26 | + logger.error(f"Remote API error: {response.status} - {error_text}") |
| 27 | + raise HTTPException(status_code=response.status, detail=f"Remote API error: {response.status}") |
| 28 | + except HTTPException: |
| 29 | + raise |
| 30 | + except Exception as e: |
| 31 | + logger.error(f"Remote prediction error: {e}") |
| 32 | + raise HTTPException(status_code=500, detail=str(e)) |
| 33 | + |
| 34 | +@router.get("/GPU", response_model=TranslationResponse, responses={400: {"model": ErrorResponse}, 500: {"model": ErrorResponse}}) |
| 35 | +async def remote_predict_kiro(video_url: str = Query(..., description="URL of the video to predict")): |
| 36 | + try: |
| 37 | + # 1. Get prediction from remote API |
| 38 | + prediction_text = "" |
| 39 | + async with aiohttp.ClientSession() as session: |
| 40 | + async with session.get(f"{REMOTE_API_URL}/predict", params={"video_url": video_url}) as response: |
| 41 | + if response.status == 200: |
| 42 | + data = await response.json() |
| 43 | + prediction_text = data.get("prediction", "") |
| 44 | + else: |
| 45 | + error_text = await response.text() |
| 46 | + logger.error(f"Remote API error: {response.status} - {error_text}") |
| 47 | + raise HTTPException(status_code=response.status, detail=f"Remote API error: {response.status}") |
| 48 | + |
| 49 | + if not prediction_text: |
| 50 | + raise HTTPException(status_code=400, detail="No prediction received from remote API") |
| 51 | + |
| 52 | + logger.info(f"Remote model made prediction: {prediction_text}. This will be sent to Kiro API for translation.") |
| 53 | + |
| 54 | + result = await process_video_translation(prediction_text) |
| 55 | + |
| 56 | + if result: |
| 57 | + return TranslationResponse(**result) |
| 58 | + else: |
| 59 | + raise HTTPException(status_code=500, detail="Failed to get translation from Kiro API") |
| 60 | + |
| 61 | + except HTTPException: |
| 62 | + raise |
| 63 | + except Exception as e: |
| 64 | + logger.error(f"Remote prediction error: {e}") |
| 65 | + raise HTTPException(status_code=500, detail=str(e)) |
| 66 | + |
| 67 | +@router.get("/NPU", response_model=TranslationResponse, responses={400: {"model": ErrorResponse}, 500: {"model": ErrorResponse}}) |
| 68 | +async def remote_predict_kiro2(video_url: str = Query(..., description="URL of the video to predict")): |
| 69 | + try: |
| 70 | + # 1. Get prediction from remote API |
| 71 | + prediction_text = "" |
| 72 | + async with aiohttp.ClientSession() as session: |
| 73 | + async with session.get(f"{REMOTE_2_API_URL}/predict", params={"video_url": video_url}) as response: |
| 74 | + if response.status == 200: |
| 75 | + data = await response.json() |
| 76 | + prediction_text = data.get("prediction", "") |
| 77 | + else: |
| 78 | + error_text = await response.text() |
| 79 | + logger.error(f"Remote API error: {response.status} - {error_text}") |
| 80 | + raise HTTPException(status_code=response.status, detail=f"Remote API error: {response.status}") |
| 81 | + |
| 82 | + if not prediction_text: |
| 83 | + raise HTTPException(status_code=400, detail="No prediction received from remote API") |
| 84 | + |
| 85 | + logger.info(f"Remote model made prediction: {prediction_text}. This will be sent to Kiro API for translation.") |
| 86 | + |
| 87 | + result = await process_video_translation(prediction_text) |
| 88 | + |
| 89 | + if result: |
| 90 | + return TranslationResponse(**result) |
| 91 | + else: |
| 92 | + raise HTTPException(status_code=500, detail="Failed to get translation from Kiro API") |
| 93 | + |
| 94 | + except HTTPException: |
| 95 | + raise |
| 96 | + except Exception as e: |
| 97 | + logger.error(f"Remote prediction error: {e}") |
| 98 | + raise HTTPException(status_code=500, detail=str(e)) |
0 commit comments