PVK-Varma commited on
Commit
46c6d70
·
verified ·
1 Parent(s): 3bf1a5e

Create app.py

Browse files

Fetch the model weights from the hugging face model repo

Files changed (1) hide show
  1. app.py +24 -0
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ from peft import PeftModel
4
+ import torch
5
+
6
+ # Load tokenizer from the uploaded HF repo
7
+ tokenizer = AutoTokenizer.from_pretrained("PVK-Varma/DistilBERT_Banking")
8
+
9
+ # Load base model and LoRA weights from HF repo
10
+ base_model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
11
+ model = PeftModel.from_pretrained(base_model, "PVK-Varma/DistilBERT_Banking")
12
+
13
+ def predict(text):
14
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
15
+ with torch.no_grad():
16
+ outputs = model(**inputs)
17
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
18
+ predicted_class_id = predictions.argmax().item()
19
+ confidence = predictions.max().item()
20
+ return f"Predicted class: {predicted_class_id}, Confidence: {confidence:.4f}"
21
+
22
+ # Create Gradio interface
23
+ iface = gr.Interface(fn=predict, inputs="text", outputs="text", title="DistilBERT Text Classifier")
24
+ iface.launch()