File size: 1,865 Bytes
900ecc2
dda9597
 
760bbe9
 
 
 
 
97d474b
 
 
 
 
900ecc2
760bbe9
 
 
 
 
 
 
 
fca7b5c
900ecc2
97d474b
2f1f82f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760bbe9
2f1f82f
 
760bbe9
2f1f82f
 
 
 
 
 
 
 
 
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
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 = 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
    API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
    headers = {"Authorization": f"Bearer {my_key}"}
    
    def query(filename):
      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()
    
    results = {}  
    for filename, file_data in uploaded.items():
        # 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
    
    # Step 3: Print results
    for file, result in results.items():
        st.write(f"\nResults for {file}:\n{result}")