File size: 2,041 Bytes
900ecc2
dda9597
1b704fc
dda9597
1b704fc
 
97d474b
 
900ecc2
 
1b704fc
97d474b
900ecc2
97d474b
 
 
 
 
 
 
900ecc2
97d474b
900ecc2
 
97d474b
 
 
 
 
 
 
1b704fc
 
 
 
 
 
 
 
97d474b
 
 
 
 
 
 
 
 
 
 
 
 
900ecc2
97d474b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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.")