alveKamruzzaman's picture
Upload app.py
741e25f verified
Raw
History Blame
1.87 kB
import gradio as gr
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
# Load the model and tokenizer from Hugging Face
model_name = "TextLabRUET/xlm-r_based_bangla_sentence_classifier"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Mapping predicted class to Bangla sentence types
class_mapping = {
0: "Assertive Sentence (বর্ণনামূলক বাক্য)",
1: "Interrogative Sentence (প্রশ্নবোধক বাক্য)",
2: "Imperative Sentence (অনুজ্ঞাসূচক বাক্য)",
3: "Optative Sentence (প্রার্থনা সূচক বাক্য)",
4: "Exclamatory Sentence (বিস্ময়সূচক বাক্য)"
}
# Function for prediction
def predict(sentence):
inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=128)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class = torch.argmax(logits, dim=1).item()
sentence_type = class_mapping.get(predicted_class, "Unknown Sentence Type")
return f"Predicted Class: {sentence_type}"
# Create Gradio UI
iface = gr.Interface(
fn=predict,
inputs=gr.Textbox(lines=2, placeholder="Enter a Bangla sentence..."),
outputs="text",
title="Bangla Sentence Classifier",
description=(
"This model was trained on a curated Bangla dataset by **TextLab RUET**. "
"It classifies Bangla sentences into five distinct categories: Assertive, Interrogative, Imperative, Optative, and Exclamatory "
"using the **XLM-R model**. Enter a Bangla sentence below to see how our model interprets it!"
),
theme="compact"
)
# Launch the Gradio app
iface.launch(share=True)