File size: 1,148 Bytes
e0a5876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from smolagents.tools import Tool
import requests

class HFSpeech2TextFromFile(Tool):
    name = "HFSpeech2TextFromFile"
    description = "This is a tool that returns text transcription from an audio          file path, calling huggingface-api under the hood."
    inputs = {'filepath': {'type': 'string', 'description': 'Path to audio file'}, 'hf_token': {'type': 'string', 'description': 'your huggingface token to call huggingface api'}, 'model': {'type': 'string', 'description': "optional, the model to call. Optional. Defaults to                 'whisper-small.en'", 'nullable': True}}
    output_type = "string"

    def forward(self, filepath: str, hf_token: str, \
                model: str = "whisper-small.en") -> str:
        import requests

        with open(filepath, "rb") as f:
            data = f.read()

        response = requests.post(
            f"https://api-inference.huggingface.co/models/openai/{model}", 
            headers={"Authorization": f"Bearer {hf_token}"}, 
            data=data
        )

        return response.json()['text']

    def __init__(self, *args, **kwargs):
        self.is_initialized = False