initial commit
Browse files- handlerForAudio.py +46 -0
handlerForAudio.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
+
from dotenv import load_dotenv, find_dotenv
|
| 4 |
+
import os
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import json
|
| 7 |
+
from textToStoryGeneration import *
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
# Configure logging
|
| 11 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 12 |
+
# Configure logging
|
| 13 |
+
logging.basicConfig(level=logging.ERROR)
|
| 14 |
+
# Configure logging
|
| 15 |
+
logging.basicConfig(level=logging.WARNING)
|
| 16 |
+
|
| 17 |
+
load_dotenv(find_dotenv())
|
| 18 |
+
HUGGINFACE_API = os.getenv("HUGNINGFACEHUB_API_TOKEN")
|
| 19 |
+
|
| 20 |
+
class CustomHandler:
|
| 21 |
+
def __init__(self):
|
| 22 |
+
self.model_name = "espnet/kan-bayashi_ljspeech_vits"
|
| 23 |
+
self.endpoint = f"https://api-inference.huggingface.co/models/{self.model_name}"
|
| 24 |
+
|
| 25 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 26 |
+
# Prepare the payload with input data
|
| 27 |
+
logging.warning(f"------input_data-- {str(data)}")
|
| 28 |
+
payload = {"inputs": data}
|
| 29 |
+
print("payload----", payload)
|
| 30 |
+
# Set headers with API token
|
| 31 |
+
headers = {"Authorization": f"Bearer {HUGGINFACE_API}"}
|
| 32 |
+
|
| 33 |
+
# Send POST request to the Hugging Face model endpoint
|
| 34 |
+
response = requests.post(self.endpoint, json=payload, headers=headers)
|
| 35 |
+
with open('StoryAudio.mp3', 'wb') as file:
|
| 36 |
+
file.write(response.content)
|
| 37 |
+
return 'StoryAudio.mp3'
|
| 38 |
+
# Check if the request was successful
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Example usage
|
| 42 |
+
# if __name__ == "__main__":
|
| 43 |
+
# handler = CustomHandler()
|
| 44 |
+
# input_data = "Today I have tried with many model but I didnt find the any model which gives us better result and can be deployed on the endpoints. I think we need to Create custom Inference Handler and then it can be deployed on the interfernce end poitn.As I have deployed on model on interfernce endpoint i,e. text-to-story generation. I have also compared the result created with this endpoint and my local server as well that is not same. The endpoint is generating the different stroy."
|
| 45 |
+
# result = handler(input_data)
|
| 46 |
+
# print(result)dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddv 4
|