Kurkur99 commited on
Commit
c86d5e7
·
verified ·
1 Parent(s): 0ad96f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -51
app.py CHANGED
@@ -1,67 +1,49 @@
1
- import gradio as gr
2
  import torch
3
- from transformers import BertForSequenceClassification, BertTokenizer
 
4
 
5
- # Load the tokenizer from Hugging Face
6
- token_model = "indolem/indobertweet-base-uncased"
7
- tokenizer = BertTokenizer.from_pretrained(token_model)
 
 
8
 
9
- # Define the model directory where your config.json and pytorch_model.bin are located
10
- model_directory = "model_directory" # Make sure this directory has config.json and pytorch_model.bin
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
- input_ids = inputs['input_ids'].to(device)
37
- attention_mask = inputs['attention_mask'].to(device)
 
38
 
39
- # Model in evaluation mode
40
  model.eval()
41
-
42
- # Make prediction
43
  with torch.no_grad():
44
- outputs = model(input_ids, attention_mask=attention_mask)
 
45
 
46
- # Extract logits and convert to probabilities
47
- logits = outputs[0]
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
- # Return the predicted class
54
- return f"Predicted Category: {predicted_class}"
 
55
 
56
- # Creating the Gradio interface
57
- iface = gr.Interface(
58
- fn=classify_transaction,
59
- inputs=gr.Textbox(lines=3, placeholder="Enter Transaction Notes Here", label="Transaction Notes"),
60
- outputs=gr.Text(label="Classification Result"),
61
- title="Transaction Category Classifier",
62
- description="Enter transaction notes to get the predicted category.",
63
- live=True # Update the output as soon as the input changes
64
- )
65
 
66
  if __name__ == "__main__":
67
- iface.launch()
 
 
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()