Anurag3703 commited on
Commit
8becd4b
·
verified ·
1 Parent(s): 684acfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -51
app.py CHANGED
@@ -1,51 +1,55 @@
1
- import torch
2
- from transformers import AutoTokenizer ,AutoModelForSequenceClassification
3
- import gradio as gr
4
-
5
-
6
- model_name = "Anurag3703/bert-spam-classifier"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
-
10
- def classify_text(text):
11
-
12
- inputs = tokenizer(text, return_tensors="pt", truncation=True,padding=True,max_lennght=512)
13
-
14
- # Get predictions
15
- with torch.no_grad():
16
- outputs = model(**inputs)
17
- predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
18
-
19
- # Get probabilities
20
- ham_prob = predictions[0][0].item()
21
- spam_prob = predictions[0][1].item()
22
-
23
- return {
24
- "Not Spam (Ham)": ham_prob,
25
- "Spam": spam_prob
26
- }
27
-
28
- # Create Gradio interface
29
- demo = gr.Interface(
30
- fn=classify_text,
31
- inputs=gr.Textbox(
32
- lines=3,
33
- placeholder="Enter a message to classify...",
34
- label="Input Text"
35
- ),
36
- outputs=gr.Label(num_top_classes=2, label="Classification Results"),
37
- title="BERT Spam Classifier",
38
- description="Enter a text message to check if it's spam or not spam.",
39
- examples=[
40
- ["Win a free iPhone now! Click here!"],
41
- ["Hey, can we meet for coffee tomorrow?"],
42
- ["URGENT: Your account will be closed. Verify now!"],
43
- ["Thanks for the meeting today, let's catch up next week."]
44
- ],
45
- theme="soft"
46
- )
47
-
48
- if __name__ == "__main__":
49
- demo.launch()
50
-
51
-
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import os
5
+
6
+ # Load model and tokenizer with authentication
7
+ model_name = "Anurag3703/bert-spam-classifier"
8
+ token = os.environ.get("HF_TOKEN") # Get token from environment
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name, token=token)
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_name, token=token)
12
+
13
+
14
+ def classify_text(text):
15
+ """Classify text as spam or not spam"""
16
+ # Tokenize input
17
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
18
+
19
+ # Get predictions
20
+ with torch.no_grad():
21
+ outputs = model(**inputs)
22
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
23
+
24
+ # Get probabilities
25
+ ham_prob = predictions[0][0].item()
26
+ spam_prob = predictions[0][1].item()
27
+
28
+ return {
29
+ "Not Spam (Ham)": ham_prob,
30
+ "Spam": spam_prob
31
+ }
32
+
33
+
34
+ # Create Gradio interface
35
+ demo = gr.Interface(
36
+ fn=classify_text,
37
+ inputs=gr.Textbox(
38
+ lines=3,
39
+ placeholder="Enter a message to classify...",
40
+ label="Input Text"
41
+ ),
42
+ outputs=gr.Label(num_top_classes=2, label="Classification Results"),
43
+ title="BERT Spam Classifier",
44
+ description="Enter a text message to check if it's spam or not spam.",
45
+ examples=[
46
+ ["Win a free iPhone now! Click here!"],
47
+ ["Hey, can we meet for coffee tomorrow?"],
48
+ ["URGENT: Your account will be closed. Verify now!"],
49
+ ["Thanks for the meeting today, let's catch up next week."]
50
+ ],
51
+ theme="soft"
52
+ )
53
+
54
+ if __name__ == "__main__":
55
+ demo.launch()