imalexianne commited on
Commit
397ed79
1 Parent(s): 3e68d2f

Add application file

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +59 -0
  3. requirements.txt +0 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .env
2
+ *venv/
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
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
+ from dotenv import load_dotenv, dotenv_values
10
+ # from huggingface_hub import login
11
+
12
+ from huggingface_hub import login
13
+
14
+ # notebook_login()
15
+ load_dotenv()
16
+ login(os.getenv("access_token"))
17
+ # Requirements
18
+ model_path = "imalexianne/distilbert-base-uncased"
19
+
20
+ tokenizer = AutoTokenizer.from_pretrained(model_path, revision="main")
21
+ config = AutoConfig.from_pretrained(model_path, revision="main")
22
+ model = AutoModelForSequenceClassification.from_pretrained(model_path, revision="main")
23
+
24
+ # Preprocess text (username and link placeholders)
25
+ def preprocess(text):
26
+ new_text = []
27
+ for x in text.split(" "):
28
+ x = "@user" if x.startswith("@") and len(x) > 1 else x
29
+ x = "http" if x.startswith("http") else x
30
+ new_text.append(x)
31
+ return " ".join(new_text)
32
+
33
+ # ---- Function to process the input and return prediction
34
+ def sentiment_analysis(text):
35
+ text = preprocess(text)
36
+
37
+ encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
38
+ output = model(**encoded_input)
39
+ scores_ = output[0][0].detach().numpy()
40
+ scores_ = softmax(scores_)
41
+
42
+ # Format output dict of scores
43
+ labels = ["Negative", "Neutral", "Positive"]
44
+ scores = {l:float(s) for (l,s) in zip(labels, scores_) }
45
+
46
+ return scores
47
+
48
+
49
+ # ---- Gradio app interface
50
+ app = gr.Interface(fn = sentiment_analysis,
51
+ inputs = gr.Textbox("Write here"),
52
+ outputs = "label",
53
+ title = "Sentiment Analysis of Tweets on COVID-19 Vaccines",
54
+ description = "Sentiment Analysis of text based on tweets about COVID-19 Vaccines using a fine-tuned 'distilbert-base-uncased' model",
55
+
56
+ examples = [["Covid vaccination has no positive impact"]]
57
+ )
58
+
59
+ app.launch(share=True)
requirements.txt ADDED
File without changes