Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,26 @@
|
|
1 |
import torch
|
2 |
-
from transformers import
|
3 |
import torch.nn.functional as F
|
4 |
|
5 |
-
def load_model(
|
6 |
-
#
|
7 |
-
model =
|
8 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
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 |
-
#
|
21 |
model.eval()
|
|
|
|
|
22 |
with torch.no_grad():
|
23 |
outputs = model(**inputs)
|
24 |
logits = outputs.logits
|
@@ -26,24 +28,26 @@ def predict(model, tokenizer, input_text):
|
|
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 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
# Example input
|
40 |
-
input_text = "
|
41 |
|
42 |
# Get predictions
|
43 |
predicted_class, probabilities = predict(model, tokenizer, input_text)
|
44 |
|
45 |
-
|
46 |
-
print("
|
|
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
main()
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
import torch.nn.functional as F
|
4 |
|
5 |
+
def load_model(model_directory):
|
6 |
+
# Assuming 'config.json' and 'pytorch_model.bin' are in 'model_directory'
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_directory)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_directory)
|
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 |
+
# Model in evaluation mode
|
21 |
model.eval()
|
22 |
+
|
23 |
+
# Make the model generate a prediction
|
24 |
with torch.no_grad():
|
25 |
outputs = model(**inputs)
|
26 |
logits = outputs.logits
|
|
|
28 |
# Convert logits to probabilities
|
29 |
probabilities = F.softmax(logits, dim=1)
|
30 |
|
31 |
+
# Get the predicted class and the probabilities
|
32 |
predicted_class = torch.argmax(probabilities, dim=1).cpu().numpy()
|
33 |
probabilities = probabilities.cpu().numpy()
|
34 |
|
35 |
return predicted_class, probabilities
|
36 |
|
37 |
def main():
|
38 |
+
# Replace 'your-model-directory' with the actual path to your model directory
|
39 |
+
model_directory = "Kurkur99/transactionmerchant/model_directory" # e.g., "Kurkur99/Kurkur99/transactionmerchant/model_directory"
|
40 |
+
model, tokenizer = load_model(model_directory)
|
41 |
|
42 |
+
# Example input text
|
43 |
+
input_text = "Example input text for prediction"
|
44 |
|
45 |
# Get predictions
|
46 |
predicted_class, probabilities = predict(model, tokenizer, input_text)
|
47 |
|
48 |
+
# Output the results
|
49 |
+
print(f"Predicted Class: {predicted_class[0]}")
|
50 |
+
print(f"Probabilities: {probabilities[0]}")
|
51 |
|
52 |
if __name__ == "__main__":
|
53 |
main()
|