-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tool): fal ai wizper ASR built-in tool (#10716)
- Loading branch information
1 parent
5ff02b4
commit ad16180
Showing
4 changed files
with
617 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import io | ||
import os | ||
from typing import Any | ||
|
||
import fal_client | ||
|
||
from core.file.enums import FileAttribute, FileType | ||
from core.file.file_manager import download, get_attr | ||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class WizperTool(BuiltinTool): | ||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMessage: | ||
audio_file = tool_parameters.get("audio_file") | ||
task = tool_parameters.get("task", "transcribe") | ||
language = tool_parameters.get("language", "en") | ||
chunk_level = tool_parameters.get("chunk_level", "segment") | ||
version = tool_parameters.get("version", "3") | ||
|
||
if audio_file.type != FileType.AUDIO: | ||
return [self.create_text_message("Not a valid audio file.")] | ||
|
||
api_key = self.runtime.credentials["fal_api_key"] | ||
|
||
os.environ["FAL_KEY"] = api_key | ||
|
||
audio_binary = io.BytesIO(download(audio_file)) | ||
mime_type = get_attr(file=audio_file, attr=FileAttribute.MIME_TYPE) | ||
file_data = audio_binary.getvalue() | ||
|
||
try: | ||
audio_url = fal_client.upload(file_data, mime_type) | ||
|
||
except Exception as e: | ||
return [self.create_text_message(f"Error uploading audio file: {str(e)}")] | ||
|
||
arguments = { | ||
"audio_url": audio_url, | ||
"task": task, | ||
"language": language, | ||
"chunk_level": chunk_level, | ||
"version": version, | ||
} | ||
|
||
result = fal_client.subscribe( | ||
"fal-ai/wizper", | ||
arguments=arguments, | ||
with_logs=False, | ||
) | ||
|
||
return self.create_json_message(result) |
Oops, something went wrong.