hra's picture
Update app.py
663188e
import json
import requests
import gradio as gr
import random
import time
import os
import datetime
from datetime import datetime
print('for update')
API_TOKEN = os.getenv("API_TOKEN")
HRA_TOKEN=os.getenv("HRA_TOKEN")
from huggingface_hub import InferenceApi
inference = InferenceApi("bigscience/bloom",token=API_TOKEN)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
url_decodemprompts='https://us-central1-createinsightsproject.cloudfunctions.net/gethrahfprompts'
data={"prompt_type":'song_recommend1',"hra_token":HRA_TOKEN}
try:
r = requests.post(url_decodemprompts, data=json.dumps(data), headers=headers)
except requests.exceptions.ReadTimeout as e:
print(e)
#print(r.content)
prompt=str(r.content, 'UTF-8')
print(prompt)
def infer(prompt,
max_length = 250,
top_k = 0,
num_beams = 0,
no_repeat_ngram_size = 2,
top_p = 0.9,
seed=42,
temperature=0.7,
greedy_decoding = False,
return_full_text = True):
print(seed)
top_k = None if top_k == 0 else top_k
do_sample = False if num_beams > 0 else not greedy_decoding
num_beams = None if (greedy_decoding or num_beams == 0) else num_beams
no_repeat_ngram_size = None if num_beams is None else no_repeat_ngram_size
top_p = None if num_beams else top_p
early_stopping = None if num_beams is None else num_beams > 0
params = {
"max_new_tokens": max_length,
"top_k": top_k,
"top_p": top_p,
"temperature": temperature,
"do_sample": do_sample,
"seed": seed,
"early_stopping":early_stopping,
"no_repeat_ngram_size":no_repeat_ngram_size,
"num_beams":num_beams,
"return_full_text":return_full_text
}
s = time.time()
response = inference(prompt, params=params)
#print(response)
proc_time = time.time()-s
#print(f"Processing time was {proc_time} seconds")
return response
def getsongrecco(text_inp1,text_inp2):
print(text_inp1,' by ',text_inp2)
print(datetime.today().strftime("%d-%m-%Y"))
text = prompt+"\nInput:"+text_inp1+' by '+text_inp2 + "\nOutput:"
resp = infer(text,seed=random.randint(0,100))
generated_text=resp[0]['generated_text']
result = generated_text.replace(text,'').strip()
result = result.replace("Output:","")
parts = result.split("###")
topic = parts[0].strip()
topic="\n".join(topic.split('\n'))
print(topic)
return(topic)
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("<h1><center>Music Recommendation</center></h1>")
gr.Markdown(
"""Get Music Recommendations from Large Language Models. Enter a song, band/artist and get recommendations. Prompt Engineered AI model bigscience/bloom."""
)
with gr.Row():
with gr.Column():
textbox1 = gr.Textbox(placeholder="Enter song name...", lines=1,label='Song')
textbox2 = gr.Textbox(placeholder="Enter band/ artist name...", lines=1,label='Band/ Artist')
with gr.Column():
btn = gr.Button("Recommend")
output1 = gr.Textbox(lines=2,label='Music Recommendations')
btn.click(getsongrecco,inputs=[textbox1,textbox2], outputs=[output1])
examples = gr.Examples(examples=[["It's My Life","Bon Jovi"],["Wavin' Flag","K'naan"],['Dreamer','Ozzy Osbourne'],['Deutschland','Rammstein']],
inputs=[textbox1,textbox2])
demo.launch()