File size: 5,727 Bytes
326094a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3990fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326094a
 
3990fcd
 
 
 
 
 
 
 
 
 
 
 
326094a
3990fcd
 
326094a
 
 
 
 
 
 
3990fcd
326094a
3990fcd
 
 
 
 
326094a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fabe773
326094a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fabe773
 
 
 
326094a
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import requests
import string
import os
import json
from pydub import AudioSegment
from pydub.utils import which

AudioSegment.converter = which("ffmpeg")

class AudioTranslation:
    def __init__(self):
        # Initialize any required variables or settings
        pass
    def convert_to_mp3(self, file_path):
        if not file_path.lower().endswith('.mp3'):
            audio = AudioSegment.from_file(file_path)
            file_path_without_ext = os.path.splitext(file_path[0]) # fix this line
            converted_file_path = f"{file_path_without_ext}.mp3"
            audio.export(converted_file_path, "mp3")
            return converted_file_path
        return file_path

    # def transcribe_audio(self, file_path):
    #     url = "https://stt.umuganda.digital/transcribe/"
    #     converted_file_path = self.convert_to_mp3(file_path)
    #     with open(converted_file_path, 'rb') as file:
    #         files = {'file': (file_path, file, 'audio/mpeg')}
    #         print('transcribing audio')
    #         try:
    #             response = requests.post(url, files=files)
    #             response.raise_for_status()
    #             transcription = response.json()
    #             # Remove punctuation
    #             translator = str.maketrans('', '', string.punctuation)
    #             cleaned_text = transcription['text'].translate(translator)
    #             print('cleaned text')
    #             print(cleaned_text)
    #             return cleaned_text
    #         except requests.exceptions.HTTPError as err:
    #             print(f"HTTP error occurred: {err}")
    #         except Exception as err:
    #             print(f"An error occurred: {err}")
    #     return None
    def transcribe_audio(self, file_path):
        url = "https://stt.umuganda.digital/transcribe/"
        
        # Check if the file is an MP3; if not, convert it
        if not file_path.lower().endswith('.mp3'):
            print(f"Converting file to MP3: {file_path}")
            converted_file_path = self.convert_to_mp3(file_path)
        else:
            converted_file_path = file_path
        
        if not os.path.exists(converted_file_path) or os.path.getsize(converted_file_path) == 0:
            print(f"File does not exist or is empty: {converted_file_path}")
            return None

        with open(converted_file_path, 'rb') as file:
            files = {'file': (os.path.basename(converted_file_path), file, 'audio/mpeg')}
            print('Transcribing audio...')
            try:
                response = requests.post(url, files=files)
                response.raise_for_status()
                transcription = response.json()
                # Remove punctuation
                translator = str.maketrans('', '', string.punctuation)
                cleaned_text = transcription['text'].translate(translator)
                print('Cleaned text:', cleaned_text)
                return cleaned_text
            except requests.exceptions.RequestException as e:
                print(f"Request error occurred: {e}")
                if hasattr(e, 'response') and e.response:
                    print(f"Server response: {e.response.text}")
                return None

    def get_translation(self, src, tgt, text):
        url = f"https://nmt-api.umuganda.digital/api/v1/translate/?src={src}&tgt={tgt}&text={text}"
        try:
            response = requests.get(url)
            response.raise_for_status()
            return response.json()
        except requests.exceptions.HTTPError as err:
            print(f"HTTP error occurred: {err}")
        except Exception as err:
            print(f"An error occurred: {err}")

        return None

    def translate_sentence(self, src, tgt, alt, use_multi, text):
        url = "https://nmt-api.umuganda.digital/api/v1/translate/"
        headers = {
            'accept': 'application/json',
            'Content-Type': 'application/json'
        }
        data = {
            "src": src,
            "tgt": tgt,
            "alt": alt,
            "use_multi": use_multi,
            "text": text
        }

        try:
            response = requests.post(url, headers=headers, data=json.dumps(data))
            response.raise_for_status()
            print('translation sentence')
            return response.json()
        except requests.exceptions.HTTPError as err:
            print(f"HTTP error occurred: {err}")
        except Exception as err:
            print(f"An error occurred: {err}")
        print(response.json())
        return None

    def post_batch_translation(self, batch_data):
        url = "https://nmt-api.umuganda.digital/api/v1/translate/batch"
        headers = {'Content-Type': 'application/json'}

        try:
            response = requests.post(url, json=batch_data, headers=headers)
            response.raise_for_status()
            return response.json()
        except requests.exceptions.HTTPError as err:
            print(f"HTTP error occurred: {err}")
        except Exception as err:
            print(f"An error occurred: {err}")

        return None

# Example usage
# audio_translator = AudioTranslation()
# transcription = audio_translator.transcribe_audio("voice_test_1.mp3")
# translation_result = audio_translator.translate_sentence("rw", "en","MULTI-rw-en","", transcription)
# print(translation_result)
'''
alternative models:
    https://huggingface.co/facebook/nllb-200-3.3B
    https://huggingface.co/facebook/mms-1b-all
    -----
    TODO:
    -function ogg to mp3
    - post_translation : preprocess punctuation
    -function batch transcription
    - merge voice-data to dataset
    - run sentiment analysis prediction -->upload voice (streamlit)
    
'''