Spaces:
Sleeping
Sleeping
aahmed10202
commited on
Commit
•
760bbe9
1
Parent(s):
1b704fc
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,58 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
-
import io
|
4 |
|
5 |
-
# Hugging Face API
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# Set up headers with the provided API key
|
12 |
-
headers = {"Authorization": f"Bearer {my_key}"}
|
13 |
-
|
14 |
-
# Function to send the file to the API and get the transcription result
|
15 |
-
def query(file_data):
|
16 |
try:
|
17 |
response = requests.post(API_URL, headers=headers, files={'file': file_data})
|
18 |
return response.json()
|
19 |
except requests.exceptions.RequestException as e:
|
20 |
return {"error": str(e)}
|
21 |
|
22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
uploaded_files = st.file_uploader("Choose an audio file", type=["mp3", "wav", "flac"], accept_multiple_files=True)
|
24 |
|
25 |
-
# Handle file uploads and process them if API key is provided
|
26 |
if my_key: # Proceed only if the API key is provided
|
27 |
if uploaded_files:
|
28 |
results = {}
|
|
|
29 |
for uploaded_file in uploaded_files:
|
30 |
st.write(f"Processing file: {uploaded_file.name}")
|
31 |
|
32 |
-
#
|
33 |
file_type = uploaded_file.type
|
34 |
st.write(f"File type: {file_type}")
|
35 |
|
|
|
36 |
if file_type not in ["audio/mpeg", "audio/wav", "audio/flac"]:
|
37 |
st.write(f"Unsupported file type: {file_type}. Please upload an MP3, WAV, or FLAC file.")
|
38 |
continue
|
39 |
-
|
40 |
-
# Send the file to the Hugging Face API
|
41 |
-
output = query(uploaded_file)
|
42 |
|
43 |
# Store and display the result
|
44 |
results[uploaded_file.name] = output
|
45 |
-
|
46 |
-
# Show results for all files
|
47 |
st.write("Results:")
|
48 |
for file, result in results.items():
|
49 |
st.write(f"**Results for {file}:**")
|
50 |
st.json(result)
|
|
|
51 |
else:
|
52 |
st.write("Please upload an audio file to transcribe.")
|
53 |
else:
|
54 |
st.write("Please enter your Hugging Face API key to proceed.")
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
3 |
|
4 |
+
# Function to send the audio file to the Hugging Face Whisper API
|
5 |
+
def query(file_data, my_key):
|
6 |
+
API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
|
7 |
+
headers = {"Authorization": f"Bearer {my_key}"}
|
8 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
try:
|
10 |
response = requests.post(API_URL, headers=headers, files={'file': file_data})
|
11 |
return response.json()
|
12 |
except requests.exceptions.RequestException as e:
|
13 |
return {"error": str(e)}
|
14 |
|
15 |
+
# Streamlit UI elements
|
16 |
+
st.title("Whisper Transcription App")
|
17 |
+
st.write("Upload a .wav, .mp3, or .flac audio file, and get the transcription.")
|
18 |
+
|
19 |
+
# Get the user's Hugging Face API key
|
20 |
+
my_key = st.text_input('Enter your Hugging Face API Key', type='password')
|
21 |
+
|
22 |
+
# File uploader for audio files
|
23 |
uploaded_files = st.file_uploader("Choose an audio file", type=["mp3", "wav", "flac"], accept_multiple_files=True)
|
24 |
|
|
|
25 |
if my_key: # Proceed only if the API key is provided
|
26 |
if uploaded_files:
|
27 |
results = {}
|
28 |
+
|
29 |
for uploaded_file in uploaded_files:
|
30 |
st.write(f"Processing file: {uploaded_file.name}")
|
31 |
|
32 |
+
# Check the MIME type of the uploaded file
|
33 |
file_type = uploaded_file.type
|
34 |
st.write(f"File type: {file_type}")
|
35 |
|
36 |
+
# Validate file type (must be one of the supported types)
|
37 |
if file_type not in ["audio/mpeg", "audio/wav", "audio/flac"]:
|
38 |
st.write(f"Unsupported file type: {file_type}. Please upload an MP3, WAV, or FLAC file.")
|
39 |
continue
|
40 |
+
|
41 |
+
# Send the file to the Hugging Face API for transcription
|
42 |
+
output = query(uploaded_file, my_key)
|
43 |
|
44 |
# Store and display the result
|
45 |
results[uploaded_file.name] = output
|
46 |
+
|
47 |
+
# Show the transcription results for all uploaded files
|
48 |
st.write("Results:")
|
49 |
for file, result in results.items():
|
50 |
st.write(f"**Results for {file}:**")
|
51 |
st.json(result)
|
52 |
+
|
53 |
else:
|
54 |
st.write("Please upload an audio file to transcribe.")
|
55 |
else:
|
56 |
st.write("Please enter your Hugging Face API key to proceed.")
|
57 |
+
|
58 |
+
|