File size: 2,089 Bytes
0cf9dfe
afc4964
03ceaeb
 
0cf9dfe
9789850
1957db6
0cf9dfe
afc4964
e06d02f
 
03ceaeb
afc4964
 
9789850
 
 
 
 
03ceaeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9789850
03ceaeb
9789850
 
50ba031
9789850
 
 
afc4964
 
 
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
import streamlit as st
import requests
from pydub import AudioSegment
from io import BytesIO

# Access the Hugging Face API key from the secrets
CTP_DATASCIENCE = st.secrets.get("CTP_DATASCIENCE")

# Set up the headers for the Hugging Face API request using the API key
headers = {"Authorization": f"Bearer {CTP_DATASCIENCE}"}

# Define the Hugging Face API URL for Whisper model
API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"

# Function to make the API request with the given file URL
def query(file_url):
    # Download the audio file from the Hugging Face Space URL
    response = requests.get(file_url)
    if response.status_code == 200:
        # Process the audio file into a compatible format (16 kHz, mono)
        audio_file = BytesIO(response.content)
        
        # Load and process the audio file using pydub
        audio = AudioSegment.from_file(audio_file)
        audio = audio.set_channels(1)  # Ensure mono audio
        audio = audio.set_frame_rate(16000)  # Ensure 16 kHz sample rate
        
        # Save the processed audio to a buffer in WAV format
        audio_buffer = BytesIO()
        audio.export(audio_buffer, format="wav")  # Export as WAV to ensure compatibility
        audio_buffer.seek(0)  # Reset pointer to the start of the buffer
        
        # Send the audio data to the Hugging Face API
        response_api = requests.post(API_URL, headers=headers, files={"file": audio_buffer})
        
        if response_api.status_code == 200:
            return response_api.json()  # Return the transcription result
        else:
            return {"error": "Failed to process the audio with Hugging Face API."}
    else:
        return {"error": "Failed to download the file from the provided URL."}

# URL to your audio file on Hugging Face Space
file_url = "https://huggingface.co/spaces/AndrewLam489/PillID_Transcribe/resolve/main/ce7581cd-534c-4f4b-b6a7-35be3e52fbc2.webm"

# Call the function with the audio file URL
output = query(file_url)

# Display the output of the API request
st.write(output)