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)