Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
# Load Pretrained Model & Tokenizer (XLM-Roberta for multilingual text classification)
|
6 |
+
MODEL_NAME = "xlm-roberta-base"
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=5)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
9 |
+
|
10 |
+
# Classification Function
|
11 |
+
def classify_text(text):
|
12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
label = torch.argmax(outputs.logits, dim=1).item()
|
16 |
+
return f"Predicted Category: {label}"
|
17 |
+
|
18 |
+
# Gradio UI
|
19 |
+
demo = gr.Interface(fn=classify_text, inputs=gr.Textbox(lines=2, placeholder="Enter business document text..."),
|
20 |
+
outputs="text", title="Multilingual Business Document Classifier")
|
21 |
+
|
22 |
+
demo.launch()
|