Spaces:
Runtime error
Runtime error
pushpinder08
commited on
Commit
•
1d39365
1
Parent(s):
295ecc7
Update app.py
Browse files
app.py
CHANGED
@@ -1,151 +1,66 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
numeric_cols,
|
29 |
-
selected="Bill Length (mm)",
|
30 |
-
),
|
31 |
-
ui.input_selectize(
|
32 |
-
"yvar",
|
33 |
-
"Y variable",
|
34 |
-
numeric_cols,
|
35 |
-
selected="Bill Depth (mm)",
|
36 |
-
),
|
37 |
-
ui.input_checkbox_group(
|
38 |
-
"species", "Filter by species", species, selected=species
|
39 |
-
),
|
40 |
-
ui.hr(),
|
41 |
-
ui.input_switch("by_species", "Show species", value=True),
|
42 |
-
ui.input_switch("show_margins", "Show marginal plots", value=True),
|
43 |
-
),
|
44 |
-
ui.output_ui("value_boxes"),
|
45 |
-
ui.output_plot("scatter", fill=True),
|
46 |
-
ui.help_text(
|
47 |
-
"Artwork by ",
|
48 |
-
ui.a("@allison_horst", href="https://twitter.com/allison_horst"),
|
49 |
-
class_="text-end",
|
50 |
-
),
|
51 |
-
),
|
52 |
-
)
|
53 |
-
|
54 |
-
|
55 |
-
def server(input: Inputs, output: Outputs, session: Session):
|
56 |
-
@reactive.Calc
|
57 |
-
def filtered_df() -> pd.DataFrame:
|
58 |
-
"""Returns a Pandas data frame that includes only the desired rows"""
|
59 |
-
|
60 |
-
# This calculation "req"uires that at least one species is selected
|
61 |
-
req(len(input.species()) > 0)
|
62 |
-
|
63 |
-
# Filter the rows so we only include the desired species
|
64 |
-
return df[df["Species"].isin(input.species())]
|
65 |
-
|
66 |
-
@output
|
67 |
-
@render.plot
|
68 |
-
def scatter():
|
69 |
-
"""Generates a plot for Shiny to display to the user"""
|
70 |
-
|
71 |
-
# The plotting function to use depends on whether margins are desired
|
72 |
-
plotfunc = sns.jointplot if input.show_margins() else sns.scatterplot
|
73 |
-
|
74 |
-
plotfunc(
|
75 |
-
data=filtered_df(),
|
76 |
-
x=input.xvar(),
|
77 |
-
y=input.yvar(),
|
78 |
-
palette=palette,
|
79 |
-
hue="Species" if input.by_species() else None,
|
80 |
-
hue_order=species,
|
81 |
-
legend=False,
|
82 |
)
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
# Artwork by @allison_horst
|
119 |
-
showcase_img=f"{name}.png",
|
120 |
-
)
|
121 |
-
for name in species
|
122 |
-
# Only include boxes for _selected_ species
|
123 |
-
if name in input.species()
|
124 |
-
]
|
125 |
-
|
126 |
-
return ui.layout_column_wrap(*value_boxes, width = 1 / len(value_boxes))
|
127 |
-
|
128 |
-
|
129 |
-
# "darkorange", "purple", "cyan4"
|
130 |
-
colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
|
131 |
-
colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
|
132 |
-
|
133 |
-
palette: Dict[str, Tuple[float, float, float]] = {
|
134 |
-
"Adelie": colors[0],
|
135 |
-
"Chinstrap": colors[1],
|
136 |
-
"Gentoo": colors[2],
|
137 |
-
"default": sns.color_palette()[0], # type: ignore
|
138 |
-
}
|
139 |
-
|
140 |
-
bg_palette = {}
|
141 |
-
# Use `sns.set_style("whitegrid")` to help find approx alpha value
|
142 |
-
for name, col in palette.items():
|
143 |
-
# Adjusted n_colors until `axe` accessibility did not complain about color contrast
|
144 |
-
bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
|
145 |
-
|
146 |
-
|
147 |
-
app = App(
|
148 |
-
app_ui,
|
149 |
-
server,
|
150 |
-
static_assets=str(www_dir),
|
151 |
)
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
# Set environment variables to address warnings
|
4 |
+
os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib-config'
|
5 |
+
os.environ['XDG_CACHE_HOME'] = '/tmp/fontconfig-cache'
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
from transformers import BertTokenizer, BertModel, BertPreTrainedModel
|
9 |
+
import torch
|
10 |
+
from torch import nn
|
11 |
+
|
12 |
+
class CustomBertForSequenceClassification(BertPreTrainedModel):
|
13 |
+
def __init__(self, config):
|
14 |
+
super().__init__(config)
|
15 |
+
self.bert = BertModel(config)
|
16 |
+
self.dropout = nn.Dropout(config.hidden_dropout_prob)
|
17 |
+
self.fc_positive = nn.Linear(config.hidden_size, 1)
|
18 |
+
self.fc_neutral = nn.Linear(config.hidden_size, 1)
|
19 |
+
self.fc_negative = nn.Linear(config.hidden_size, 1)
|
20 |
+
|
21 |
+
self.init_weights()
|
22 |
+
|
23 |
+
def forward(self, input_ids, attention_mask=None, token_type_ids=None):
|
24 |
+
outputs = self.bert(
|
25 |
+
input_ids,
|
26 |
+
attention_mask=attention_mask,
|
27 |
+
token_type_ids=token_type_ids,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
)
|
29 |
+
pooled_output = outputs[1]
|
30 |
+
pooled_output = self.dropout(pooled_output)
|
31 |
+
|
32 |
+
logits_positive = self.fc_positive(pooled_output)
|
33 |
+
logits_neutral = self.fc_neutral(pooled_output)
|
34 |
+
logits_negative = self.fc_negative(pooled_output)
|
35 |
+
|
36 |
+
logits = torch.cat([logits_positive, logits_neutral, logits_negative], dim=-1)
|
37 |
+
return logits
|
38 |
+
|
39 |
+
# Load the tokenizer
|
40 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
41 |
+
|
42 |
+
# Load the model with the trained weights
|
43 |
+
model = CustomBertForSequenceClassification.from_pretrained('bert-base-uncased')
|
44 |
+
model.load_state_dict(torch.load('bert_classifier.pth', map_location=torch.device('cpu')))
|
45 |
+
model.eval()
|
46 |
+
|
47 |
+
# Define the class labels
|
48 |
+
class_labels = ["No Surprise", "Positive Surprise", "Negative Surprise"]
|
49 |
+
|
50 |
+
def classify_text(text):
|
51 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
52 |
+
with torch.no_grad():
|
53 |
+
outputs = model(**inputs)
|
54 |
+
logits = outputs
|
55 |
+
prediction = torch.argmax(logits, dim=1).item()
|
56 |
+
return class_labels[prediction]
|
57 |
+
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=classify_text,
|
60 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter text here..."),
|
61 |
+
outputs="label",
|
62 |
+
title="BERT Classifier for Surprises",
|
63 |
+
description="Enter text to classify it as a positive surprise, negative surprise, or no surprise",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
)
|
65 |
+
|
66 |
+
iface.launch()
|