Kurkur99 commited on
Commit
47241b4
·
verified ·
1 Parent(s): 807fd7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
+
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/modeling" # 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()