Spaces:
Runtime error
Runtime error
| import os | |
| import time | |
| import csv | |
| import datetime | |
| import gradio | |
| import schedule | |
| 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", "timestamp", "elapsed"] | |
| repo = huggingface_hub.Repository( | |
| local_dir=dataset_dir, | |
| clone_from="https://huggingface.co/datasets/jeevavijay10/senti-pred-gradio", | |
| token=hf_token, | |
| ) | |
| repo.git_pull(lfs=True) | |
| def log_record(vals): | |
| log_file = Path(dataset_dir) / "data.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(vals)) | |
| # with open(log_file, "r", encoding="utf-8") as csvfile: | |
| # line_count = len([None for _ in csv.reader(csvfile)]) - 1 | |
| def predict(sentence): | |
| timestamp = datetime.datetime.now().isoformat() | |
| start_time = time.time() | |
| predictions = model.evaluate([sentence]) | |
| elapsed_time = time.time() - start_time | |
| output = classes[predictions[0]] | |
| print(f"Sentence: {sentence} \nPrediction: {predictions[0]} - {output}") | |
| log_record([sentence, output, timestamp, str(elapsed_time)]) | |
| return output | |
| gradio.Interface( | |
| fn=predict, | |
| inputs="text", | |
| outputs="text", | |
| allow_flagging='never' | |
| ).launch() | |
| def sync_logs(): | |
| if not repo.is_repo_clean(): | |
| repo.git_add() | |
| repo.git_commit() | |
| # repo.git_push() | |
| repo.push_to_hub() | |
| schedule.every(2).minutes.do(sync_logs) | |