import os #os.system("pip install git+https://github.com/openai/whisper.git") #os.system("pip install neon-tts-plugin-coqui==0.6.0") import gradio as gr #import whisper import requests import tempfile #from neon_tts_plugin_coqui import CoquiTTS from datasets import load_dataset import random dataset = load_dataset("ysharma/short_jokes", split="train") filtered_dataset = dataset.filter( lambda x: (True not in [nsfw in x["Joke"].lower() for nsfw in ["warning","porn", "blow", "fuck", "dead", "nsfw","69", "sex", "prostitute","prostitutes", "pedophiles", "pedophile"]]) ) # Model 2: Sentence Transformer API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/msmarco-distilbert-base-tas-b" HF_TOKEN = os.environ["HF_TOKEN"] headers = {"Authorization": f"Bearer {HF_TOKEN}"} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.json() # Language common in both the multilingual models - English, Chinese, Spanish, and French etc # Model 1: Whisper: Speech-to-text #model = whisper.load_model("base") #Model 2: Text-to-Speech #LANGUAGES = list(CoquiTTS.langs.keys()) #coquiTTS = CoquiTTS() #Languages for Coqui are: ['en', 'es', 'fr', 'de', 'pl', 'uk', 'ro', 'hu', 'el', 'bg', 'nl', 'fi', 'sl', 'lv', 'ga'] # Driver function def driver_fun(text) : print("*********** Inside Driver ************") #if (text == 'dummy') and (audio is not None) : # print(f"Audio is {audio}") # translation, lang = whisper_stt(audio) #else: # translation = text random_val = random.randrange(0,231657) if random_val < 226657: lower_limit = random_val upper_limit = random_val + 4000 else: lower_limit = random_val - 4000 upper_limit = random_val print(f"lower_limit : upper_limit = {lower_limit} : {upper_limit}") dataset_subset = filtered_dataset['Joke'][lower_limit : upper_limit] data = query({"inputs": {"source_sentence": text ,"sentences": dataset_subset} } ) #"That is a happy person" if 'error' in data: print(f"Error is : {data}") return 'Error in model inference - Run Again Please', 'Error in model inference - Run Again Please', None print(f"type(data) : {type(data)}") #print(f"data : {data} ") max_match_score = max(data) indx_score = data.index(max_match_score) joke = dataset_subset[indx_score] print(f"Joke is : {joke}") #speech = tts(joke, 'en') return joke demo = gr.Blocks() with demo: gr.Markdown("