Spaces:
Sleeping
Sleeping
SammyGasana
commited on
Commit
•
3990fcd
1
Parent(s):
fabe773
Update audio_predictions.py
Browse filesupdated audio transcription code
- audio_predictions.py +41 -10
audio_predictions.py
CHANGED
@@ -20,12 +20,44 @@ class AudioTranslation:
|
|
20 |
return converted_file_path
|
21 |
return file_path
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def transcribe_audio(self, file_path):
|
24 |
url = "https://stt.umuganda.digital/transcribe/"
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
with open(converted_file_path, 'rb') as file:
|
27 |
-
files = {'file': (
|
28 |
-
print('
|
29 |
try:
|
30 |
response = requests.post(url, files=files)
|
31 |
response.raise_for_status()
|
@@ -33,14 +65,13 @@ class AudioTranslation:
|
|
33 |
# Remove punctuation
|
34 |
translator = str.maketrans('', '', string.punctuation)
|
35 |
cleaned_text = transcription['text'].translate(translator)
|
36 |
-
print('
|
37 |
-
print(cleaned_text)
|
38 |
return cleaned_text
|
39 |
-
except requests.exceptions.
|
40 |
-
print(f"
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
def get_translation(self, src, tgt, text):
|
46 |
url = f"https://nmt-api.umuganda.digital/api/v1/translate/?src={src}&tgt={tgt}&text={text}"
|
|
|
20 |
return converted_file_path
|
21 |
return file_path
|
22 |
|
23 |
+
# def transcribe_audio(self, file_path):
|
24 |
+
# url = "https://stt.umuganda.digital/transcribe/"
|
25 |
+
# converted_file_path = self.convert_to_mp3(file_path)
|
26 |
+
# with open(converted_file_path, 'rb') as file:
|
27 |
+
# files = {'file': (file_path, file, 'audio/mpeg')}
|
28 |
+
# print('transcribing audio')
|
29 |
+
# try:
|
30 |
+
# response = requests.post(url, files=files)
|
31 |
+
# response.raise_for_status()
|
32 |
+
# transcription = response.json()
|
33 |
+
# # Remove punctuation
|
34 |
+
# translator = str.maketrans('', '', string.punctuation)
|
35 |
+
# cleaned_text = transcription['text'].translate(translator)
|
36 |
+
# print('cleaned text')
|
37 |
+
# print(cleaned_text)
|
38 |
+
# return cleaned_text
|
39 |
+
# except requests.exceptions.HTTPError as err:
|
40 |
+
# print(f"HTTP error occurred: {err}")
|
41 |
+
# except Exception as err:
|
42 |
+
# print(f"An error occurred: {err}")
|
43 |
+
# return None
|
44 |
def transcribe_audio(self, file_path):
|
45 |
url = "https://stt.umuganda.digital/transcribe/"
|
46 |
+
|
47 |
+
# Check if the file is an MP3; if not, convert it
|
48 |
+
if not file_path.lower().endswith('.mp3'):
|
49 |
+
print(f"Converting file to MP3: {file_path}")
|
50 |
+
converted_file_path = self.convert_to_mp3(file_path)
|
51 |
+
else:
|
52 |
+
converted_file_path = file_path
|
53 |
+
|
54 |
+
if not os.path.exists(converted_file_path) or os.path.getsize(converted_file_path) == 0:
|
55 |
+
print(f"File does not exist or is empty: {converted_file_path}")
|
56 |
+
return None
|
57 |
+
|
58 |
with open(converted_file_path, 'rb') as file:
|
59 |
+
files = {'file': (os.path.basename(converted_file_path), file, 'audio/mpeg')}
|
60 |
+
print('Transcribing audio...')
|
61 |
try:
|
62 |
response = requests.post(url, files=files)
|
63 |
response.raise_for_status()
|
|
|
65 |
# Remove punctuation
|
66 |
translator = str.maketrans('', '', string.punctuation)
|
67 |
cleaned_text = transcription['text'].translate(translator)
|
68 |
+
print('Cleaned text:', cleaned_text)
|
|
|
69 |
return cleaned_text
|
70 |
+
except requests.exceptions.RequestException as e:
|
71 |
+
print(f"Request error occurred: {e}")
|
72 |
+
if hasattr(e, 'response') and e.response:
|
73 |
+
print(f"Server response: {e.response.text}")
|
74 |
+
return None
|
75 |
|
76 |
def get_translation(self, src, tgt, text):
|
77 |
url = f"https://nmt-api.umuganda.digital/api/v1/translate/?src={src}&tgt={tgt}&text={text}"
|