Spaces:
Sleeping
Sleeping
File size: 1,069 Bytes
f986847 d7e59e7 54a5305 f986847 363fc30 f986847 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import torch
from transformers import BertTokenizer, BertForSequenceClassification
import gradio as gr
# Load model from local folder
model = BertForSequenceClassification.from_pretrained("bert-expense-classifier/bert-expense-classifier", trust_remote_code=True)
tokenizer = BertTokenizer.from_pretrained("bert-expense-classifier/bert-expense-classifier")
model.eval()
label_map = {0: "statement", 1: "query"}
def classify_sentence(text):
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
with torch.no_grad():
outputs = model(**inputs)
predicted_class = torch.argmax(outputs.logits, dim=1).item()
return label_map[predicted_class]
interface = gr.Interface(
fn=classify_sentence,
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
outputs=gr.Textbox(label="Prediction"),
title="Expense Sentence Classifier",
description="Classifies whether a sentence is a user question or a statement for an expense tracker."
)
if __name__ == "__main__":
interface.launch()
|