Spaces:
Runtime error
Runtime error
APP: init
Browse files- .gitignore +1 -0
- app.py +46 -0
- requirements.txt +5 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*venv/
|
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSequenceClassification
|
2 |
+
from transformers import TFAutoModelForSequenceClassification
|
3 |
+
from transformers import AutoTokenizer, AutoConfig
|
4 |
+
import numpy as np
|
5 |
+
from scipy.special import softmax
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Requirements
|
9 |
+
model_path = f"Calistus/test_trainer"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
|
11 |
+
config = AutoConfig.from_pretrained(model_path)
|
12 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
13 |
+
|
14 |
+
# Preprocess text (username and link placeholders)
|
15 |
+
def preprocess(text):
|
16 |
+
new_text = []
|
17 |
+
for t in text.split(" "):
|
18 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
19 |
+
t = 'http' if t.startswith('http') else t
|
20 |
+
new_text.append(t)
|
21 |
+
return " ".join(new_text)
|
22 |
+
|
23 |
+
|
24 |
+
def sentiment_analysis(text):
|
25 |
+
text = preprocess(text)
|
26 |
+
|
27 |
+
# PyTorch-based models
|
28 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
29 |
+
output = model(**encoded_input)
|
30 |
+
scores_ = output[0][0].detach().numpy()
|
31 |
+
scores_ = softmax(scores_)
|
32 |
+
|
33 |
+
# Format output dict of scores
|
34 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
35 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
36 |
+
|
37 |
+
return scores
|
38 |
+
|
39 |
+
app = gr.Interface(
|
40 |
+
fn=sentiment_analysis,
|
41 |
+
inputs=gr.Textbox(placeholder="Write your tweet here..."),
|
42 |
+
outputs="label",
|
43 |
+
interpretation="default",
|
44 |
+
examples=[["Please don't listen to anyone. Vaccinate your child"],['My kid has a lump on his hand because of the vaccine']])
|
45 |
+
|
46 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
gradio
|
3 |
+
numpy
|
4 |
+
scipy
|
5 |
+
torch
|