Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
device = '
|
7 |
|
8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
model_name = "Rahmat82/DistilBERT-finetuned-on-emotion"
|
10 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
|
|
|
|
|
|
|
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
12 |
|
13 |
def predict(query: str) -> dict:
|
14 |
inputs = tokenizer(query, return_tensors='pt')
|
15 |
-
inputs = {k: v.to(device) for k, v in inputs.items()} #
|
16 |
outputs = model(**inputs)
|
17 |
outputs = torch.sigmoid(outputs.logits)
|
18 |
outputs = outputs.detach().cpu().numpy()
|
19 |
|
20 |
# Define label to ID mapping
|
21 |
label2ids = {
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
for i, k in enumerate(label2ids.keys()):
|
30 |
label2ids[k] = outputs[0][i]
|
31 |
label2ids = {k: float(v) for k, v in sorted(label2ids.items(), key=lambda item: item[1], reverse=True)}
|
@@ -35,7 +45,7 @@ def predict(query: str) -> dict:
|
|
35 |
demo = gr.Interface(
|
36 |
theme=gr.themes.Soft(),
|
37 |
title="RHM Emotion Classifier π",
|
38 |
-
description="Beyond Words: Capturing the Essence of Emotion in Text<h3>On
|
39 |
fn=predict,
|
40 |
inputs=gr.components.Textbox(label='Write your text here', lines=3),
|
41 |
outputs=gr.components.Label(label='Predictions', num_top_classes=6),
|
@@ -51,6 +61,7 @@ demo.launch(share=True)
|
|
51 |
|
52 |
|
53 |
|
|
|
54 |
#import gradio as gr
|
55 |
# from transformers import pipeline, AutoTokenizer
|
56 |
# from optimum.onnxruntime import ORTModelForSequenceClassification
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig
|
3 |
import torch
|
4 |
|
5 |
+
# Set device to CPU since GPU quantization is unavailable
|
6 |
+
device = 'cpu'
|
7 |
|
8 |
+
# Set up 8-bit quantization with BitsAndBytesConfig
|
9 |
+
quantization_config = BitsAndBytesConfig(
|
10 |
+
load_in_8bit=True, # Enable 8-bit quantization
|
11 |
+
llm_int8_enable_fp32_cpu_offload=True # Use CPU for 8-bit quantization operations
|
12 |
+
)
|
13 |
+
|
14 |
+
# Load the model with quantization configuration
|
15 |
model_name = "Rahmat82/DistilBERT-finetuned-on-emotion"
|
16 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
17 |
+
model_name,
|
18 |
+
quantization_config=quantization_config,
|
19 |
+
device_map={"": device} # Ensures everything runs on CPU
|
20 |
+
)
|
21 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
22 |
|
23 |
def predict(query: str) -> dict:
|
24 |
inputs = tokenizer(query, return_tensors='pt')
|
25 |
+
inputs = {k: v.to(device) for k, v in inputs.items()} # Ensure inputs are on CPU
|
26 |
outputs = model(**inputs)
|
27 |
outputs = torch.sigmoid(outputs.logits)
|
28 |
outputs = outputs.detach().cpu().numpy()
|
29 |
|
30 |
# Define label to ID mapping
|
31 |
label2ids = {
|
32 |
+
"sadness": 0,
|
33 |
+
"joy": 1,
|
34 |
+
"love": 2,
|
35 |
+
"anger": 3,
|
36 |
+
"fear": 4,
|
37 |
+
"surprise": 5,
|
38 |
+
}
|
39 |
for i, k in enumerate(label2ids.keys()):
|
40 |
label2ids[k] = outputs[0][i]
|
41 |
label2ids = {k: float(v) for k, v in sorted(label2ids.items(), key=lambda item: item[1], reverse=True)}
|
|
|
45 |
demo = gr.Interface(
|
46 |
theme=gr.themes.Soft(),
|
47 |
title="RHM Emotion Classifier π",
|
48 |
+
description="Beyond Words: Capturing the Essence of Emotion in Text<h3>On CPU with 8-bit quantization</h3>",
|
49 |
fn=predict,
|
50 |
inputs=gr.components.Textbox(label='Write your text here', lines=3),
|
51 |
outputs=gr.components.Label(label='Predictions', num_top_classes=6),
|
|
|
61 |
|
62 |
|
63 |
|
64 |
+
|
65 |
#import gradio as gr
|
66 |
# from transformers import pipeline, AutoTokenizer
|
67 |
# from optimum.onnxruntime import ORTModelForSequenceClassification
|