aahmed10202 commited on
Commit
2f1f82f
·
verified ·
1 Parent(s): 760bbe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -29
app.py CHANGED
@@ -23,36 +23,33 @@ my_key = st.text_input('Enter your Hugging Face API Key', type='password')
23
  uploaded_files = 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
- if uploaded_files:
27
- results = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- for uploaded_file in uploaded_files:
30
- st.write(f"Processing file: {uploaded_file.name}")
31
-
32
- # Check the MIME type of the uploaded file
33
- file_type = uploaded_file.type
34
- st.write(f"File type: {file_type}")
35
-
36
- # Validate file type (must be one of the supported types)
37
- if file_type not in ["audio/mpeg", "audio/wav", "audio/flac"]:
38
- st.write(f"Unsupported file type: {file_type}. Please upload an MP3, WAV, or FLAC file.")
39
- continue
40
-
41
- # Send the file to the Hugging Face API for transcription
42
- output = query(uploaded_file, my_key)
43
-
44
- # Store and display the result
45
- results[uploaded_file.name] = output
46
 
47
- # Show the transcription results for all uploaded files
48
- st.write("Results:")
49
- for file, result in results.items():
50
- st.write(f"**Results for {file}:**")
51
- st.json(result)
52
-
53
- else:
54
- st.write("Please upload an audio file to transcribe.")
55
- else:
56
- st.write("Please enter your Hugging Face API key to proceed.")
57
 
58
 
 
23
  uploaded_files = 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
+
 
54
 
55