aahmed10202 commited on
Commit
97d474b
·
verified ·
1 Parent(s): 04bbf0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -29
app.py CHANGED
@@ -1,41 +1,50 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
  import requests
4
 
5
- # Get the Hugging Face API Key from the user
 
 
 
 
6
  my_key = st.text_input('Enter your Hugging Face API Key', type='password')
7
 
8
- # Set the API URL for Whisper model
9
  API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
10
- headers = {"Authorization": f"Bearer {my_key}"}
11
 
12
- # Function to send the file to the API and get the response
13
- def query(filename, file_data):
14
- response = requests.post(API_URL, headers=headers, files={'file': file_data})
15
- return response.json()
16
 
17
- # Streamlit UI elements for file upload
18
- st.title("Whisper API Audio Transcription")
19
- st.markdown("Upload an audio file and get transcription results")
 
 
 
 
20
 
21
- # File uploader widget
22
  uploaded_files = st.file_uploader("Choose an audio file", type=["mp3", "wav", "flac"], accept_multiple_files=True)
23
 
24
- if uploaded_files:
25
- results = {}
26
- for uploaded_file in uploaded_files:
27
- st.write(f"Processing file: {uploaded_file.name}")
28
-
29
- # Send the file to Hugging Face API
30
- output = query(uploaded_file.name, uploaded_file)
31
-
32
- # Store the result
33
- results[uploaded_file.name] = output
34
-
35
- # Display the results
36
- st.write("Results:")
37
- for file, result in results.items():
38
- st.write(f"**Results for {file}:**")
39
- st.json(result)
 
 
 
 
40
  else:
41
- st.write("Please upload an audio file.")
 
 
1
  import streamlit as st
 
2
  import requests
3
 
4
+ # Title of the app
5
+ st.title("Whisper API Transcription Service")
6
+ st.markdown("Upload an audio file (mp3, wav, or flac) and get transcription results.")
7
+
8
+ # Prompt the user for their Hugging Face API key
9
  my_key = st.text_input('Enter your Hugging Face API Key', type='password')
10
 
11
+ # API URL for Whisper model
12
  API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
 
13
 
14
+ # Set up the headers with the provided API key
15
+ headers = {"Authorization": f"Bearer {my_key}"}
 
 
16
 
17
+ # Function to send the file to the API and get the transcription result
18
+ def query(file_data):
19
+ try:
20
+ response = requests.post(API_URL, headers=headers, files={'file': file_data})
21
+ return response.json()
22
+ except requests.exceptions.RequestException as e:
23
+ return {"error": str(e)}
24
 
25
+ # File uploader widget for audio files
26
  uploaded_files = st.file_uploader("Choose an audio file", type=["mp3", "wav", "flac"], accept_multiple_files=True)
27
 
28
+ # Handle file uploads and process them if API key is provided
29
+ if my_key: # Proceed only if the API key is provided
30
+ if uploaded_files:
31
+ results = {}
32
+ for uploaded_file in uploaded_files:
33
+ st.write(f"Processing file: {uploaded_file.name}")
34
+
35
+ # Send the file to the Hugging Face API
36
+ output = query(uploaded_file)
37
+
38
+ # Store and display the result
39
+ results[uploaded_file.name] = output
40
+
41
+ # Show results for all files
42
+ st.write("Results:")
43
+ for file, result in results.items():
44
+ st.write(f"**Results for {file}:**")
45
+ st.json(result)
46
+ else:
47
+ st.write("Please upload an audio file to transcribe.")
48
  else:
49
+ st.write("Please enter your Hugging Face API key to proceed.")
50
+