from select import select
from app.schemas.common import (
    IGetResponseBase,
    IPostResponseBase,
    IDeleteResponseBase,
)
from app.utils.zeroshot_nlp import analyze_text
from app.schemas.zeroshot_inference import (
    ZeroShotInferenceCreate,
    ZeroShotInferenceRead,
)
from fastapi_pagination import Page, Params
from sqlmodel.ext.asyncio.session import AsyncSession
from fastapi import APIRouter, Depends, HTTPException, Query
from app.api import deps
from app import crud
from app.models import ZeroShotInference
from app.models import ZeroShotInferenceBase
from app.models.user import User
from sqlmodel import select

router = APIRouter()


@router.get(
    "/zero-shot-classification-inferences/",
    response_model=IGetResponseBase[Page[ZeroShotInference]],
)
async def get_zero_shot_classification_inferences(
    params: Params = Depends(),
    db_session: AsyncSession = Depends(deps.get_db),
    current_user: User = Depends(deps.get_current_active_user),
):
    inferences = await crud.zeroshot_inference.get_multi_paginated(
        db_session, params=params
    )
    return IGetResponseBase[Page[ZeroShotInference]](data=inferences)


@router.get(
    "/zero-shot-classification-inferences/order_by_created_at/",
    response_model=IGetResponseBase[Page[ZeroShotInference]],
)
async def zero_shot_classification_inferences_order_by_created_at(
    params: Params = Depends(),
    db_session: AsyncSession = Depends(deps.get_db),
    current_user: User = Depends(deps.get_current_active_user),
):
    query = select(ZeroShotInference).order_by(ZeroShotInference.created_at)
    inferences = await crud.zeroshot_inference.get_multi_paginated(
        db_session, query=query, params=params
    )
    return IGetResponseBase[Page[ZeroShotInferenceRead]](data=inferences)


@router.post(
    "/zero-shot-classification-predict/",
    response_model=IPostResponseBase[ZeroShotInferenceRead],
)
async def predict(
    request: ZeroShotInferenceBase,
    db_session: AsyncSession = Depends(deps.get_db),
    current_user: User = Depends(deps.get_current_active_user),
):
    text = request.text
    labels = request.candidate_labels

    result = await analyze_text(text, labels)
    text = result[0]
    candidate_labels = result[1]
    res = result[2]

    inference = ZeroShotInferenceCreate(
        text=text, candidate_labels=candidate_labels, result=res
    )

    my_inference = await crud.zeroshot_inference.create_inference(
        db_session, obj_in=inference, user_id=current_user.id
    )

    return IPostResponseBase(data=ZeroShotInferenceRead.from_orm(my_inference))

app.utils.zeroshot_nlp.analyze_text sqlmodel.select fastapi.Depends fastapi.APIRouter app.crud.zeroshot_inference.get_multi_paginated app.crud.zeroshot_inference.create_inference app.schemas.zeroshot_inference.ZeroShotInferenceRead.from_orm app.schemas.zeroshot_inference.ZeroShotInferenceCreate
[((644, 655), 'fastapi.APIRouter', 'APIRouter', ([], {}), '()\n', (653, 655), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((852, 861), 'fastapi.Depends', 'Depends', ([], {}), '()\n', (859, 861), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((894, 914), 'fastapi.Depends', 'Depends', (['deps.get_db'], {}), '(deps.get_db)\n', (901, 914), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((941, 978), 'fastapi.Depends', 'Depends', (['deps.get_current_active_user'], {}), '(deps.get_current_active_user)\n', (948, 978), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1393, 1402), 'fastapi.Depends', 'Depends', ([], {}), '()\n', (1400, 1402), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1435, 1455), 'fastapi.Depends', 'Depends', (['deps.get_db'], {}), '(deps.get_db)\n', (1442, 1455), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1482, 1519), 'fastapi.Depends', 'Depends', (['deps.get_current_active_user'], {}), '(deps.get_current_active_user)\n', (1489, 1519), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((2003, 2023), 'fastapi.Depends', 'Depends', (['deps.get_db'], {}), '(deps.get_db)\n', (2010, 2023), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((2050, 2087), 'fastapi.Depends', 'Depends', (['deps.get_current_active_user'], {}), '(deps.get_current_active_user)\n', (2057, 2087), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((2292, 2377), 'app.schemas.zeroshot_inference.ZeroShotInferenceCreate', 'ZeroShotInferenceCreate', ([], {'text': 'text', 'candidate_labels': 'candidate_labels', 'result': 'res'}), '(text=text, candidate_labels=candidate_labels,\n    result=res)\n', (2315, 2377), False, 'from app.schemas.zeroshot_inference import ZeroShotInferenceCreate, ZeroShotInferenceRead\n'), ((1006, 1076), 'app.crud.zeroshot_inference.get_multi_paginated', 'crud.zeroshot_inference.get_multi_paginated', (['db_session'], {'params': 'params'}), '(db_session, params=params)\n', (1049, 1076), False, 'from app import crud\n'), ((1624, 1712), 'app.crud.zeroshot_inference.get_multi_paginated', 'crud.zeroshot_inference.get_multi_paginated', (['db_session'], {'query': 'query', 'params': 'params'}), '(db_session, query=query, params\n    =params)\n', (1667, 1712), False, 'from app import crud\n'), ((2174, 2200), 'app.utils.zeroshot_nlp.analyze_text', 'analyze_text', (['text', 'labels'], {}), '(text, labels)\n', (2186, 2200), False, 'from app.utils.zeroshot_nlp import analyze_text\n'), ((2414, 2513), 'app.crud.zeroshot_inference.create_inference', 'crud.zeroshot_inference.create_inference', (['db_session'], {'obj_in': 'inference', 'user_id': 'current_user.id'}), '(db_session, obj_in=inference,\n    user_id=current_user.id)\n', (2454, 2513), False, 'from app import crud\n'), ((1536, 1561), 'sqlmodel.select', 'select', (['ZeroShotInference'], {}), '(ZeroShotInference)\n', (1542, 1561), False, 'from sqlmodel import select\n'), ((2559, 2603), 'app.schemas.zeroshot_inference.ZeroShotInferenceRead.from_orm', 'ZeroShotInferenceRead.from_orm', (['my_inference'], {}), '(my_inference)\n', (2589, 2603), False, 'from app.schemas.zeroshot_inference import ZeroShotInferenceCreate, ZeroShotInferenceRead\n')]