File size: 2,996 Bytes
1306b2e
 
a45f5c1
216ee15
1306b2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216ee15
 
a45f5c1
 
 
 
 
 
 
216ee15
a45f5c1
 
 
 
 
 
216ee15
a45f5c1
216ee15
a45f5c1
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
import requests
import logging
# from gradio_client import Client


class AudioGeneration:
    """
    Class to convert text to local language and back to audio
    """
    def __init__(self, prediction, confidence, language):
        self.confidence = confidence
        self.language = language
        self.prediction = prediction
        self.selected_languages = ['English', 'Ewe', 'Hausa','Akuapem', 'Asante']
        self.ghanaian_language_translator_api = "https://hnmensah-ghanaian-language-translator.hf.space/api/predict"
        self.template = self.generate_template()


    def generate_template(self):
        """
        Generate the template for the audio file
        """

        # For non adulterated palm oil
        if self.prediction == 0: 
            if self.confidence > 80.0 and self.confidence < 100.0:
                return f"Your palm oil is good and the quality is very high"
            elif self.confidence > 60.0 and self.confidence < 80.0:
                return f"Your palm oil is good and the quality is high"
            else:
                return f"Your palm oil is good and the quality is low"
            
        # For adulterated palm oil
        else:
            if self.confidence > 80.0 and self.confidence < 100.0:
                return f"Your palm oil is bad and the confidence is very high"
            elif self.confidence > 60.0 and self.confidence < 80.0:
                return f"Your palm oil is bad and tthe confidence is high"
            else:
                return f"Your palm oil is bad and the confidence is very low"


    def ghanaian_language_translator(self):
        """
        Covert the template to the local language 
        :return: translated text
        """
        logging.info("Translating text from english to local language")

        # Translate template to local language
        response = requests.post(
            self.ghanaian_language_translator_api,
            {
                "data": [
                    "English",
                    self.selected_languages[self.language],
                    self.template
                ]
            }
        )
       
        # Parse the response
        response = response.json()

        # Extract transcribe text
        translated_text = response['data'][0]
        logging.debug(f"Translated text:{translated_text}")

        return translated_text
    
    # def text_to_audio(self,text):
    #     """
    #     Convert the translated text to audio
    #     :param text: translated text
    #     :return: audio file
    #     """
    #     logging.info("Converting text to audio")

    #     client = Client("https://softwarearoma-ghanalanguageaudiosynthesizer.hf.space/--replicas/0t7ah/")
    #     result = client.predict(
    #             self.selected_languages[self.language],
    #             text,	
    #             api_name="/predict"
    #     )
        
    #     logging.debug(f"Audio file: {result[0]}")

    #     return result[0]