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