Spaces:
Runtime error
Runtime error
imalexianne
commited on
Commit
•
8a02fed
1
Parent(s):
8e2c25c
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,25 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
import numpy as np
|
4 |
from transformers import AutoTokenizer, AutoModel
|
5 |
from scipy.special import softmax
|
6 |
-
|
7 |
-
import gradio as gr
|
8 |
-
import numpy as np
|
9 |
-
import pandas as pd
|
10 |
-
import pickle
|
11 |
-
import transformers
|
12 |
-
from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification,TFAutoModelForSequenceClassification, pipeline
|
13 |
-
from scipy.special import softmax
|
14 |
-
from dotenv import load_dotenv, dotenv_values
|
15 |
from huggingface_hub import login
|
|
|
|
|
|
|
16 |
load_dotenv()
|
|
|
17 |
# Get the token from the environment variable
|
18 |
access_token = os.getenv("access_token")
|
19 |
-
login(os.getenv("access_token"))
|
20 |
|
|
|
|
|
21 |
|
22 |
# Requirements
|
23 |
-
|
24 |
model_path = "imalexianne/distilbert-base-uncased"
|
25 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
|
26 |
|
27 |
-
|
28 |
-
config = AutoConfig.from_pretrained(model_path)
|
29 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
30 |
-
|
31 |
-
# Preprocessessing function
|
32 |
def preprocess(text):
|
33 |
new_text = []
|
34 |
for x in text.split(" "):
|
@@ -37,30 +28,28 @@ def preprocess(text):
|
|
37 |
new_text.append(x)
|
38 |
return " ".join(new_text)
|
39 |
|
40 |
-
#
|
41 |
def sentiment_analysis(text):
|
42 |
text = preprocess(text)
|
43 |
-
|
44 |
-
encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
|
45 |
output = model(**encoded_input)
|
46 |
-
scores_ = output[0]
|
47 |
scores_ = softmax(scores_)
|
48 |
|
49 |
# Format output dict of scores
|
50 |
labels = ["Negative", "Neutral", "Positive"]
|
51 |
-
scores = {l:float(s) for (l,s) in zip(labels, scores_)
|
52 |
|
53 |
return scores
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
)
|
65 |
|
66 |
app.launch()
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
from transformers import AutoTokenizer, AutoModel
|
4 |
from scipy.special import softmax
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from huggingface_hub import login
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
from dotenv import load_dotenv
|
9 |
load_dotenv()
|
10 |
+
|
11 |
# Get the token from the environment variable
|
12 |
access_token = os.getenv("access_token")
|
|
|
13 |
|
14 |
+
# Log in to Hugging Face (commented out for now)
|
15 |
+
# login(access_token)
|
16 |
|
17 |
# Requirements
|
|
|
18 |
model_path = "imalexianne/distilbert-base-uncased"
|
19 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
20 |
+
model = AutoModel.from_pretrained(model_path)
|
21 |
|
22 |
+
# Preprocessing function
|
|
|
|
|
|
|
|
|
23 |
def preprocess(text):
|
24 |
new_text = []
|
25 |
for x in text.split(" "):
|
|
|
28 |
new_text.append(x)
|
29 |
return " ".join(new_text)
|
30 |
|
31 |
+
# Function to process the input and return prediction
|
32 |
def sentiment_analysis(text):
|
33 |
text = preprocess(text)
|
34 |
+
encoded_input = tokenizer(text, return_tensors="pt")
|
|
|
35 |
output = model(**encoded_input)
|
36 |
+
scores_ = output.logits[0].detach().numpy()
|
37 |
scores_ = softmax(scores_)
|
38 |
|
39 |
# Format output dict of scores
|
40 |
labels = ["Negative", "Neutral", "Positive"]
|
41 |
+
scores = {l: float(s) for (l, s) in zip(labels, scores_)}
|
42 |
|
43 |
return scores
|
44 |
|
45 |
+
# Gradio app interface
|
46 |
+
app = gr.Interface(
|
47 |
+
fn=sentiment_analysis,
|
48 |
+
inputs=gr.Textbox("Write your text here..."),
|
49 |
+
outputs="label",
|
50 |
+
title="Sentiment Analysis of Tweets on COVID-19 Vaccines",
|
51 |
+
description="Sentiment Analysis of text based on tweets about COVID-19 Vaccines using a fine-tuned 'distilbert-base-uncased' model",
|
52 |
+
examples=[["Covid vaccination has no positive impact"]]
|
53 |
+
)
|
|
|
54 |
|
55 |
app.launch()
|