aahmed10202 commited on
Commit
125977e
1 Parent(s): fca7b5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -1,5 +1,7 @@
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):
@@ -7,47 +9,49 @@ def query(file_data, my_key):
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 = 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
- API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
27
- headers = {"Authorization": f"Bearer {my_key}"}
28
-
29
- def query(filename):
30
- filename = list(uploaded.keys())[0] # Get the uploaded file's nameprint("Uploaded filename:", filename)
31
- with open(filename, "rb") as f:
32
- data = f.read()
33
- response = requests.post(API_URL, headers=headers, data=data)
34
- return response.json()
35
 
36
- results = {}
37
- for filename, file_data in uploaded.items():
38
- # Save the file locally
39
- with open(filename, "wb") as f:
40
- f.write(file_data)
41
-
42
- print(f"Sending {filename} to API...")
43
- output = query(filename)
44
-
45
- # Store the result
46
- results[filename] = output
 
 
47
 
48
- # Step 3: Print results
49
  for file, result in results.items():
50
- st.write(f"\nResults for {file}:\n{result}")
 
 
 
 
51
 
52
 
53
 
 
1
  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):
 
9
  headers = {"Authorization": f"Bearer {my_key}"}
10
 
11
  try:
12
+ response = requests.post(API_URL, headers=headers, files={"file": file_data})
13
+ if response.status_code == 200:
14
+ return response.json()
15
+ else:
16
+ return {"error": f"API returned status code {response.status_code}: {response.text}"}
17
  except requests.exceptions.RequestException as e:
18
  return {"error": str(e)}
19
 
20
  # Streamlit UI elements
21
  st.title("Whisper Transcription App")
22
+ st.write("Upload one or more .wav, .mp3, or .flac audio files, and get the transcription.")
23
 
24
  # Get the user's Hugging Face API key
25
+ my_key = st.text_input("Enter your Hugging Face API Key", type="password")
26
 
27
  # File uploader for audio files
28
+ uploaded_files = st.file_uploader("Choose audio file(s)", type=["mp3", "wav", "flac"], accept_multiple_files=True)
29
 
30
+ if my_key and uploaded_files: # Proceed only if the API key is provided and files are uploaded
31
+ st.write("Processing your files...")
32
+ results = {}
 
 
 
 
 
 
 
33
 
34
+ for uploaded_file in uploaded_files:
35
+ try:
36
+ # Read the file data
37
+ file_data = uploaded_file.read()
38
+
39
+ # Query the API
40
+ st.write(f"Transcribing: {uploaded_file.name}")
41
+ result = query(file_data, my_key)
42
+
43
+ # Store the result
44
+ results[uploaded_file.name] = result
45
+ except Exception as e:
46
+ st.write(f"Error processing {uploaded_file.name}: {str(e)}")
47
 
48
+ # Display results
49
  for file, result in results.items():
50
+ st.write(f"**Results for {file}:**")
51
+ if "error" in result:
52
+ st.error(result["error"])
53
+ else:
54
+ st.json(result)
55
 
56
 
57