CoolFace
Apppublic

Henok21/sentiment_analysis_covid19_comment

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py68 linesDownload Raw Back to root
1# Importing module2from transformers import AutoModelForSequenceClassification3from transformers import TFAutoModelForSequenceClassification4from transformers import AutoModel, AutoTokenizer5from transformers import AutoTokenizer , pipeline , AutoConfig6import numpy as np7 8import gradio as gr9from scipy.special import softmax10 11# HuggingFace path where the fine tuned model is placed12model_path = "Henok21/test_trainer"13 14# Loading the model15model = AutoModelForSequenceClassification.from_pretrained(model_path)16 17# Loading config file18config = AutoConfig.from_pretrained(model_path)19 20# Loading tokenizer21tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')22 23# Using pipeline24calssifier = pipeline("sentiment-analysis" , model , tokenizer = tokenizer)25 26# Preprocessor Function27def preprocess(text):28    new_text = []29    for t in text.split(" "):30        t = '@user' if t.startswith('@') and len(t) > 1 else t31        t = 'http' if t.startswith('http') else t32        new_text.append(t)33    return " ".join(new_text)34 35# Adjusting config36config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}37 38 39# Function used for gradio app40def sentiment_analysis(text):41    # Your code to get the scores for each class42    scores = output[0][0].detach().numpy()43    scores = softmax(scores)44 45    # Convert the numpy array into a list46    scores = scores.tolist()47 48    # Print labels and scores49    ranking = np.argsort(scores)50    ranking = ranking[::-1]51    for i in range(len(scores)):52        l = config.id2label[ranking[i]]53 54        s = scores[ranking[i]]55 56        a = f"{i+1}) {l} {np.round(float(s), 4)}"57 58        # Convert the numpy float32 object into a float59        d[l] = float(s)60 61    # Return the dictionary as the response content62    return d63 64# Create your interface65demo = gr.Interface(fn=sentiment_analysis, inputs="text", outputs="label")66 67# Launch your interface68demo.launch(debug = True)