File size: 1,617 Bytes
46e6114
 
01f65eb
46e6114
 
 
01f65eb
 
 
ce6c474
01f65eb
 
 
46e6114
 
 
 
 
 
 
 
a48d452
46e6114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af77b0c
46e6114
 
760ecf9
46e6114
760ecf9
46e6114
760ecf9
46e6114
01f65eb
46e6114
 
 
 
 
 
 
01f65eb
 
 
 
 
 
 
 
 
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
import os
import csv
import gradio
from gradio import utils
import huggingface_hub
from pathlib import Path
from src.models.bert import BERTClassifier
from src.utils.utilities import Utility

model = BERTClassifier(model_name='jeevavijay10/nlp-goemotions-bert')

classes = Utility().read_emotion_list()

hf_token = os.getenv("HF_TOKEN")

dataset_dir = "logs"

headers = ["input", "output"]


repo = huggingface_hub.Repository(
    local_dir=dataset_dir, clone_from="https://huggingface.co/datasets/jeevavijay10/senti-pred-gradio", use_auth_token=hf_token
)

def log_record(input, output):
    repo.git_pull(lfs=True)
    
    log_file = Path(dataset_dir) / "log.csv"
    
    is_new = not Path(log_file).exists()    
    
    with open(log_file, "a", newline="", encoding="utf-8") as csvfile:
        writer = csv.writer(csvfile)

    if is_new:
        writer.writerow(utils.sanitize_list_for_csv(headers))

    writer.writerow(utils.sanitize_list_for_csv([input, output]))
    
    with open(log_file, "r", encoding="utf-8") as csvfile:
        line_count = len([None for _ in csv.reader(csvfile)]) - 1
    
    repo.push_to_hub(commit_message=f"Logged sample #{line_count}")
    

def predict(sentence):
    
    print(sentence)
    
    predictions = model.evaluate([sentence])
    
    print(f"Predictions: {predictions}")
    
    output = classes[predictions[0]]
    
    log_record(sentence, output)
    
    return output


gradio.Interface(
    fn=predict,
    inputs="text",
    outputs="text",
    allow_flagging='auto',
    flagging_dir='logs',
    flagging_callback=gradio.SimpleCSVLogger(),
).launch()