Mawube commited on
Commit
1306b2e
·
unverified ·
1 Parent(s): 30c5114

Add audio generation

Browse files
Files changed (1) hide show
  1. utils/audio_generation.py +67 -0
utils/audio_generation.py CHANGED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import logging
3
+
4
+ class AudioGeneration:
5
+ """
6
+ Class to convert text to local language and back to audio
7
+ """
8
+ def __init__(self, prediction, confidence, language):
9
+ self.confidence = confidence
10
+ self.language = language
11
+ self.prediction = prediction
12
+ self.selected_languages = ['English', 'Ewe', 'Hausa','Akuapem', 'Asante']
13
+ self.ghanaian_language_translator_api = "https://hnmensah-ghanaian-language-translator.hf.space/api/predict"
14
+ self.template = self.generate_template()
15
+
16
+
17
+ def generate_template(self):
18
+ """
19
+ Generate the template for the audio file
20
+ """
21
+
22
+ # For non adulterated palm oil
23
+ if self.prediction == 0:
24
+ if self.confidence > 80.0 and self.confidence < 100.0:
25
+ return f"Your palm oil is good and the quality is very high"
26
+ elif self.confidence > 60.0 and self.confidence < 80.0:
27
+ return f"Your palm oil is good and the quality is high"
28
+ else:
29
+ return f"Your palm oil is good and the quality is low"
30
+
31
+ # For adulterated palm oil
32
+ else:
33
+ if self.confidence > 80.0 and self.confidence < 100.0:
34
+ return f"Your palm oil is bad and the confidence is very high"
35
+ elif self.confidence > 60.0 and self.confidence < 80.0:
36
+ return f"Your palm oil is bad and tthe confidence is high"
37
+ else:
38
+ return f"Your palm oil is bad and the confidence is very low"
39
+
40
+
41
+ def ghanaian_language_translator(self):
42
+ """
43
+ Covert the template to the local language
44
+ :return: translated text
45
+ """
46
+ logging.info("Translating text from english to local language")
47
+
48
+ # Translate template to local language
49
+ response = requests.post(
50
+ self.ghanaian_language_translator_api,
51
+ {
52
+ "data": [
53
+ "English",
54
+ self.selected_languages[self.language],
55
+ self.template
56
+ ]
57
+ }
58
+ )
59
+
60
+ # Parse the response
61
+ response = response.json()
62
+
63
+ # Extract transcribe text
64
+ translated_text = response['data'][0]
65
+ logging.debug(f"Translated text:{translated_text}")
66
+
67
+ return translated_text