File size: 2,177 Bytes
900ecc2
dda9597
 
760bbe9
 
 
 
 
97d474b
 
 
 
 
900ecc2
760bbe9
 
 
 
 
 
 
 
900ecc2
 
97d474b
 
 
760bbe9
97d474b
 
 
760bbe9
1b704fc
 
 
760bbe9
1b704fc
 
 
760bbe9
 
 
97d474b
 
 
760bbe9
 
97d474b
 
 
 
760bbe9
97d474b
 
900ecc2
97d474b
760bbe9
 
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
56
57
58
59
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.")