33import fastapi
44from fastapi import BackgroundTasks
55from fastapi_mongo_base .routes import AbstractTaskRouter
6+ from fastapi_mongo_base .core import exceptions
67from usso .fastapi import jwt_access_security
78
89from .models import VoiceConvert
@@ -28,13 +29,51 @@ def __init__(self):
2829 def config_routes (self , ** kwargs ):
2930 super ().config_routes (update_route = False )
3031
32+ async def retrieve_item (
33+ self ,
34+ request : fastapi .Request ,
35+ uid : uuid .UUID ,
36+ ):
37+ user = await self .get_user (request )
38+ is_admin = "admin" in user .data .get ("scopes" , [])
39+ if is_admin :
40+ item = await self .get_item (uid , ignore_user_id = True )
41+ else :
42+ item = await self .get_item (uid , user_id = user .uid )
43+ return item
44+
3145 async def create_item (
3246 self ,
3347 request : fastapi .Request ,
3448 data : VoiceConvertTaskCreateSchema ,
3549 background_tasks : BackgroundTasks ,
50+ # user_id: uuid.UUID | None = fastapi.Body(
51+ # default=None,
52+ # embed=True,
53+ # description="Request for another User ID. It is possible only if the request user is admin. If not provided, the request user will be used.",
54+ # ),
3655 ):
37- return await super ().create_item (request , data .model_dump (), background_tasks )
56+ user = await self .get_user (request )
57+ is_admin = "admin" in user .data .get ("scopes" , [])
58+ # import logging
59+ # logging.info(f"user_id: {user_id} is_admin: {is_admin} user.uid: {user.uid}")
60+
61+ # if user_id and not is_admin and user_id != user.uid:
62+ # raise exceptions.BaseHTTPException(
63+ # status_code=403,
64+ # error="Forbidden",
65+ # message={
66+ # "en": "You are not allowed to request another user's ID.",
67+ # "fa": "شما مجوز دسترسی به آیدی کاربر دیگری را ندارید.",
68+ # },
69+ # )
70+
71+ user_id = user .uid
72+ item = await self .model .create_item ({** data .model_dump (), "user_id" : user_id })
73+
74+ if item .task_status == "init" or not self .draftable :
75+ background_tasks .add_task (item .start_processing )
76+ return item
3877
3978 async def webhook (
4079 self ,
@@ -48,3 +87,12 @@ async def webhook(
4887
4988
5089router = VoiceConvertRouter ().router
90+
91+
92+ @router .post ("/pitch" )
93+ async def get_pitch (url : str = fastapi .Body (..., embed = True )):
94+ from utils import voice
95+ from .services import get_voice
96+
97+ pitch_data = voice .get_voice_pitch_parselmouth (await get_voice (url ))
98+ return pitch_data
0 commit comments