Spaces:
Runtime error
Runtime error
app init
Browse files- .gitignore +1 -0
- app.py +58 -0
- requirements.txt +5 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.venv/
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForSequenceClassification
|
5 |
+
from transformers import TFAutoModelForSequenceClassification
|
6 |
+
from transformers import AutoTokenizer, AutoConfig
|
7 |
+
from scipy.special import softmax
|
8 |
+
|
9 |
+
|
10 |
+
# Setup
|
11 |
+
model_path = f"pakornor/roberta-base"
|
12 |
+
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained('roberta-base')
|
14 |
+
config = AutoConfig.from_pretrained(model_path)
|
15 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
16 |
+
|
17 |
+
|
18 |
+
# Functions
|
19 |
+
|
20 |
+
# Preprocess text
|
21 |
+
def preprocess(text):
|
22 |
+
new_text = []
|
23 |
+
for t in text.split(" "):
|
24 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
25 |
+
t = 'http' if t.startswith('http') else t
|
26 |
+
new_text.append(t)
|
27 |
+
return " ".join(new_text)
|
28 |
+
|
29 |
+
# Input preprocessing
|
30 |
+
def sentiment_analysis(text):
|
31 |
+
text = preprocess(text)
|
32 |
+
|
33 |
+
|
34 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
35 |
+
output = model(**encoded_input)
|
36 |
+
scores_ = output[0][0].detach().numpy()
|
37 |
+
scores_ = softmax(scores_)
|
38 |
+
|
39 |
+
# Format output dictionary 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 |
+
|
46 |
+
# Gradio App
|
47 |
+
app = gr.Interface(
|
48 |
+
fn=sentiment_analysis,
|
49 |
+
inputs=gr.Textbox("Input tweet here:"),
|
50 |
+
outputs="label",
|
51 |
+
title="Sentiment Analysis of Tweets on Covid-19 Vaccines",
|
52 |
+
description="With this App, you can type Tweets related to the Covid Vaccine and the app will rate the sentiment of the tweet..!",
|
53 |
+
examples=[["Be careful of covid vaccination"],
|
54 |
+
["The vaccine can reduce your immunity to diseases"],
|
55 |
+
["I cant wait for the Covid Vaccine!"]]
|
56 |
+
)
|
57 |
+
|
58 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers==4.35.0
|
2 |
+
gradio==4.2.0
|
3 |
+
numpy==1.23.5
|
4 |
+
scipy==1.11.3
|
5 |
+
scikit-learn==1.2.2
|