Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tool): fal ai wizper ASR built-in tool #10716

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
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
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