File size: 1,015 Bytes
81d9a9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
import requests
import json
import time


class whisper_api():
    def __init__(self, HF_Token) -> None:
        self.API_URL = "https://api-inference.huggingface.co/models/openai/whisper-base.en"
        self.Headers = {"Authorization": f"Bearer {HF_Token}"}

    def transcribe(self, audio_file_path: str):
        print('whisper working...')
        with open(audio_file_path, 'rb') as audio:
            data = audio.read()
        res = requests.post(self.API_URL, headers=self.Headers, data=data)
        # Verificar si el modelo está cargado completamente
        while res.status_code == 503 and "estimated_time" in res.json():
            estimated_time = res.json()["estimated_time"]
            print(
                f"El modelo está cargando. Esperando {estimated_time} segundos...")
            time.sleep((estimated_time+10))
            res = requests.post(self.API_URL, headers=self.Headers, data=data)

        response_data = json.loads(res.text)
        return response_data.get('text', '')