File size: 2,360 Bytes
bd4f7b2
 
 
 
 
1129d6c
a108533
 
 
2734ce5
bd4f7b2
 
 
 
 
c2a94ab
bd4f7b2
ce531bd
669b0a4
 
bd4f7b2
 
 
a108533
bd4f7b2
 
 
 
 
 
 
 
 
 
 
 
 
 
1129d6c
a108533
5fa79a1
bd4f7b2
 
1129d6c
 
 
 
 
 
 
 
 
a108533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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

# Configure logging
logging.basicConfig(level=logging.DEBUG)
# Configure logging
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]:
        # Prepare the payload with input data
        logging.warning(f"------input_data-- {str(data)}")
        payload = str(data)
        logging.warning(f"payload----{str(payload)}")
        # Set headers with API token
        inputs = self.tokenizer(payload, return_tensors="pt")

        # Generate the waveform from the input text
        with torch.no_grad():
            outputs = self.model(**inputs)

        # Save the audio to a file
        sf.write("StoryAudio.wav", outputs["waveform"][0].numpy(), self.model.config.sampling_rate)
        uploadGraphFile("StoryAudio.wav")
        playAudio()
        #return 'StoryAudio.wav'
        # Check if the request was successful
        
def uploadGraphFile(fileName):
    # Configure Cloudinary credentials
    cloudinary.config( 
        cloud_name = "dm9tdqvp6", 
        api_key ="793865869491345", 
        api_secret = "0vhdvBoM35IWcO29NyI04Qj1PMo" 
    )
    # Upload a file to Cloudinary
    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 playAudio():
    root = tk.Tk()
    root.title("Audio Player")

    # Create a play button
    play_button = tk.Button(root, text="Play", command=play_audio)
    play_button.pack()

    # Create a stop button
    stop_button = tk.Button(root, text="Stop", command=stop_audio)
    stop_button.pack()

    # Create a progress bar
    progress_bar = ttk.Progressbar(root, orient="horizontal", length=200, mode="determinate")
    progress_bar.pack()
    root.mainloop()