Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import pickle
|
| 5 |
-
import transformers
|
| 6 |
-
from transformers import AutoTokenizer
|
| 7 |
-
from transformers import AutoConfig
|
| 8 |
-
from transformers import AutoModelForSequenceClassification
|
| 9 |
-
from transformers import TFAutoModelForSequenceClassification
|
| 10 |
-
from transformers import pipeline
|
| 11 |
-
from scipy.special import softmax
|
| 12 |
-
|
| 13 |
-
# Requirements
|
| 14 |
-
model_path ="HOLYBOY/Sentiment_Analysis"
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 16 |
-
config = AutoConfig.from_pretrained(model_path)
|
| 17 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 18 |
-
|
| 19 |
-
# Preprocess text (username and link placeholders)
|
| 20 |
-
def preprocess(text):
|
| 21 |
-
new_text = []
|
| 22 |
-
for t in text.split(" "):
|
| 23 |
-
t = "@user" if t.startswith("@") and len(t) > 1 else t
|
| 24 |
-
t = "http" if t.startswith("http") else t
|
| 25 |
-
new_text.append(t)
|
| 26 |
-
return " ".join(new_text)
|
| 27 |
-
|
| 28 |
-
# ---- Function to process the input and return prediction
|
| 29 |
-
def sentiment_analysis(text):
|
| 30 |
-
text = preprocess(text)
|
| 31 |
-
|
| 32 |
-
encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
|
| 33 |
-
output = model(**encoded_input)
|
| 34 |
-
scores_ = output[0][0].detach().numpy()
|
| 35 |
-
scores_ = softmax(scores_)
|
| 36 |
-
|
| 37 |
-
# Format output dict of scores
|
| 38 |
-
labels = ["Negative", "Neutral", "Positive"]
|
| 39 |
-
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
| 40 |
-
|
| 41 |
-
return scores
|
| 42 |
-
|
| 43 |
-
|
| 44 |
# ---- Gradio app interface
|
| 45 |
app = gr.Interface(fn = sentiment_analysis,
|
| 46 |
inputs = gr.Textbox("Input your tweet to classify or use the example provided below..."),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ---- Gradio app interface
|
| 2 |
app = gr.Interface(fn = sentiment_analysis,
|
| 3 |
inputs = gr.Textbox("Input your tweet to classify or use the example provided below..."),
|