mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
fix - don't write file.filename
This commit is contained in:
parent
3dc2ec8119
commit
a900f352b5
1 changed files with 61 additions and 64 deletions
|
@ -2,6 +2,7 @@ import ast
|
||||||
import asyncio
|
import asyncio
|
||||||
import copy
|
import copy
|
||||||
import inspect
|
import inspect
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import secrets
|
import secrets
|
||||||
|
@ -3787,74 +3788,70 @@ async def audio_transcriptions(
|
||||||
|
|
||||||
router_model_names = llm_router.model_names if llm_router is not None else []
|
router_model_names = llm_router.model_names if llm_router is not None else []
|
||||||
|
|
||||||
assert (
|
if file.filename is None:
|
||||||
file.filename is not None
|
raise ProxyException(
|
||||||
) # make sure filename passed in (needed for type)
|
message="File name is None. Please check your file name",
|
||||||
|
code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
type="bad_request",
|
||||||
|
param="file",
|
||||||
|
)
|
||||||
|
|
||||||
_original_filename = file.filename
|
# Instead of writing to a file
|
||||||
file_extension = os.path.splitext(file.filename)[1]
|
file_content = await file.read()
|
||||||
# rename the file to a random hash file name -> we eventuall remove the file and don't want to remove any local files
|
file_object = io.BytesIO(file_content)
|
||||||
file.filename = f"tmp-request" + str(uuid.uuid4()) + file_extension
|
file_object.name = file.filename
|
||||||
|
data["file"] = file_object
|
||||||
|
try:
|
||||||
|
### CALL HOOKS ### - modify incoming data / reject request before calling the model
|
||||||
|
data = await proxy_logging_obj.pre_call_hook(
|
||||||
|
user_api_key_dict=user_api_key_dict,
|
||||||
|
data=data,
|
||||||
|
call_type="audio_transcription",
|
||||||
|
)
|
||||||
|
|
||||||
# IMP - Asserts that we've renamed the uploaded file, since we run os.remove(file.filename), we should rename the original file
|
## ROUTE TO CORRECT ENDPOINT ##
|
||||||
assert file.filename != _original_filename
|
# skip router if user passed their key
|
||||||
|
if "api_key" in data:
|
||||||
|
response = await litellm.atranscription(**data)
|
||||||
|
elif (
|
||||||
|
llm_router is not None and data["model"] in router_model_names
|
||||||
|
): # model in router model list
|
||||||
|
response = await llm_router.atranscription(**data)
|
||||||
|
|
||||||
with open(file.filename, "wb+") as f:
|
elif (
|
||||||
f.write(await file.read())
|
llm_router is not None and data["model"] in llm_router.deployment_names
|
||||||
try:
|
): # model in router deployments, calling a specific deployment on the router
|
||||||
data["file"] = open(file.filename, "rb")
|
response = await llm_router.atranscription(
|
||||||
### CALL HOOKS ### - modify incoming data / reject request before calling the model
|
**data, specific_deployment=True
|
||||||
data = await proxy_logging_obj.pre_call_hook(
|
|
||||||
user_api_key_dict=user_api_key_dict,
|
|
||||||
data=data,
|
|
||||||
call_type="audio_transcription",
|
|
||||||
)
|
)
|
||||||
|
elif (
|
||||||
## ROUTE TO CORRECT ENDPOINT ##
|
llm_router is not None
|
||||||
# skip router if user passed their key
|
and llm_router.model_group_alias is not None
|
||||||
if "api_key" in data:
|
and data["model"] in llm_router.model_group_alias
|
||||||
response = await litellm.atranscription(**data)
|
): # model set in model_group_alias
|
||||||
elif (
|
response = await llm_router.atranscription(
|
||||||
llm_router is not None and data["model"] in router_model_names
|
**data
|
||||||
): # model in router model list
|
) # ensure this goes the llm_router, router will do the correct alias mapping
|
||||||
response = await llm_router.atranscription(**data)
|
elif (
|
||||||
|
llm_router is not None
|
||||||
elif (
|
and data["model"] not in router_model_names
|
||||||
llm_router is not None
|
and llm_router.default_deployment is not None
|
||||||
and data["model"] in llm_router.deployment_names
|
): # model in router deployments, calling a specific deployment on the router
|
||||||
): # model in router deployments, calling a specific deployment on the router
|
response = await llm_router.atranscription(**data)
|
||||||
response = await llm_router.atranscription(
|
elif user_model is not None: # `litellm --model <your-model-name>`
|
||||||
**data, specific_deployment=True
|
response = await litellm.atranscription(**data)
|
||||||
)
|
else:
|
||||||
elif (
|
raise HTTPException(
|
||||||
llm_router is not None
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
and llm_router.model_group_alias is not None
|
detail={
|
||||||
and data["model"] in llm_router.model_group_alias
|
"error": "audio_transcriptions: Invalid model name passed in model="
|
||||||
): # model set in model_group_alias
|
+ data.get("model", "")
|
||||||
response = await llm_router.atranscription(
|
},
|
||||||
**data
|
)
|
||||||
) # ensure this goes the llm_router, router will do the correct alias mapping
|
except Exception as e:
|
||||||
elif (
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
llm_router is not None
|
finally:
|
||||||
and data["model"] not in router_model_names
|
file_object.close() # close the file read in by io library
|
||||||
and llm_router.default_deployment is not None
|
|
||||||
): # model in router deployments, calling a specific deployment on the router
|
|
||||||
response = await llm_router.atranscription(**data)
|
|
||||||
elif user_model is not None: # `litellm --model <your-model-name>`
|
|
||||||
response = await litellm.atranscription(**data)
|
|
||||||
else:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
|
||||||
detail={
|
|
||||||
"error": "audio_transcriptions: Invalid model name passed in model="
|
|
||||||
+ data.get("model", "")
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
|
||||||
finally:
|
|
||||||
os.remove(file.filename) # Delete the saved file
|
|
||||||
|
|
||||||
### ALERTING ###
|
### ALERTING ###
|
||||||
asyncio.create_task(
|
asyncio.create_task(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue