Spaces:
Sleeping
Sleeping
Henamen21
commited on
Commit
·
24453e1
1
Parent(s):
1e7d53f
adding files
Browse files- inference.py +79 -0
- requirements.txt +2 -0
inference.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import a module
|
2 |
+
from transformers import AutoModelForSequenceClassification
|
3 |
+
from transformers import TFAutoModelForSequenceClassification
|
4 |
+
from transformers import AutoModel, AutoTokenizer
|
5 |
+
from transformers import AutoTokenizer , pipeline , AutoConfig
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
from scipy.special import softmax
|
10 |
+
import torch
|
11 |
+
|
12 |
+
# Loading requirements from Hugging Face
|
13 |
+
# HuggingFace path where the fine tuned model is placed
|
14 |
+
model_path = "Henok21/test_trainer"
|
15 |
+
|
16 |
+
# Loading the model
|
17 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
18 |
+
|
19 |
+
config = AutoConfig.from_pretrained(model_path)
|
20 |
+
|
21 |
+
# Loading tokenizer
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
|
23 |
+
|
24 |
+
# Creating pipeline
|
25 |
+
calssifier = pipeline("sentiment-analysis" , model , tokenizer = tokenizer)
|
26 |
+
|
27 |
+
# Preparing gradio app
|
28 |
+
# Preprocessor Function
|
29 |
+
def preprocess(text):
|
30 |
+
new_text = []
|
31 |
+
for t in text.split(" "):
|
32 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
33 |
+
t = 'http' if t.startswith('http') else t
|
34 |
+
new_text.append(t)
|
35 |
+
return " ".join(new_text)
|
36 |
+
|
37 |
+
# Configuring the outputs
|
38 |
+
config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
|
39 |
+
config.label2id = {"NEGATIVE": 0, "NEUTRAL": 1, "POSITIVE": 2}
|
40 |
+
|
41 |
+
# creating a function used for gradio app
|
42 |
+
# Creating dictionary
|
43 |
+
dictionary = {}
|
44 |
+
|
45 |
+
def sentiment_analysis(text):
|
46 |
+
|
47 |
+
# Encode the text using the tokenizer
|
48 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
49 |
+
# Get the output logits from the model
|
50 |
+
output = model(**encoded_input)
|
51 |
+
|
52 |
+
# Your code to get the scores for each class
|
53 |
+
scores = output[0][0].detach().numpy()
|
54 |
+
scores = softmax(scores)
|
55 |
+
|
56 |
+
# Convert the numpy array into a list
|
57 |
+
scores = scores.tolist()
|
58 |
+
ranking = np.argsort(scores)
|
59 |
+
ranking = ranking[::-1]
|
60 |
+
|
61 |
+
for i in range(len(scores)):
|
62 |
+
l = config.id2label[ranking[i]]
|
63 |
+
s = scores[ranking[i]]
|
64 |
+
|
65 |
+
# Convert the numpy float32 object into a float
|
66 |
+
dictionary[l] = float(s)
|
67 |
+
|
68 |
+
# Return the dictionary as the response content
|
69 |
+
return dictionary
|
70 |
+
|
71 |
+
# Create your interface
|
72 |
+
demo = gr.Interface(
|
73 |
+
fn=sentiment_analysis,
|
74 |
+
inputs="text",
|
75 |
+
outputs="label"
|
76 |
+
)
|
77 |
+
|
78 |
+
# Launch your interface
|
79 |
+
demo.launch(debug = True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|