Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,49 @@
|
|
1 |
-
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# Load the model
|
13 |
-
# If your weights are named differently, ensure the file is named pytorch_model.bin or modify the loading method
|
14 |
-
model = BertForSequenceClassification.from_pretrained(model_directory)
|
15 |
-
model.eval() # Set the model to evaluation mode
|
16 |
-
|
17 |
-
# Check if CUDA is available and set the device accordingly
|
18 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
19 |
-
model.to(device)
|
20 |
-
|
21 |
-
def classify_transaction(notes):
|
22 |
-
# Tokenize the input text
|
23 |
-
inputs = tokenizer.encode_plus(
|
24 |
-
notes,
|
25 |
-
None,
|
26 |
-
add_special_tokens=True,
|
27 |
-
max_length=256,
|
28 |
-
padding='max_length',
|
29 |
-
return_token_type_ids=False,
|
30 |
-
return_attention_mask=True,
|
31 |
-
truncation=True,
|
32 |
-
return_tensors='pt'
|
33 |
-
)
|
34 |
|
35 |
# Move tensors to the same device as the model
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
#
|
40 |
model.eval()
|
41 |
-
|
42 |
-
# Make prediction
|
43 |
with torch.no_grad():
|
44 |
-
outputs = model(
|
|
|
45 |
|
46 |
-
#
|
47 |
-
logits =
|
48 |
-
probabilities = torch.softmax(logits, dim=1)
|
49 |
|
50 |
# Get the predicted class
|
51 |
predicted_class = torch.argmax(probabilities, dim=1).cpu().numpy()
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
|
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
)
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
-
|
|
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
+
import torch.nn.functional as F
|
4 |
|
5 |
+
def load_model(model_name):
|
6 |
+
# Load model from Hugging Face Hub
|
7 |
+
model = AutoModel.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
return model, tokenizer
|
10 |
|
11 |
+
def predict(model, tokenizer, input_text):
|
12 |
+
# Preprocess the input
|
13 |
+
inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Move tensors to the same device as the model
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
model.to(device)
|
18 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
19 |
|
20 |
+
# Make the model generate a prediction
|
21 |
model.eval()
|
|
|
|
|
22 |
with torch.no_grad():
|
23 |
+
outputs = model(**inputs)
|
24 |
+
logits = outputs.logits
|
25 |
|
26 |
+
# Convert logits to probabilities
|
27 |
+
probabilities = F.softmax(logits, dim=1)
|
|
|
28 |
|
29 |
# Get the predicted class
|
30 |
predicted_class = torch.argmax(probabilities, dim=1).cpu().numpy()
|
31 |
+
probabilities = probabilities.cpu().numpy()
|
32 |
+
|
33 |
+
return predicted_class, probabilities
|
34 |
|
35 |
+
def main():
|
36 |
+
model_name = "your-huggingface-model-name" # Replace with your model's name
|
37 |
+
model, tokenizer = load_model(model_name)
|
38 |
|
39 |
+
# Example input
|
40 |
+
input_text = "Your input text goes here"
|
41 |
+
|
42 |
+
# Get predictions
|
43 |
+
predicted_class, probabilities = predict(model, tokenizer, input_text)
|
44 |
+
|
45 |
+
print("Predicted Class:", predicted_class)
|
46 |
+
print("Probabilities:", probabilities)
|
|
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
+
main()
|