Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
|
|
3 |
import torch
|
4 |
|
5 |
-
|
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 =
|
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
|
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 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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)}
|
42 |
return label2ids
|
43 |
|
44 |
-
# Gradio interface setup
|
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
|
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),
|
52 |
-
allow_flagging='never',
|
53 |
-
examples=[
|
54 |
["The gentle touch of your hand on mine is a silent promise that echoes through the corridors of my heart."],
|
55 |
["The rain mirrored the tears I couldn't stop, each drop a tiny echo of the ache in my heart. The world seemed muted, colors drained, and a heavy weight settled upon my soul."],
|
56 |
["Walking through the dusty attic, I stumbled upon a hidden door. With a mix of trepidation and excitement, I pushed it open, expecting cobwebs and forgotten junk. Instead, a flood of sunlight revealed a secret garden, blooming with vibrant flowers and buzzing with life. My jaw dropped in pure astonishment."],
|
57 |
-
|
58 |
)
|
59 |
|
60 |
-
demo.launch(share=True)
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
#import gradio as gr
|
66 |
-
# from transformers import pipeline, AutoTokenizer
|
67 |
-
# from optimum.onnxruntime import ORTModelForSequenceClassification
|
68 |
-
# import torch
|
69 |
-
|
70 |
-
# device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
71 |
-
|
72 |
-
# model_name = "Rahmat82/DistilBERT-finetuned-on-emotion"
|
73 |
-
# model = ORTModelForSequenceClassification.from_pretrained(model_name, export=True)
|
74 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
75 |
-
# model.to(device)
|
76 |
-
|
77 |
-
# def predict(query: str) -> dict:
|
78 |
-
# inputs = tokenizer(query, return_tensors='pt')
|
79 |
-
# inputs.to(device)
|
80 |
-
# outputs = model(**inputs)
|
81 |
-
# outputs = torch.sigmoid(outputs.logits)
|
82 |
-
# outputs = outputs.detach().cpu().numpy()
|
83 |
-
|
84 |
-
# label2ids = {
|
85 |
-
# "sadness": 0,
|
86 |
-
# "joy": 1,
|
87 |
-
# "love": 2,
|
88 |
-
# "anger": 3,
|
89 |
-
# "fear": 4,
|
90 |
-
# "surprise": 5,
|
91 |
-
# }
|
92 |
-
# for i, k in enumerate(label2ids.keys()):
|
93 |
-
# label2ids[k] = outputs[0][i]
|
94 |
-
# label2ids = {k: float(v) for k, v in sorted(label2ids.items(), key=lambda item: item[1], reverse=True)}
|
95 |
-
# return label2ids
|
96 |
-
|
97 |
-
# demo = gr.Interface(
|
98 |
-
# theme = gr.themes.Soft(),
|
99 |
-
# title = "RHM Emotion Classifier π",
|
100 |
-
# description = "Beyond Words: Capturing the Essence of Emotion in Text<h3>On GPU it is much faster π</h3>",
|
101 |
-
# fn = predict,
|
102 |
-
# inputs = gr.components.Textbox(label='Write your text here', lines=3),
|
103 |
-
# outputs = gr.components.Label(label='Predictions', num_top_classes=6),
|
104 |
-
# allow_flagging = 'never',
|
105 |
-
# examples = [
|
106 |
-
# ["The gentle touch of your hand on mine is a silent promise that echoes through the corridors of my heart."],
|
107 |
-
# ["The rain mirrored the tears I couldn't stop, each drop a tiny echo of the ache in my heart. The world seemed muted, colors drained, and a heavy weight settled upon my soul."],
|
108 |
-
# ["Walking through the dusty attic, I stumbled upon a hidden door. With a mix of trepidation and excitement, I pushed it open, expecting cobwebs and forgotten junk. Instead, a flood of sunlight revealed a secret garden, blooming with vibrant flowers and buzzing with life. My jaw dropped in pure astonishment."],
|
109 |
-
# ]
|
110 |
-
# )
|
111 |
-
|
112 |
-
# demo.launch(share=True)
|
|
|
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)}
|
42 |
+
# return label2ids
|
43 |
+
|
44 |
+
# # Gradio interface setup
|
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),
|
52 |
+
# allow_flagging='never',
|
53 |
+
# examples=[
|
54 |
+
# ["The gentle touch of your hand on mine is a silent promise that echoes through the corridors of my heart."],
|
55 |
+
# ["The rain mirrored the tears I couldn't stop, each drop a tiny echo of the ache in my heart. The world seemed muted, colors drained, and a heavy weight settled upon my soul."],
|
56 |
+
# ["Walking through the dusty attic, I stumbled upon a hidden door. With a mix of trepidation and excitement, I pushed it open, expecting cobwebs and forgotten junk. Instead, a flood of sunlight revealed a secret garden, blooming with vibrant flowers and buzzing with life. My jaw dropped in pure astonishment."],
|
57 |
+
# ]
|
58 |
+
# )
|
59 |
+
|
60 |
+
# demo.launch(share=True)
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
import gradio as gr
|
66 |
+
from transformers import pipeline, AutoTokenizer
|
67 |
+
from optimum.onnxruntime import ORTModelForSequenceClassification
|
68 |
import torch
|
69 |
|
70 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
model_name = "Rahmat82/DistilBERT-finetuned-on-emotion"
|
73 |
+
model = ORTModelForSequenceClassification.from_pretrained(model_name, export=True)
|
|
|
|
|
|
|
|
|
74 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
75 |
+
model.to(device)
|
76 |
|
77 |
def predict(query: str) -> dict:
|
78 |
inputs = tokenizer(query, return_tensors='pt')
|
79 |
+
inputs.to(device)
|
80 |
outputs = model(**inputs)
|
81 |
outputs = torch.sigmoid(outputs.logits)
|
82 |
outputs = outputs.detach().cpu().numpy()
|
83 |
|
|
|
84 |
label2ids = {
|
85 |
+
"sadness": 0,
|
86 |
+
"joy": 1,
|
87 |
+
"love": 2,
|
88 |
+
"anger": 3,
|
89 |
+
"fear": 4,
|
90 |
+
"surprise": 5,
|
91 |
+
}
|
92 |
for i, k in enumerate(label2ids.keys()):
|
93 |
label2ids[k] = outputs[0][i]
|
94 |
label2ids = {k: float(v) for k, v in sorted(label2ids.items(), key=lambda item: item[1], reverse=True)}
|
95 |
return label2ids
|
96 |
|
|
|
97 |
demo = gr.Interface(
|
98 |
+
theme = gr.themes.Soft(),
|
99 |
+
title = "RHM Emotion Classifier π",
|
100 |
+
description = "Beyond Words: Capturing the Essence of Emotion in Text<h3>On GPU it is much faster π</h3>",
|
101 |
+
fn = predict,
|
102 |
+
inputs = gr.components.Textbox(label='Write your text here', lines=3),
|
103 |
+
outputs = gr.components.Label(label='Predictions', num_top_classes=6),
|
104 |
+
allow_flagging = 'never',
|
105 |
+
examples = [
|
106 |
["The gentle touch of your hand on mine is a silent promise that echoes through the corridors of my heart."],
|
107 |
["The rain mirrored the tears I couldn't stop, each drop a tiny echo of the ache in my heart. The world seemed muted, colors drained, and a heavy weight settled upon my soul."],
|
108 |
["Walking through the dusty attic, I stumbled upon a hidden door. With a mix of trepidation and excitement, I pushed it open, expecting cobwebs and forgotten junk. Instead, a flood of sunlight revealed a secret garden, blooming with vibrant flowers and buzzing with life. My jaw dropped in pure astonishment."],
|
109 |
+
]
|
110 |
)
|
111 |
|
112 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|