Spaces:
Build error
Build error
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
#os.system("pip install git+https://github.com/openai/whisper.git")
|
| 3 |
+
#os.system("pip install neon-tts-plugin-coqui==0.6.0")
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import whisper
|
| 6 |
+
import requests
|
| 7 |
+
import tempfile
|
| 8 |
+
#from neon_tts_plugin_coqui import CoquiTTS
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
import random
|
| 11 |
+
|
| 12 |
+
dataset = load_dataset("ysharma/short_jokes", split="train")
|
| 13 |
+
filtered_dataset = dataset.filter(
|
| 14 |
+
lambda x: (True not in [nsfw in x["Joke"].lower() for nsfw in ["warning", "fuck", "dead", "nsfw","69", "sex"]])
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Model 2: Sentence Transformer
|
| 19 |
+
API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/msmarco-distilbert-base-tas-b"
|
| 20 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
| 21 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 22 |
+
|
| 23 |
+
def query(payload):
|
| 24 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 25 |
+
return response.json()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Language common in both the multilingual models - English, Chinese, Spanish, and French etc
|
| 29 |
+
# Model 1: Whisper: Speech-to-text
|
| 30 |
+
#model = whisper.load_model("base")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
#Model 2: Text-to-Speech
|
| 34 |
+
#LANGUAGES = list(CoquiTTS.langs.keys())
|
| 35 |
+
#coquiTTS = CoquiTTS()
|
| 36 |
+
#Languages for Coqui are: ['en', 'es', 'fr', 'de', 'pl', 'uk', 'ro', 'hu', 'el', 'bg', 'nl', 'fi', 'sl', 'lv', 'ga']
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Driver function
|
| 40 |
+
def driver_fun(text) :
|
| 41 |
+
print("*********** Inside Driver ************")
|
| 42 |
+
#if (text == 'dummy') and (audio is not None) :
|
| 43 |
+
# print(f"Audio is {audio}")
|
| 44 |
+
# translation, lang = whisper_stt(audio)
|
| 45 |
+
#else:
|
| 46 |
+
# translation = text
|
| 47 |
+
|
| 48 |
+
random_val = random.randrange(0,231657)
|
| 49 |
+
if random_val < 226657:
|
| 50 |
+
lower_limit = random_val
|
| 51 |
+
upper_limit = random_val + 4000
|
| 52 |
+
else:
|
| 53 |
+
lower_limit = random_val - 4000
|
| 54 |
+
upper_limit = random_val
|
| 55 |
+
print(f"lower_limit : upper_limit = {lower_limit} : {upper_limit}")
|
| 56 |
+
dataset_subset = filtered_dataset['Joke'][lower_limit : upper_limit]
|
| 57 |
+
data = query({"inputs": {"source_sentence": text ,"sentences": dataset_subset} } ) #"That is a happy person"
|
| 58 |
+
if 'error' in data:
|
| 59 |
+
print(f"Error is : {data}")
|
| 60 |
+
return 'Error in model inference - Run Again Please', 'Error in model inference - Run Again Please', None
|
| 61 |
+
print(f"type(data) : {type(data)}")
|
| 62 |
+
#print(f"data : {data} ")
|
| 63 |
+
max_match_score = max(data)
|
| 64 |
+
indx_score = data.index(max_match_score)
|
| 65 |
+
joke = dataset_subset[indx_score]
|
| 66 |
+
print(f"Joke is : {joke}")
|
| 67 |
+
|
| 68 |
+
#speech = tts(joke, 'en')
|
| 69 |
+
return joke
|
| 70 |
+
|
| 71 |
+
demo = gr.Blocks()
|
| 72 |
+
with demo:
|
| 73 |
+
gr.Markdown("<h1><center>Text-to-Joke</center></h1>")
|
| 74 |
+
gr.Markdown(
|
| 75 |
+
"""<center>Enter a theme or a context for AI to find a joke for you on that.</center><br><center>If you see the message 'Error in model inference - Run Again Please', just press the button again every time!</center>
|
| 76 |
+
""")
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
#in_audio = gr.Audio(source="microphone", type="filepath", label='Record your voice command here in English -') #type='filepath'
|
| 80 |
+
in_text = gr.Textbox(label= 'Enter a theme or context for a joke')
|
| 81 |
+
b1 = gr.Button("Get a Joke")
|
| 82 |
+
|
| 83 |
+
with gr.Column():
|
| 84 |
+
#in_text = gr.Textbox(label='Or enter any text here..', value='dummy')
|
| 85 |
+
#out_audio = gr.Audio(label='Audio response form CoquiTTS')
|
| 86 |
+
out_generated_joke = gr.Textbox(label= 'Joke returned! ')
|
| 87 |
+
|
| 88 |
+
b1.click(driver_fun,inputs=[in_text], outputs=[out_generated_joke]) #out_translation_en, out_generated_text,out_generated_text_en,
|
| 89 |
+
with gr.Row():
|
| 90 |
+
gr.Markdown(
|
| 91 |
+
"""Built using [Sentence Transformers](https://huggingface.co/models?library=sentence-transformers&sort=downloads) and [**Gradio Block API**](https://gradio.app/docs/#blocks).<br><br>Few Caveats:<br>1. Please note that sometimes the joke might be NSFW. Although, I have tried putting in filters to not have that experience, but the filters seem non-exhaustive.<br>2. Sometimes the joke might not match your theme, please bear with the limited capabilities of free open-source ML prototypes.<br>3. Much like real life, sometimes the joke might just not land, haha!<br>4. Repeating this: If you see the message 'Error in model inference - Run Again Please', just press the button again every time!
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
+
demo.launch(enable_queue=True, debug=True)
|