priyasaravana commited on
Commit
3c9a94a
·
1 Parent(s): 52d72aa

Upload gradio_app.py

Browse files
Files changed (1) hide show
  1. gradio_app.py +75 -0
gradio_app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import csv
4
+ import datetime
5
+ import gradio
6
+ import schedule
7
+ from gradio import utils
8
+ import huggingface_hub
9
+ from pathlib import Path
10
+ from src.models.bert import BERTClassifier
11
+ from src.utils.utilities import Utility
12
+
13
+ model = BERTClassifier(model_name='jeevavijay10/nlp-goemotions-bert')
14
+
15
+ classes = Utility().read_emotion_list()
16
+
17
+ hf_token = os.getenv("HF_TOKEN")
18
+
19
+ dataset_dir = "logs"
20
+
21
+ headers = ["input", "output", "timestamp", "elapsed"]
22
+
23
+
24
+ repo = huggingface_hub.Repository(
25
+ local_dir=dataset_dir,
26
+ clone_from="https://huggingface.co/datasets/jeevavijay10/senti-pred-gradio",
27
+ token=hf_token,
28
+ )
29
+ repo.git_pull(lfs=True)
30
+
31
+ def log_record(vals):
32
+ log_file = Path(dataset_dir) / "data.csv"
33
+ is_new = not Path(log_file).exists()
34
+ with open(log_file, "a", newline="", encoding="utf-8") as csvfile:
35
+ writer = csv.writer(csvfile)
36
+ if is_new:
37
+ writer.writerow(utils.sanitize_list_for_csv(headers))
38
+ writer.writerow(utils.sanitize_list_for_csv(vals))
39
+ schedule.run_pending()
40
+ print(f"Last Sync: {job.last_run}")
41
+
42
+ def predict(sentence):
43
+
44
+ timestamp = datetime.datetime.now().isoformat()
45
+ start_time = time.time()
46
+ predictions = model.evaluate([sentence])
47
+ elapsed_time = time.time() - start_time
48
+
49
+ output = classes[predictions[0]]
50
+
51
+ print(f"Sentence: {sentence} \nPrediction: {predictions[0]} - {output}")
52
+ log_record([sentence, output, timestamp, str(elapsed_time)])
53
+
54
+ return output
55
+
56
+
57
+ def sync_logs():
58
+ print(f"Repo Clean: {repo.is_repo_clean()}")
59
+ if not repo.is_repo_clean():
60
+ repo.git_add()
61
+ repo.git_commit()
62
+ repo.git_pull(lfs=True)
63
+ result = repo.git_push()
64
+ # result = repo.push_to_hub()
65
+ print(result)
66
+
67
+ job = schedule.every(5).minutes.do(sync_logs)
68
+ print("Scheduler engaged")
69
+
70
+ gradio.Interface(
71
+ fn=predict,
72
+ inputs="text",
73
+ outputs="text",
74
+ allow_flagging='never'
75
+ ).launch()