Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
import onnxruntime as ort
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load tokenizer and ONNX quantized model
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("onnx/")
|
8 |
+
session = ort.InferenceSession("onnx/model_quantized.onnx")
|
9 |
+
|
10 |
+
# Softmax function
|
11 |
+
def softmax(x):
|
12 |
+
e_x = np.exp(x - np.max(x))
|
13 |
+
return e_x / e_x.sum()
|
14 |
+
|
15 |
+
# Prediction function
|
16 |
+
def classify_sentiment(text):
|
17 |
+
# Tokenize the input text
|
18 |
+
inputs = tokenizer(text, return_tensors="np")
|
19 |
+
#print(inputs)
|
20 |
+
# Run inference
|
21 |
+
outputs = session.run(None, {
|
22 |
+
"input_ids": inputs["input_ids"],
|
23 |
+
"attention_mask": inputs["attention_mask"]
|
24 |
+
})
|
25 |
+
|
26 |
+
# Process logits
|
27 |
+
logits = outputs[0][0]
|
28 |
+
probs = softmax(logits)
|
29 |
+
pred_class = int(np.argmax(probs))
|
30 |
+
|
31 |
+
label_map = {0: "Negative", 1: "Positive"}
|
32 |
+
print(label_map[pred_class])
|
33 |
+
return label_map[pred_class]
|
34 |
+
|
35 |
+
|
36 |
+
# Gradio Interface
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=classify_sentiment,
|
39 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
40 |
+
outputs='label',
|
41 |
+
title="Sentiment Classifier",
|
42 |
+
description="Enter a sentence to classify its sentiment",
|
43 |
+
)
|
44 |
+
|
45 |
+
# Launch the app
|
46 |
+
if __name__ == "__main__":
|
47 |
+
interface.launch(share=True)
|