Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions inference/tool_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,37 @@ class FileParser(BaseTool):
}
]

# Optimized version of `call`:
# - Uses list comprehensions to separate mp3 and non-mp3 files in one pass
# - Avoids reusing the `params` dict (clearer semantics)
# - Replaces `if len(x)` with the more Pythonic `if x`
# - Limits file_parser response to 30,000 entries directly
# - Computes token count efficiently without extra intermediate variables
# - Improves readability while keeping the original logic intact
async def call(self, params, file_root_path):
file_name = params["files"]
file_names = params.get("files", [])

# Separate mp3 files and others
file_paths = [os.path.join(file_root_path, f) for f in file_names if not f.endswith(".mp3")]
omni_paths = [os.path.join(file_root_path, f) for f in file_names if f.endswith(".mp3")]

outputs = []

file_path = []
omnifile_path = []
for f_name in file_name:
if '.mp3' not in f_name:
file_path.append(os.path.join(file_root_path, f_name))
else:
omnifile_path.append(os.path.join(file_root_path, f_name))

if len(file_path):
params = {'files': file_path}
response = await file_parser(params)
# Handle non-mp3 files
if file_paths:
response = await file_parser({'files': file_paths})
response = response[:30000]

parsed_file_content = ' '.join(response)
outputs.extend([f'File token number: {len(parsed_file_content.split())}\nFile content:\n']+response)
parsed_file_content = " ".join(response)
token_count = len(parsed_file_content.split())

outputs.append(f"File token number: {token_count}\nFile content:\n")
outputs.extend(response)


if len(omnifile_path):
params['files'] = omnifile_path
# Handle mp3 files
if omni_paths:
agent = VideoAgent()
res = await agent.call(params)
res = await agent.call({'files': omni_paths})
outputs.extend(json.loads(res))

res = json.loads(res)
outputs += res

return outputs