amirhosseinkarami's picture
Simple Recommender complete
e12639d
import gradio
import pandas as pd
import concurrent.futures
from App.tfidfrecommender import TfidfRecommender
import gradio as gr
desc = pd.read_csv('App/data/descriptions.csv')
rec = TfidfRecommender(desc, 'id', 'description' , "none")
def initialize_and_tokenize(tokenizer):
print("tok")
rec.tokenization_method = tokenizer
rec.tokenize_text()
names = []
def recommend (movies, tok) :
rec.tokenization_method = tok
tf, vecs = rec.tokenize_text()
rec.fit(tf, vecs)
print("rec")
pool = concurrent.futures.ThreadPoolExecutor(max_workers=10)
futures = [pool.submit(rec.recommend_k_items, movie, 5) for movie in movies]
idss = []
print("after submit")
for i in range(len(futures)):
print("res")
idss.append(futures[i].result())
print("shutdown")
pool.shutdown(wait=True)
ids = [id for ids in idss for id in ids]
ids = list(set(ids))
names = desc[desc['id'].isin(ids)]['title'].to_list()
return ', '.join(names)
def recom(movies, tok):
rec.tokenization_method = tok
tf, vecs = rec.tokenize_text()
rec.fit(tf, vecs)
print(movies[0])
ids = rec.recommend_k_items(movies[0], 5)
print("reccc")
# ids = list(set(ids))
names = desc[desc['id'].isin(ids)]['title'].to_list()
return ', '.join(names)
demo = gr.Interface(fn=recom,
inputs=[gr.Dropdown(choices = list(desc['title'][:20]), multiselect=True, max_choices=3, label="Movies"),
gr.Radio(["bert", "scibert", "nltk" , "none"], value="none", label="Tokenization and text preprocess")],
outputs=gr.Textbox(label="Recommended"))
demo.launch()
# ===========================
# with gr.Blocks() as demo:
# gr.Markdown("Start typing below and then click **Run** to see the output.")
# with gr.Row():
# radio = gr.Radio(["bert", "scibert", "nltk" , "none"], value="none",
# label="Tokenization and text preprocess")
# btn = gr.Button("Tokenize and Preprocess")
# btn.click(fn=initialize_and_tokenize, inputs=radio)
# # demo.launch()
# # with gr.Blocks() as demo2:
# gr.Markdown("Choose 3 movies")
# with gr.Row():
# dropdown = gr.Dropdown(choices = list(desc['title']), multiselect=True, max_choices=3,
# label="Movies")
# box = gr.Textbox(lines=3, label="recs")
# btn2 = gr.Button("Recommend")
# btn2.click(fn=recommend, inputs=dropdown,outputs=[])
# gr.Markdown("rec{}".format(len(names)))
# demo.launch()
# ==========================
# with gr.Blocks() as demo :
# gr.Markdown("Start typing below and then click **Run** to see the output.")
# with gr.Row():
# radio = gr.Radio(["bert", "scibert", "nltk" , "none"], value="none",
# label="Tokenization and text preprocess")
# btn = gr.Button("Tokenize and Preprocess")
# btn.click(fn=initialize_and_tokenize, inputs=radio, outputs=[])
# demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label")