audio_to_text / app.py
aahmed10202's picture
Update app.py
0048de5 verified
raw
history blame
1.47 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):
filename = list(uploaded.keys())[0] # Get the uploaded file's nameprint("Uploaded filename:", filename)
with open(filename, "rb") as f:
data = f.read()
response = requests.post(API_URL, headers=headers, data=data)
return response.json()
# Streamlit UI elements
st.title("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 = st.file_uploader("Choose audio file(s)", type=["mp3", "wav", "flac"], accept_multiple_files=True)
if my_key and uploaded: # Proceed only if the API key is provided and files are uploaded
st.write("Processing your files...")
results = {}
for uploaded_file in uploaded.items():
filename = uploaded_file.name
file_data = uploaded_file.read()
# Save the file locally
with open(filename, "wb") as f:
f.write(file_data)
print(f"Sending {filename} to API...")
output = query(filename)
# Store the result
results[filename] = output
for file, result in results.items():
st.write(f"\nResults for {file}:\n{result}")