Spaces:
Configuration error
Configuration error
Delete async_ai_voice.py
Browse files- async_ai_voice.py +0 -60
async_ai_voice.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
import aiohttp # Import aiohttp for making asynchronous HTTP requests
|
| 2 |
-
import os # Import os to access environment variables
|
| 3 |
-
import json # Import json to handle JSON data
|
| 4 |
-
from typing import Dict, Any # Import type hints for better code clarity
|
| 5 |
-
|
| 6 |
-
# Define the API endpoint URL for the Hugging Face Whisper model
|
| 7 |
-
API_URL = "https://api-inference.huggingface.co/models/distil-whisper/distil-large-v3"
|
| 8 |
-
|
| 9 |
-
# Set up the headers with the authorization token retrieved from environment variables
|
| 10 |
-
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
|
| 11 |
-
|
| 12 |
-
async def send_to_voice_AI(audio_data: bytes) -> Dict[str, Any]:
|
| 13 |
-
"""
|
| 14 |
-
Asynchronously sends audio data to the Hugging Face Whisper model for transcription.
|
| 15 |
-
|
| 16 |
-
Args:
|
| 17 |
-
audio_data (bytes): The raw audio data to be transcribed.
|
| 18 |
-
|
| 19 |
-
Returns:
|
| 20 |
-
Dict[str, Any]: A dictionary containing the transcription result or error message.
|
| 21 |
-
"""
|
| 22 |
-
print("Sending to voice AI") # Log the action for debugging purposes
|
| 23 |
-
|
| 24 |
-
# Create an asynchronous HTTP session using aiohttp
|
| 25 |
-
async with aiohttp.ClientSession() as session:
|
| 26 |
-
try:
|
| 27 |
-
# Asynchronously send a POST request to the API_URL with headers and audio data
|
| 28 |
-
async with session.post(API_URL, headers=headers, data=audio_data) as response:
|
| 29 |
-
# Await the response to ensure the request completes before proceeding
|
| 30 |
-
if response.status == 200:
|
| 31 |
-
# If the response is successful, await and return the JSON content
|
| 32 |
-
return await response.json()
|
| 33 |
-
else:
|
| 34 |
-
# If the response status is not 200, await the text of the response
|
| 35 |
-
error_text = await response.text()
|
| 36 |
-
# Parse the error text into a JSON object
|
| 37 |
-
error_json = json.loads(error_text)
|
| 38 |
-
# Return a dictionary with an error message and estimated loading time
|
| 39 |
-
return {
|
| 40 |
-
"text": f"error: {error_json.get('error', 'Unknown error')}\n"
|
| 41 |
-
f"Estimated time to load: {error_json.get('estimated_time', 'unknown')} seconds"
|
| 42 |
-
}
|
| 43 |
-
except aiohttp.ClientError as e:
|
| 44 |
-
# Handle any client-related errors that occur during the request
|
| 45 |
-
return {"text": f"Network error: {str(e)}"}
|
| 46 |
-
except json.JSONDecodeError:
|
| 47 |
-
# Handle errors that occur if the response cannot be parsed as JSON
|
| 48 |
-
return {"text": "Error: Unable to parse API response"}
|
| 49 |
-
|
| 50 |
-
# Example usage:
|
| 51 |
-
# The following example demonstrates how to use the send_to_voice_AI function asynchronously.
|
| 52 |
-
# It reads an audio file, sends it to the voice AI, and prints the output.
|
| 53 |
-
|
| 54 |
-
# import asyncio # Import asyncio to run the asynchronous function
|
| 55 |
-
# async def example():
|
| 56 |
-
# with open("sample1.flac", "rb") as f: # Open the audio file in binary read mode
|
| 57 |
-
# audio_data = f.read() # Read the audio data from the file
|
| 58 |
-
# output = await send_to_voice_AI(audio_data) # Await the asynchronous transcription function
|
| 59 |
-
# print(output) # Print the transcription result
|
| 60 |
-
# asyncio.run(example()) # Run the asynchronous example function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|