audio_to_text / app.py
aahmed10202's picture
Update app.py
125977e verified
raw
history blame
1.97 kB
import streamlit as st
import requests
from transformers import pipeline
# 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})
if response.status_code == 200:
return response.json()
else:
return {"error": f"API returned status code {response.status_code}: {response.text}"}
except requests.exceptions.RequestException as e:
return {"error": str(e)}
# Streamlit UI elements
st.title("Whisper Transcription App")
st.write("Upload one or more .wav, .mp3, or .flac audio files, 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 audio file(s)", type=["mp3", "wav", "flac"], accept_multiple_files=True)
if my_key and uploaded_files: # Proceed only if the API key is provided and files are uploaded
st.write("Processing your files...")
results = {}
for uploaded_file in uploaded_files:
try:
# Read the file data
file_data = uploaded_file.read()
# Query the API
st.write(f"Transcribing: {uploaded_file.name}")
result = query(file_data, my_key)
# Store the result
results[uploaded_file.name] = result
except Exception as e:
st.write(f"Error processing {uploaded_file.name}: {str(e)}")
# Display results
for file, result in results.items():
st.write(f"**Results for {file}:**")
if "error" in result:
st.error(result["error"])
else:
st.json(result)