bambadij commited on
Commit
bd9e726
·
1 Parent(s): 91376cd

add conf gradio

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +61 -0
  3. requirements.txt +6 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ *venv/
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from scipy.special import softmax
3
+ from transformers import AutoTokenizer, AutoConfig
4
+ from transformers import AutoModelForSequenceClassification
5
+ import numpy as np
6
+
7
+ #Setup
8
+ Model = f"cardiffnlp/twitter-roberta-base-sentiment-latest"
9
+ tokenizer = AutoTokenizer.from_pretrained(Model)
10
+ model_path = f"bambadij/sentiment_analysis_model_trainer"
11
+ config = AutoConfig.from_pretrained(model_path)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
13
+
14
+ #Function
15
+
16
+ # Preprocess text (username and link placeholders)
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
+ # Input preprocessing
26
+ text = "Covid cases are increasing fast!"
27
+ text = preprocess(text)
28
+
29
+ # PyTorch-based models
30
+ encoded_input = tokenizer(text, return_tensors='pt')
31
+ output = model(**encoded_input)
32
+ scores = output[0][0].detach().numpy()
33
+ scores = softmax(scores)
34
+
35
+ def sentiment_analysis(text):
36
+ text =preprocess(text)
37
+ #Pytorch-based models
38
+ encoded_input = tokenizer(text,return_tensors='pt')
39
+ output = model(**encoded_input)
40
+ scores_ = output[0][0].detach().numpy()
41
+ scores_ =softmax(scores_)
42
+
43
+ #Foramt ouptput dict of scores
44
+ labels =['Negative','Neutral','Positive']
45
+ scores = {l:float(s) for (l,s) in zip(labels,scores_)}
46
+ return scores
47
+
48
+ demo = gr.Interface(
49
+ fn=sentiment_analysis,
50
+ inputs=gr.Textbox(placeholder="Copy and paste /Write a tweet her..."),
51
+ outputs="text",
52
+ examples=[["what's up with the vaccine"],
53
+ ["Covid cases are increasing fast!"],
54
+ ["Covid has been invented by Issa"],
55
+ ["I have a covid"],
56
+ ["All the people are sick maybe it's covid"],
57
+ ],
58
+ title="Tutorial:Sentiment Analysis App",
59
+ description = "This Aplication assesses if a twitter post relating vaccination is positive"
60
+ )
61
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers==4.35.0
2
+ gradio==4.1.1
3
+ numpy==1.23.5
4
+ scipy==1.11.3
5
+ torch @ https://download.pytorch.org/whl/cu118/torch-2.1.0%2Bcu118-cp310-cp310-linux_x86_64.whl#sha256=a81b554184492005543ddc32e96469f9369d778dedd195d73bda9bed407d6589
6
+ black