audio_to_text / app.py
aahmed10202's picture
Update app.py
760bbe9 verified
raw
history blame
2.18 kB
import streamlit as st
import requests
# Function to send the audio file to the Hugging Face Whisper API
def query(file_data, my_key):
API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
headers = {"Authorization": f"Bearer {my_key}"}
try:
response = requests.post(API_URL, headers=headers, files={'file': file_data})
return response.json()
except requests.exceptions.RequestException as e:
return {"error": str(e)}
# Streamlit UI elements
st.title("Whisper Transcription App")
st.write("Upload a .wav, .mp3, or .flac audio file, and get the transcription.")
# Get the user's Hugging Face API key
my_key = st.text_input('Enter your Hugging Face API Key', type='password')
# File uploader for audio files
uploaded_files = st.file_uploader("Choose an audio file", type=["mp3", "wav", "flac"], accept_multiple_files=True)
if my_key: # Proceed only if the API key is provided
if uploaded_files:
results = {}
for uploaded_file in uploaded_files:
st.write(f"Processing file: {uploaded_file.name}")
# Check the MIME type of the uploaded file
file_type = uploaded_file.type
st.write(f"File type: {file_type}")
# Validate file type (must be one of the supported types)
if file_type not in ["audio/mpeg", "audio/wav", "audio/flac"]:
st.write(f"Unsupported file type: {file_type}. Please upload an MP3, WAV, or FLAC file.")
continue
# Send the file to the Hugging Face API for transcription
output = query(uploaded_file, my_key)
# Store and display the result
results[uploaded_file.name] = output
# Show the transcription results for all uploaded files
st.write("Results:")
for file, result in results.items():
st.write(f"**Results for {file}:**")
st.json(result)
else:
st.write("Please upload an audio file to transcribe.")
else:
st.write("Please enter your Hugging Face API key to proceed.")