Spaces:
Runtime error
Runtime error
Henamen21
commited on
Commit
·
6a8400d
1
Parent(s):
4890b6e
add app
Browse files- app.py +68 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing 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 |
+
|
11 |
+
# HuggingFace path where the fine tuned model is placed
|
12 |
+
model_path = "Henok21/test_trainer"
|
13 |
+
|
14 |
+
# Loading the model
|
15 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
16 |
+
|
17 |
+
# Loading config file
|
18 |
+
config = AutoConfig.from_pretrained(model_path)
|
19 |
+
|
20 |
+
# Loading tokenizer
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
|
22 |
+
|
23 |
+
# Using pipeline
|
24 |
+
calssifier = pipeline("sentiment-analysis" , model , tokenizer = tokenizer)
|
25 |
+
|
26 |
+
# Preprocessor Function
|
27 |
+
def preprocess(text):
|
28 |
+
new_text = []
|
29 |
+
for t in text.split(" "):
|
30 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
31 |
+
t = 'http' if t.startswith('http') else t
|
32 |
+
new_text.append(t)
|
33 |
+
return " ".join(new_text)
|
34 |
+
|
35 |
+
# Adjusting config
|
36 |
+
config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
|
37 |
+
|
38 |
+
|
39 |
+
# Function used for gradio app
|
40 |
+
def sentiment_analysis(text):
|
41 |
+
# Your code to get the scores for each class
|
42 |
+
scores = output[0][0].detach().numpy()
|
43 |
+
scores = softmax(scores)
|
44 |
+
|
45 |
+
# Convert the numpy array into a list
|
46 |
+
scores = scores.tolist()
|
47 |
+
|
48 |
+
# Print labels and scores
|
49 |
+
ranking = np.argsort(scores)
|
50 |
+
ranking = ranking[::-1]
|
51 |
+
for i in range(len(scores)):
|
52 |
+
l = config.id2label[ranking[i]]
|
53 |
+
|
54 |
+
s = scores[ranking[i]]
|
55 |
+
|
56 |
+
a = f"{i+1}) {l} {np.round(float(s), 4)}"
|
57 |
+
|
58 |
+
# Convert the numpy float32 object into a float
|
59 |
+
d[l] = float(s)
|
60 |
+
|
61 |
+
# Return the dictionary as the response content
|
62 |
+
return d
|
63 |
+
|
64 |
+
# Create your interface
|
65 |
+
demo = gr.Interface(fn=sentiment_analysis, inputs="text", outputs="label")
|
66 |
+
|
67 |
+
# Launch your interface
|
68 |
+
demo.launch(debug = True)
|
requirements.txt
ADDED
File without changes
|