Spaces:
Sleeping
Sleeping
KwabenaMufasa
commited on
Commit
•
81442b0
1
Parent(s):
e667323
docker files
Browse files- app.py +55 -0
- dockerfile.txt +26 -0
- requirements.txt +14 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import the required Libraries
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import pickle
|
6 |
+
import transformers
|
7 |
+
from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification,TFAutoModelForSequenceClassification, pipeline
|
8 |
+
from scipy.special import softmax
|
9 |
+
|
10 |
+
# Requirements
|
11 |
+
model_path = "KwabenaMufasa/Finetuned-Distilbert-base-model"
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
13 |
+
config = AutoConfig.from_pretrained(model_path)
|
14 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
15 |
+
|
16 |
+
#Preprocess text
|
17 |
+
def preprocess(text):
|
18 |
+
new_text = []
|
19 |
+
for t in text.split(" "):
|
20 |
+
t = "@user" if t.startswith("@") and len(t) > 1 else t
|
21 |
+
t = "http" if t.startswith("http") else t
|
22 |
+
new_text.append(t)
|
23 |
+
return " ".join(new_text)
|
24 |
+
|
25 |
+
#Process the input and return prediction
|
26 |
+
def sentiment_analysis(text):
|
27 |
+
text = preprocess(text)
|
28 |
+
|
29 |
+
encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
|
30 |
+
output = model(**encoded_input)
|
31 |
+
scores_ = output[0][0].detach().numpy()
|
32 |
+
scores_ = softmax(scores_)
|
33 |
+
|
34 |
+
# Format output dict of scores
|
35 |
+
labels = ["Negative", "Neutral", "Positive"]
|
36 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
37 |
+
|
38 |
+
return scores
|
39 |
+
|
40 |
+
#Gradio app interface
|
41 |
+
app = gr.Interface(fn = sentiment_analysis,
|
42 |
+
inputs = gr.Textbox("Write your text or tweet here"),
|
43 |
+
outputs = "label",
|
44 |
+
title = "Twitter Sentiment Analyzer App",
|
45 |
+
description = "Vaccinate or Do Not Vaccinate",
|
46 |
+
interpretation = "default",
|
47 |
+
examples = [["Being vaccinated is actually awesome :)"]]
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
app.launch(server_name = "0.0.0.0.", server_port = 7860)
|
53 |
+
|
54 |
+
if __name__=="__app__":
|
55 |
+
run()
|
dockerfile.txt
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
# Set up a new user named "user" with user ID 1000
|
10 |
+
RUN useradd -m -u 1000 user
|
11 |
+
# Switch to the "user" user
|
12 |
+
USER user
|
13 |
+
# Set home to the user's home directory
|
14 |
+
ENV HOME=/home/user \
|
15 |
+
PATH=/home/user/.local/bin:$PATH
|
16 |
+
|
17 |
+
# Set the working directory to the user's home directory
|
18 |
+
WORKDIR $HOME/app
|
19 |
+
|
20 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
21 |
+
COPY --chown=user . $HOME/app
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
#COPY . .
|
26 |
+
CMD ["python","app.py"]
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
scikit-learn
|
3 |
+
seaborn
|
4 |
+
fastai
|
5 |
+
transformers
|
6 |
+
simpletransformers
|
7 |
+
nltk
|
8 |
+
spacy
|
9 |
+
gensim
|
10 |
+
plotly
|
11 |
+
notebook
|
12 |
+
jupyter
|
13 |
+
ipywidgets
|
14 |
+
gradio
|