Create usage/python.py
Browse files- usage/python.py +37 -0
usage/python.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
# API endpoint - https://tri4-semalab.hf.space/
|
4 |
+
url = "https://jikoni-semabox.hf.space/transcribe"
|
5 |
+
|
6 |
+
# Path to the audio file you want to transcribe
|
7 |
+
audio_file_path = "/content/audio_samples/sample7.wav"
|
8 |
+
#audio_file_path ="/content/audio_samples/audio_file_test.wav"
|
9 |
+
|
10 |
+
# Open the audio file in binary mode
|
11 |
+
try:
|
12 |
+
with open(audio_file_path, 'rb') as audio_file:
|
13 |
+
# Prepare the files dictionary with the 'audio' key
|
14 |
+
files = {
|
15 |
+
'audio': audio_file
|
16 |
+
}
|
17 |
+
|
18 |
+
# Send a POST request to the API
|
19 |
+
response = requests.post(url, files=files)
|
20 |
+
|
21 |
+
# Check if the request was successful
|
22 |
+
if response.status_code == 200:
|
23 |
+
# Print the JSON response containing the transcription
|
24 |
+
print(response.json())
|
25 |
+
|
26 |
+
# Extract the transcription from the JSON response
|
27 |
+
transcription = response.json().get('transcription', '')
|
28 |
+
print(f"\nTranscription: {transcription}")
|
29 |
+
|
30 |
+
else:
|
31 |
+
# Print the error if the request was not successful
|
32 |
+
print(f"Error: {response.status_code}, {response.text}")
|
33 |
+
|
34 |
+
except FileNotFoundError:
|
35 |
+
print(f"Error: The file {audio_file_path} was not found.")
|
36 |
+
except requests.RequestException as e:
|
37 |
+
print(f"Request error: {e}")
|