aahmed10202 commited on
Commit
fd8125a
1 Parent(s): 0048de5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -2,16 +2,19 @@ import streamlit as st
2
  import requests
3
  from transformers import pipeline
4
 
 
 
5
 
6
  # Function to send the audio file to the Hugging Face Whisper API
7
  def query(file_data, my_key):
8
- filename = list(uploaded.keys())[0] # Get the uploaded file's nameprint("Uploaded filename:", filename)
9
- with open(filename, "rb") as f:
10
- data = f.read()
11
- response = requests.post(API_URL, headers=headers, data=data)
12
- return response.json()
13
- # Streamlit UI elements
14
 
 
15
  st.title("Transcription App")
16
  st.write("Upload one or more .wav, .mp3, or .flac audio files, and get the transcription.")
17
 
@@ -19,23 +22,25 @@ st.write("Upload one or more .wav, .mp3, or .flac audio files, and get the trans
19
  my_key = st.text_input("Enter your Hugging Face API Key", type="password")
20
 
21
  # File uploader for audio files
22
- uploaded = st.file_uploader("Choose audio file(s)", type=["mp3", "wav", "flac"], accept_multiple_files=True)
23
 
24
- if my_key and uploaded: # Proceed only if the API key is provided and files are uploaded
25
  st.write("Processing your files...")
26
- results = {}
27
- for uploaded_file in uploaded.items():
28
- filename = uploaded_file.name
29
- file_data = uploaded_file.read()
30
- # Save the file locally
31
- with open(filename, "wb") as f:
32
- f.write(file_data)
33
 
34
- print(f"Sending {filename} to API...")
35
- output = query(filename)
36
 
37
  # Store the result
38
- results[filename] = output
39
-
 
40
  for file, result in results.items():
41
- st.write(f"\nResults for {file}:\n{result}")
 
 
 
 
 
2
  import requests
3
  from transformers import pipeline
4
 
5
+ # Hugging Face Whisper API endpoint
6
+ API_URL = "https://api-inference.huggingface.co/models/openai/whisper"
7
 
8
  # Function to send the audio file to the Hugging Face Whisper API
9
  def query(file_data, my_key):
10
+ headers = {"Authorization": f"Bearer {my_key}"}
11
+ response = requests.post(API_URL, headers=headers, data=file_data)
12
+ if response.status_code == 200:
13
+ return response.json()
14
+ else:
15
+ return {"error": response.text}
16
 
17
+ # Streamlit UI elements
18
  st.title("Transcription App")
19
  st.write("Upload one or more .wav, .mp3, or .flac audio files, and get the transcription.")
20
 
 
22
  my_key = st.text_input("Enter your Hugging Face API Key", type="password")
23
 
24
  # File uploader for audio files
25
+ uploaded_files = st.file_uploader("Choose audio file(s)", type=["mp3", "wav", "flac"], accept_multiple_files=True)
26
 
27
+ if my_key and uploaded_files: # Proceed only if the API key is provided and files are uploaded
28
  st.write("Processing your files...")
29
+ results = {} # Store results for each file
30
+
31
+ for uploaded_file in uploaded_files:
32
+ file_data = uploaded_file.read() # Read the file content
 
 
 
33
 
34
+ st.write(f"Sending {uploaded_file.name} to API...")
35
+ output = query(file_data, my_key) # Send the file to the API
36
 
37
  # Store the result
38
+ results[uploaded_file.name] = output
39
+
40
+ # Display the results
41
  for file, result in results.items():
42
+ st.write(f"### Results for `{file}`:")
43
+ if "error" in result:
44
+ st.error(result["error"])
45
+ else:
46
+ st.json(result)