aahmed10202 commited on
Commit
900ecc2
·
verified ·
1 Parent(s): dda9597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -33
app.py CHANGED
@@ -1,39 +1,41 @@
1
- %%capture
2
- !pip install transformers
3
-
4
  from transformers import pipeline
5
-
6
- from google.colab import files
7
- uploaded = files.upload()
8
- print(f"Uploaded files: {list(uploaded.keys())}")
9
-
10
- from google.colab import userdata
11
- my_key = userdata.get('key')
12
-
13
  import requests
14
 
 
 
 
 
15
  API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
16
  headers = {"Authorization": f"Bearer {my_key}"}
17
 
18
- def query(filename):
19
- filename = list(uploaded.keys())[0] # Get the uploaded file's nameprint("Uploaded filename:", filename)
20
- with open(filename, "rb") as f:
21
- data = f.read()
22
- response = requests.post(API_URL, headers=headers, data=data)
23
- return response.json()
24
-
25
- results = {}
26
- for filename, file_data in uploaded.items():
27
- # Save the file locally
28
- with open(filename, "wb") as f:
29
- f.write(file_data)
30
-
31
- print(f"Sending {filename} to API...")
32
- output = query(filename)
33
-
34
- # Store the result
35
- results[filename] = output
36
-
37
- # Step 3: Print results
38
- for file, result in results.items():
39
- print(f"\nResults for {file}:\n{result}")
 
 
 
 
 
 
 
 
 
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.")