import gradio as gr from fastai.tabular.all import * import pickle import pandas as pd import torch learn = load_learner('learn.pkl') def recommendation(user_id, book_1, book_2, book_3): data = {'user_id': [user_id, user_id, user_id], 'name': [book_1, book_2, book_3]} df = pd.DataFrame(data=data) testdl = learn.dls.test_dl(df) preds = learn.get_preds(dl=testdl) preds rec = 0 idx = -1 for p in range(len(preds[0])): if float(preds[0][p]) > rec: idx = p rec = float(preds[0][p]) return f"O livro recomendado é {books[idx]}. A avaliação estimada é de {rec}" books = ['To Kill a Mockingbird', "Harry Potter and the Sorcerer's Stone (Harry Potter (Paperback))", 'The Da Vinci Code', 'Harry Potter and the Goblet of Fire (Book 4)', 'Silence of the Lambs', 'The Fellowship of the Ring (The Lord of the Rings, Part 1)', 'The Little Prince', 'Jurassic Park', 'A Time to Kill' ] gr.Interface( fn=recommendation, title="Sistema de Recomendação para livros", allow_flagging="never", share=True, inputs=[ gr.inputs.Number(default=777777, label="Id do usuário"), gr.Dropdown(books, label="Primeira opção de livro"), gr.Dropdown(books, label="Segunda opção de livro"), gr.Dropdown(books, label="Terceira opção de livro"), ], outputs="text").launch()