Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer from Hugging Face
|
| 6 |
+
model_name = "TextLabRUET/xlm-r_based_bangla_sentence_classifier"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Mapping predicted class to Bangla sentence types
|
| 11 |
+
class_mapping = {
|
| 12 |
+
0: "Assertive Sentence (বর্ণনামূলক বাক্য)",
|
| 13 |
+
1: "Interrogative Sentence (প্রশ্নবোধক বাক্য)",
|
| 14 |
+
2: "Imperative Sentence (অনুজ্ঞাসূচক বাক্য)",
|
| 15 |
+
3: "Optative Sentence (প্রার্থনা সূচক বাক্য)",
|
| 16 |
+
4: "Exclamatory Sentence (বিস্ময়সূচক বাক্য)"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Function for prediction
|
| 20 |
+
def predict(sentence):
|
| 21 |
+
inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
logits = outputs.logits
|
| 25 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 26 |
+
|
| 27 |
+
sentence_type = class_mapping.get(predicted_class, "Unknown Sentence Type")
|
| 28 |
+
return f"Predicted Class: {sentence_type}"
|
| 29 |
+
|
| 30 |
+
# Create Gradio UI
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=predict,
|
| 33 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a Bangla sentence..."),
|
| 34 |
+
outputs="text",
|
| 35 |
+
title="Bangla Sentence Classifier",
|
| 36 |
+
description=(
|
| 37 |
+
"This model was trained on a curated Bangla dataset by **TextLab RUET**. "
|
| 38 |
+
"It classifies Bangla sentences into five distinct categories: Assertive, Interrogative, Imperative, Optative, and Exclamatory "
|
| 39 |
+
"using the **XLM-R model**. Enter a Bangla sentence below to see how our model interprets it!"
|
| 40 |
+
),
|
| 41 |
+
theme="compact"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Launch the Gradio app
|
| 45 |
+
iface.launch(share=True)
|