Skip to content

Commit

Permalink
feat(tool): fal ai wizper ASR built-in tool (#10716)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdb02983rhy authored Nov 15, 2024
1 parent 5ff02b4 commit ad16180
Show file tree
Hide file tree
Showing 4 changed files with 617 additions and 2 deletions.
52 changes: 52 additions & 0 deletions api/core/tools/provider/builtin/fal/tools/wizper.py
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)
Loading

0 comments on commit ad16180

Please sign in to comment.