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]