Spaces:
Sleeping
Sleeping
File size: 1,510 Bytes
851216a | 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import gradio as gr
from transformers import pipeline
import json
# Load a zero-shot classification pipeline
classifier = pipeline("zero-shot-classification",
model="facebook/bart-large-mnli")
# Define expense categories
expense_categories = [
"Groceries", "Restaurants", "Coffee Shops", "Transportation",
"Utilities", "Entertainment", "Shopping", "Health", "Travel",
"Education", "Home Improvement", "Personal Care", "Gifts"
]
def categorize_expense(merchant_name, item_description=""):
"""Categorize an expense based on merchant name and optional item description"""
# Combine inputs for better context
input_text = f"{merchant_name} {item_description}".strip()
# Run zero-shot classification
result = classifier(
input_text,
expense_categories,
multi_label=False
)
# Get top 3 categories with their scores
top_categories = []
for category, score in zip(result['labels'][:3], result['scores'][:3]):
top_categories.append({"category": category, "confidence": float(score)})
return json.dumps(top_categories)
# Create interface
iface = gr.Interface(
fn=categorize_expense,
inputs=[
gr.Textbox(label="Merchant Name"),
gr.Textbox(label="Item Description (Optional)")
],
outputs=gr.Textbox(label="Categories"),
title="Reciply Expense Categorizer",
description="Categorize expenses based on merchant name and item description"
)
iface.launch()
|