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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -42
app.py CHANGED
@@ -5,55 +5,32 @@ from transformers import pipeline
5
 
6
  # Function to send the audio file to the Hugging Face Whisper API
7
  def query(file_data, my_key):
8
- API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo"
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
-
58
-
59
-
 
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
 
18
  # Get the user's Hugging Face API key
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_files: # Proceed only if the API key is provided and files are uploaded
25
  st.write("Processing your files...")
26
+ results = {}
27
+ for filename, file_data in uploaded.items():
28
+ # Save the file locally
29
+ with open(filename, "wb") as f:
30
+ f.write(file_data)
31
+
32
+ print(f"Sending {filename} to API...")
33
+ output = query(filename)
34
+
35
+ # Store the result
36
+ results[filename] = output