File size: 1,537 Bytes
7a4c140
15037bb
 
ef2614f
c9da813
7a4c140
414cbcb
9514195
 
 
 
414cbcb
f61df04
 
 
414cbcb
f61df04
 
 
 
94a4ce2
 
ef2614f
94a4ce2
414cbcb
9514195
 
 
 
414cbcb
9514195
158086e
 
 
414cbcb
 
 
9514195
 
 
 
 
 
 
414cbcb
94a4ce2
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
# Utilities
from src.modello import Modello
from src.dataset import LoadDataset
from sklearn.metrics import accuracy_score
import gradio as gr

# Dichiaro le variabili e costanti che mi serviranno in seguito
model = None
dataset = None
X = None
y = None
y_pred = None
valori = ['negative', 'neutral', 'positive']

def predict_tweet(tweet,esito_atteso) :
    # Aggiunge gli input (tweet ed esito_atteso) e l'esito predetto al dataset; restituisce l'esito predetto e la nuova accuracy calcolata sul dataset aggiornato
    X.append(tweet)
    y.append(esito_atteso)
    y_new = model.predict(tweet)[0]
    y_pred.append(y_new)
    acc_new = f"{accuracy_score(y, y_pred)}"
    return y_new,acc_new

if __name__ == "__main__":
    # Inizializza gli oggetti
    model = Modello()
    dataset = LoadDataset()
    X = dataset.X
    y = dataset.y
    y_pred = model.predict(X)

    # Stampa accuracy iniziale
    print(f"Accuracy iniziale: {accuracy_score(y, y_pred)}")

    # Definisce l'interfaccia grafica di Gradio
    # Prende in input un testo (tweet) e il sentiment predetto (quest'ultimo scelto da un menu a tendina)
    # Restituisce l'esito predetto e la nuova accuracy calcolata sul dataset aggiornato
    demo = gr.Interface(
        fn=predict_tweet,
        inputs=[gr.Textbox(label="Tweet"),gr.Dropdown(choices=valori,label="Esito atteso")],
        outputs=[gr.Textbox(label="Previsione modello"),gr.Textbox(label="Ricalcolo accuracy")],
        flagging_mode = 'never'
    )

    # Lancia l'interfaccia grafica
    demo.launch()