Spaces:
Runtime error
Runtime error
pushpinder08
commited on
Commit
•
23ce71d
1
Parent(s):
739db44
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,8 @@
|
|
1 |
-
import os
|
2 |
-
import logging
|
3 |
-
from transformers import BertTokenizer, BertModel, BertPreTrainedModel, BertConfig
|
4 |
-
import torch
|
5 |
-
from torch import nn
|
6 |
import gradio as gr
|
7 |
-
import torch.nn.functional as F
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
os.environ['XDG_CACHE_HOME'] = '/tmp/fontconfig-cache'
|
12 |
-
os.environ['HF_HOME'] = '/tmp/hf_cache'
|
13 |
|
14 |
-
|
15 |
-
if not os.path.exists('/tmp/hf_cache'):
|
16 |
-
os.makedirs('/tmp/hf_cache')
|
17 |
-
|
18 |
-
logging.basicConfig(level=logging.INFO)
|
19 |
-
|
20 |
-
class CustomBertForSequenceClassification(BertPreTrainedModel):
|
21 |
-
def __init__(self, config):
|
22 |
-
super().__init__(config)
|
23 |
-
self.bert = BertModel(config)
|
24 |
-
self.dropout = nn.Dropout(config.hidden_dropout_prob)
|
25 |
-
self.fc_positive = nn.Linear(config.hidden_size, 1)
|
26 |
-
self.fc_neutral = nn.Linear(config.hidden_size, 1)
|
27 |
-
self.fc_negative = nn.Linear(config.hidden_size, 1)
|
28 |
-
self.init_weights()
|
29 |
-
|
30 |
-
def forward(self, input_ids, attention_mask=None, token_type_ids=None):
|
31 |
-
outputs = self.bert(
|
32 |
-
input_ids,
|
33 |
-
attention_mask=attention_mask,
|
34 |
-
token_type_ids=token_type_ids,
|
35 |
-
)
|
36 |
-
pooled_output = outputs[1]
|
37 |
-
pooled_output = self.dropout(pooled_output)
|
38 |
-
logits_positive = self.fc_positive(pooled_output)
|
39 |
-
logits_neutral = self.fc_neutral(pooled_output)
|
40 |
-
logits_negative = self.fc_negative(pooled_output)
|
41 |
-
return logits_positive, logits_neutral, logits_negative
|
42 |
-
|
43 |
-
# Load the tokenizer
|
44 |
-
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
45 |
-
|
46 |
-
# Load the model with the trained weights
|
47 |
-
model = CustomBertForSequenceClassification(BertConfig.from_pretrained('bert-base-uncased'))
|
48 |
-
model.load_state_dict(torch.load('bert_classifier.pth', map_location=torch.device('cpu')), strict=False)
|
49 |
-
model.eval()
|
50 |
-
|
51 |
-
# Define the class labels
|
52 |
-
class_labels = ["Positive Surprise", "No Surprise", "Negative Surprise"]
|
53 |
-
|
54 |
-
def classify_text(text):
|
55 |
-
logging.info("Classifying text...")
|
56 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
57 |
-
with torch.no_grad():
|
58 |
-
logits_positive, logits_neutral, logits_negative = model(**inputs)
|
59 |
-
prob_positive = torch.sigmoid(logits_positive).item()
|
60 |
-
prob_neutral = torch.sigmoid(logits_neutral).item()
|
61 |
-
prob_negative = torch.sigmoid(logits_negative).item()
|
62 |
-
prob_dict = {
|
63 |
-
"Positive Surprise": round(prob_positive, 4),
|
64 |
-
"No Surprise": round(prob_neutral, 4),
|
65 |
-
"Negative Surprise": round(prob_negative, 4)
|
66 |
-
}
|
67 |
-
logging.info(f"Probabilities: {prob_dict}")
|
68 |
-
return prob_dict
|
69 |
-
|
70 |
-
# Health check function
|
71 |
-
def health_check():
|
72 |
-
return "Healthy"
|
73 |
-
|
74 |
-
# Set flagging_dir to a writable directory
|
75 |
-
flagging_dir = "/tmp/flagged"
|
76 |
-
os.makedirs(flagging_dir, exist_ok=True)
|
77 |
-
|
78 |
-
iface = gr.Interface(
|
79 |
-
fn=classify_text,
|
80 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
81 |
-
outputs=gr.Label(),
|
82 |
-
title="BERT Classifier for Surprises",
|
83 |
-
description="Enter text to get probabilities for positive surprise, negative surprise, or no surprise",
|
84 |
-
flagging_dir=flagging_dir
|
85 |
-
)
|
86 |
|
87 |
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
+
def greet(name):
|
4 |
+
return f"Hello {name}!"
|
|
|
|
|
5 |
|
6 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
iface.launch(share=True)
|