audio_to_text / app.py
aahmed10202's picture
Update app.py
1b704fc verified
raw
history blame
2.04 kB
import streamlit as st
import requests
import io
# Hugging Face API setup
API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
# Prompt the user for their Hugging Face API key
my_key = st.text_input('Enter your Hugging Face API Key', type='password')
# Set up headers with the provided API key
headers = {"Authorization": f"Bearer {my_key}"}
# Function to send the file to the API and get the transcription result
def query(file_data):
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)}
# File uploader widget for audio files
uploaded_files = st.file_uploader("Choose an audio file", type=["mp3", "wav", "flac"], accept_multiple_files=True)
# Handle file uploads and process them if API key is provided
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}")
# Validate file type and check if it's in the correct format
file_type = uploaded_file.type
st.write(f"File type: {file_type}")
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
output = query(uploaded_file)
# Store and display the result
results[uploaded_file.name] = output
# Show results for all 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.")