|
from typing import Dict, Any |
|
import logging |
|
import torch |
|
import soundfile as sf |
|
from transformers import AutoTokenizer, AutoModelForTextToWaveform |
|
import cloudinary.uploader |
|
import tkinter as tk |
|
from tkinter import ttk |
|
import pygame |
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
|
|
logging.basicConfig(level=logging.WARNING) |
|
|
|
|
|
|
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
|
|
self.tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng") |
|
self.model= AutoModelForTextToWaveform.from_pretrained("facebook/mms-tts-eng") |
|
pygame.init() |
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: |
|
|
|
logging.warning(f"------input_data-- {str(data)}") |
|
payload = str(data) |
|
logging.warning(f"payload----{str(payload)}") |
|
|
|
inputs = self.tokenizer(payload, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = self.model(**inputs) |
|
|
|
|
|
sf.write("StoryAudio.wav", outputs["waveform"][0].numpy(), self.model.config.sampling_rate) |
|
uploadGraphFile("StoryAudio.wav") |
|
playAudioFile() |
|
|
|
|
|
|
|
def uploadGraphFile(fileName): |
|
|
|
cloudinary.config( |
|
cloud_name = "dm9tdqvp6", |
|
api_key ="793865869491345", |
|
api_secret = "0vhdvBoM35IWcO29NyI04Qj1PMo" |
|
) |
|
|
|
result = cloudinary.uploader.upload(fileName, folder="poc-graph", resource_type="raw") |
|
return result |
|
|
|
def play_audio(): |
|
pygame.mixer.music.load("StoryAudio.wav") |
|
pygame.mixer.music.play() |
|
|
|
def stop_audio(): |
|
pygame.mixer.music.stop() |
|
|
|
def playAudioFile(): |
|
root = tk.Tk() |
|
root.title("Audio Player") |
|
|
|
|
|
play_button = tk.Button(root, text="Play", command=play_audio) |
|
play_button.pack() |
|
|
|
|
|
stop_button = tk.Button(root, text="Stop", command=stop_audio) |
|
stop_button.pack() |
|
|
|
|
|
progress_bar = ttk.Progressbar(root, orient="horizontal", length=200, mode="determinate") |
|
progress_bar.pack() |
|
root.mainloop() |
|
|