CoolFace
Apppublic

bright1/My-Second-Docker-App

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
utils.py49 linesDownload Raw Back to root
1import numpy as np2import pandas as pd3from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification4from scipy.special import softmax5import os6 7 8 9def check_csv(csv_file, data):10    if os.path.isfile(csv_file):11        data.to_csv(csv_file, mode='a', header=False, index=False, encoding='utf-8')12    else:13        history = data.copy()14        history.to_csv(csv_file, index=False)15 16#Preprocess text17def preprocess(text):18    new_text = []19    for t in text.split(" "):20        t = "@user" if t.startswith("@") and len(t) > 1 else t21        t = "http" if t.startswith("http") else t22        print(t)23        new_text.append(t)24    print(new_text)25 26    return " ".join(new_text)27 28#Process the input and return prediction29def run_sentiment_analysis(text, tokenizer, model):30    # save_text =  {'tweet': text}31    encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models32    output = model(**encoded_input)33    scores_ = output[0][0].detach().numpy()34    scores_ = softmax(scores_)35 36    # Format output dict of scores37    labels = ["Negative", "Neutral", "Positive"]38    scores = {l:float(s) for (l,s) in zip(labels, scores_) }39    # save_text.update(scores)40    # user_data = {key: [value] for key,value in save_text.items()}41    # data = pd.DataFrame(user_data,)42    # check_csv('history.csv', data)43    # hist_df = pd.read_csv('history.csv')44    return scores45 46 47    48 49