File size: 2,789 Bytes
448b2e6
1acbc7e
 
448b2e6
4ca8af8
2f1c2d1
4ca8af8
1acbc7e
448b2e6
4ca8af8
 
 
 
 
 
 
 
 
 
 
 
 
 
1acbc7e
4ca8af8
 
 
1acbc7e
4ca8af8
 
 
 
1acbc7e
 
 
 
4ca8af8
 
 
 
1acbc7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ca8af8
 
 
 
 
 
 
 
b0d9fab
448b2e6
4ca8af8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1acbc7e
4ca8af8
 
 
b0d9fab
4ca8af8
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import gradio as gr
import random
import pandas as pd

opo = pd.read_csv('oportunidades_results.csv', lineterminator='\n')
# opo = opo.iloc[np.where(opo['opo_brazil']=='Y')]
simulation = pd.read_csv('simulation2.csv')
userID = max(simulation['userID']) + 1

def build_display_text(opo_n):
    
    title = opo.loc[opo_n]['opo_titulo']
    link = opo.loc[opo_n]['link']
    summary = opo.loc[opo_n]['facebook-bart-large-cnn_results']

    display_text = f"**{title}**\n\nURL:\n{link}\n\nSUMMARY:\n{summary}"

    return display_text

opo_n_one = random.randrange(len(opo))
opo_n_two = random.randrange(len(opo))
opo_n_three = random.randrange(len(opo))
opo_n_four = random.randrange(len(opo))

evaluated = []

def predict_next(option, nota):
    global userID
    global opo_n_one
    global opo_n_two
    global opo_n_three
    global opo_n_four
    global evaluated
    global opo
    global simulation

    selected = [opo_n_one, opo_n_two, opo_n_three, opo_n_four][int(option)-1]

    simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)
    evaluated.append(selected)
    
    from surprise import Reader
    reader = Reader(rating_scale=(1, 5))

    from surprise import Dataset
    data = Dataset.load_from_df(simulation[['userID', 'itemID', 'rating']], reader)
    trainset = data.build_full_trainset()

    from surprise import SVDpp
    svdpp = SVDpp()
    svdpp.fit(trainset)

    items = list()
    est = list()

    for i in range(len(opo)):
        if i not in evaluated:
            items.append(i)
            est.append(svdpp.predict(userID, i).est)

    opo_n_one = items[est.index(sorted(est)[-1])]
    opo_n_two = items[est.index(sorted(est)[-2])]
    opo_n_three = items[est.index(sorted(est)[-3])]
    opo_n_four = items[est.index(sorted(est)[-4])]

    return build_display_text(opo_n_one), build_display_text(opo_n_two), build_display_text(opo_n_three), build_display_text(opo_n_four)


with gr.Blocks() as demo:
    with gr.Row():
        one_opo = gr.Textbox(build_display_text(opo_n_one), label='Oportunidade 1')
        two_opo = gr.Textbox(build_display_text(opo_n_two), label='Oportunidade 2')

    with gr.Row():
        three_opo = gr.Textbox(build_display_text(opo_n_three), label='Oportunidade 3')
        four_opo = gr.Textbox(build_display_text(opo_n_four), label='Oportunidade 4')

    with gr.Row():
        option = gr.Radio(['1', '2', '3', '4'], label='Opção', value = '1')

    with gr.Row():
        nota = gr.Slider(1,5,step=1,label="Nota 1")

    with gr.Row():
        confirm = gr.Button("Confirmar")

        confirm.click(fn=predict_next,
               inputs=[option, nota],
               outputs=[one_opo, two_opo, three_opo, four_opo])

if __name__ == "__main__":
    demo.launch()