Spaces:
Sleeping
Sleeping
removed onnx runtim
Browse files
app.py
CHANGED
@@ -1,22 +1,23 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
from optimum.onnxruntime import ORTModelForSequenceClassification
|
4 |
import torch
|
5 |
|
|
|
6 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
7 |
|
|
|
8 |
model_name = "Rahmat82/DistilBERT-finetuned-on-emotion"
|
9 |
-
model =
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
11 |
-
model.to(device)
|
12 |
|
13 |
def predict(query: str) -> dict:
|
14 |
inputs = tokenizer(query, return_tensors='pt')
|
15 |
-
inputs.to(device)
|
16 |
outputs = model(**inputs)
|
17 |
outputs = torch.sigmoid(outputs.logits)
|
18 |
outputs = outputs.detach().cpu().numpy()
|
19 |
|
|
|
20 |
label2ids = {
|
21 |
"sadness": 0,
|
22 |
"joy": 1,
|
@@ -30,19 +31,71 @@ def predict(query: str) -> dict:
|
|
30 |
label2ids = {k: float(v) for k, v in sorted(label2ids.items(), key=lambda item: item[1], reverse=True)}
|
31 |
return label2ids
|
32 |
|
|
|
33 |
demo = gr.Interface(
|
34 |
-
theme
|
35 |
-
title
|
36 |
-
description
|
37 |
-
fn
|
38 |
-
inputs
|
39 |
-
outputs
|
40 |
-
allow_flagging
|
41 |
-
examples
|
42 |
["The gentle touch of your hand on mine is a silent promise that echoes through the corridors of my heart."],
|
43 |
["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."],
|
44 |
["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."],
|
45 |
-
|
46 |
)
|
47 |
|
48 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
|
3 |
import torch
|
4 |
|
5 |
+
# Check if GPU is available
|
6 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
7 |
|
8 |
+
# Load the model with 8-bit precision
|
9 |
model_name = "Rahmat82/DistilBERT-finetuned-on-emotion"
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, load_in_8bit=True, device_map="auto")
|
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()} # Move inputs to the appropriate device
|
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 |
"sadness": 0,
|
23 |
"joy": 1,
|
|
|
31 |
label2ids = {k: float(v) for k, v in sorted(label2ids.items(), key=lambda item: item[1], reverse=True)}
|
32 |
return label2ids
|
33 |
|
34 |
+
# Gradio interface setup
|
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 GPU it is much faster π</h3>",
|
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),
|
42 |
+
allow_flagging='never',
|
43 |
+
examples=[
|
44 |
["The gentle touch of your hand on mine is a silent promise that echoes through the corridors of my heart."],
|
45 |
["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."],
|
46 |
["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."],
|
47 |
+
]
|
48 |
)
|
49 |
|
50 |
+
demo.launch(share=True)
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
#import gradio as gr
|
55 |
+
# from transformers import pipeline, AutoTokenizer
|
56 |
+
# from optimum.onnxruntime import ORTModelForSequenceClassification
|
57 |
+
# import torch
|
58 |
+
|
59 |
+
# device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
60 |
+
|
61 |
+
# model_name = "Rahmat82/DistilBERT-finetuned-on-emotion"
|
62 |
+
# model = ORTModelForSequenceClassification.from_pretrained(model_name, export=True)
|
63 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
64 |
+
# model.to(device)
|
65 |
+
|
66 |
+
# def predict(query: str) -> dict:
|
67 |
+
# inputs = tokenizer(query, return_tensors='pt')
|
68 |
+
# inputs.to(device)
|
69 |
+
# outputs = model(**inputs)
|
70 |
+
# outputs = torch.sigmoid(outputs.logits)
|
71 |
+
# outputs = outputs.detach().cpu().numpy()
|
72 |
+
|
73 |
+
# label2ids = {
|
74 |
+
# "sadness": 0,
|
75 |
+
# "joy": 1,
|
76 |
+
# "love": 2,
|
77 |
+
# "anger": 3,
|
78 |
+
# "fear": 4,
|
79 |
+
# "surprise": 5,
|
80 |
+
# }
|
81 |
+
# for i, k in enumerate(label2ids.keys()):
|
82 |
+
# label2ids[k] = outputs[0][i]
|
83 |
+
# label2ids = {k: float(v) for k, v in sorted(label2ids.items(), key=lambda item: item[1], reverse=True)}
|
84 |
+
# return label2ids
|
85 |
+
|
86 |
+
# demo = gr.Interface(
|
87 |
+
# theme = gr.themes.Soft(),
|
88 |
+
# title = "RHM Emotion Classifier π",
|
89 |
+
# description = "Beyond Words: Capturing the Essence of Emotion in Text<h3>On GPU it is much faster π</h3>",
|
90 |
+
# fn = predict,
|
91 |
+
# inputs = gr.components.Textbox(label='Write your text here', lines=3),
|
92 |
+
# outputs = gr.components.Label(label='Predictions', num_top_classes=6),
|
93 |
+
# allow_flagging = 'never',
|
94 |
+
# examples = [
|
95 |
+
# ["The gentle touch of your hand on mine is a silent promise that echoes through the corridors of my heart."],
|
96 |
+
# ["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."],
|
97 |
+
# ["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."],
|
98 |
+
# ]
|
99 |
+
# )
|
100 |
+
|
101 |
+
# demo.launch(share=True)
|