|
from typing import Dict, List, Any |
|
|
|
import torch as torch |
|
from transformers import pipeline, WhisperProcessor, WhisperForConditionalGeneration |
|
|
|
import gradio as gr |
|
import subprocess |
|
import numpy as np |
|
import time |
|
|
|
import pandas as pd |
|
|
|
from datasets import Audio, Dataset |
|
|
|
|
|
|
|
class EndpointHandler(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, path=""): |
|
device = 0 if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
|
|
|
|
|
|
|
|
self.processor = WhisperProcessor.from_pretrained("openai/whisper-large") |
|
self.model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large") |
|
self.model.config.forced_decoder_ids = self.processor.get_decoder_prompt_ids(language="nl", task="transcribe") |
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
inputs (:obj: `str`) |
|
date (:obj: `str`) |
|
Return: |
|
A :obj:`list` | `dict`: will be serialized and returned |
|
""" |
|
|
|
print("request") |
|
print(data) |
|
print(data["inputs"]) |
|
|
|
|
|
inputs = data.pop("inputs", data) |
|
print("here comes text") |
|
print(inputs) |
|
data = [inputs] |
|
ds = pd.DataFrame(data, columns=['audio']) |
|
ds = Dataset.from_pandas(ds) |
|
|
|
ds = ds.cast_column("audio", Audio(sampling_rate=16_000)) |
|
input_speech = next(iter(ds))["audio"]["array"] |
|
input_features = self.processor(input_speech, return_tensors="pt").input_features |
|
predicted_ids = self.model.generate(input_features, forced_decoder_ids=self.model.config.forced_decoder_ids) |
|
transcription = self.processor.batch_decode(predicted_ids) |
|
print("this is the description") |
|
print(transcription) |
|
|
|
|
|
|
|
|
|
return transcription |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|