Spaces:
Sleeping
Sleeping
Create app.py
Browse filesFetch the model weights from the hugging face model repo
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()
|