himanshubeniwal commited on
Commit
f884fa6
·
verified ·
1 Parent(s): 0917c9f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # Load the model and tokenizer
6
+ model_name = "himanshubeniwal/bert_lf_bond"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ def predict_bond(text):
11
+ # Tokenize the input text
12
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
13
+
14
+ # Get model prediction
15
+ with torch.no_grad():
16
+ outputs = model(**inputs)
17
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
18
+ predicted_class = torch.argmax(predictions).item()
19
+ confidence = predictions[0][predicted_class].item()
20
+
21
+ # Get the label mapping (you may need to adjust these based on your model's specific labels)
22
+ labels = ["Negative", "Positive"] # Replace with your actual class labels
23
+
24
+ predicted_label = labels[predicted_class]
25
+ confidence_percentage = f"{confidence * 100:.2f}%"
26
+
27
+ return {
28
+ "Predicted Class": predicted_label,
29
+ "Confidence": confidence_percentage
30
+ }
31
+
32
+ # Create the Gradio interface
33
+ iface = gr.Interface(
34
+ fn=predict_bond,
35
+ inputs=gr.Textbox(lines=5, label="Enter bond-related text"),
36
+ outputs=gr.JSON(label="Prediction Results"),
37
+ title="James Bond Classification Model",
38
+ description="This model classifies bond-related text using a fine-tuned BERT model.",
39
+ examples=[
40
+ ["I love the James Bond movies!"],
41
+ ["I hate the James Bond movies. "]
42
+ ]
43
+ )
44
+
45
+ # Launch the interface
46
+ if __name__ == "__main__":
47
+ iface.launch()