fix - don't write file.filename

This commit is contained in:
Ishaan Jaff 2024-07-15 14:56:01 -07:00
parent 3dc2ec8119
commit a900f352b5

View file

@ -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,22 +3788,20 @@ 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
# IMP - Asserts that we've renamed the uploaded file, since we run os.remove(file.filename), we should rename the original file
assert file.filename != _original_filename
with open(file.filename, "wb+") as f:
f.write(await file.read())
try: try:
data["file"] = open(file.filename, "rb")
### CALL HOOKS ### - modify incoming data / reject request before calling the model ### CALL HOOKS ### - modify incoming data / reject request before calling the model
data = await proxy_logging_obj.pre_call_hook( data = await proxy_logging_obj.pre_call_hook(
user_api_key_dict=user_api_key_dict, user_api_key_dict=user_api_key_dict,
@ -3820,8 +3819,7 @@ async def audio_transcriptions(
response = await llm_router.atranscription(**data) response = await llm_router.atranscription(**data)
elif ( elif (
llm_router is not None llm_router is not None and data["model"] in llm_router.deployment_names
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( response = await llm_router.atranscription(
**data, specific_deployment=True **data, specific_deployment=True
@ -3850,11 +3848,10 @@ async def audio_transcriptions(
+ data.get("model", "") + data.get("model", "")
}, },
) )
except Exception as e: except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
finally: finally:
os.remove(file.filename) # Delete the saved file file_object.close() # close the file read in by io library
### ALERTING ### ### ALERTING ###
asyncio.create_task( asyncio.create_task(