Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
from deep_translator import GoogleTranslator
|
| 6 |
+
|
| 7 |
+
# -------------------- Load Kannada Model & Tokenizer --------------------
|
| 8 |
+
MODEL_PATH = "Thilak118/indic-bert-toxicity-classifier_kannada"
|
| 9 |
+
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
model.to(device)
|
| 14 |
+
model.eval()
|
| 15 |
+
|
| 16 |
+
# -------------------- Translator (English → Kannada) --------------------
|
| 17 |
+
translator = GoogleTranslator(source="en", target="kn")
|
| 18 |
+
|
| 19 |
+
# -------------------- Clean Kannada Text --------------------
|
| 20 |
+
def clean_text(text):
|
| 21 |
+
text = re.sub(r"[^\u0C80-\u0CFF\s.,!?]", "", str(text))
|
| 22 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 23 |
+
return text
|
| 24 |
+
|
| 25 |
+
# -------------------- Transliterate / Translate to Kannada --------------------
|
| 26 |
+
def transliterate_to_kannada(text):
|
| 27 |
+
if text and text.strip():
|
| 28 |
+
try:
|
| 29 |
+
return translator.translate(text)
|
| 30 |
+
except Exception:
|
| 31 |
+
return "Translation failed"
|
| 32 |
+
return ""
|
| 33 |
+
|
| 34 |
+
# -------------------- Toxicity Prediction --------------------
|
| 35 |
+
def predict_toxicity(english_text):
|
| 36 |
+
kannada_text = transliterate_to_kannada(english_text)
|
| 37 |
+
|
| 38 |
+
if "failed" in kannada_text.lower():
|
| 39 |
+
return f"Transliterated Kannada Text: {kannada_text}\nPrediction: Failed"
|
| 40 |
+
|
| 41 |
+
cleaned_text = clean_text(kannada_text)
|
| 42 |
+
|
| 43 |
+
inputs = tokenizer(
|
| 44 |
+
cleaned_text,
|
| 45 |
+
return_tensors="pt",
|
| 46 |
+
padding=True,
|
| 47 |
+
truncation=True,
|
| 48 |
+
max_length=128
|
| 49 |
+
).to(device)
|
| 50 |
+
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
|
| 54 |
+
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 55 |
+
probs = torch.softmax(outputs.logits, dim=1)[0]
|
| 56 |
+
confidence = probs[prediction].item() * 100
|
| 57 |
+
|
| 58 |
+
label = "Toxic" if prediction == 0 else "Non-Toxic"
|
| 59 |
+
|
| 60 |
+
return (
|
| 61 |
+
f"Transliterated Kannada Text: {cleaned_text}\n"
|
| 62 |
+
f"Prediction: {label}\n"
|
| 63 |
+
f"Confidence: {confidence:.2f}%"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# -------------------- Gradio UI --------------------
|
| 67 |
+
with gr.Blocks(title="Kannada Text Toxicity Classifier (IndicBERT)") as demo:
|
| 68 |
+
gr.Markdown(
|
| 69 |
+
"""
|
| 70 |
+
# Kannada Text Toxicity Classifier (IndicBERT + Transliteration)
|
| 71 |
+
|
| 72 |
+
Enter Kannada text written in **English letters**
|
| 73 |
+
Example:
|
| 74 |
+
**`ninage ashtondu seen illa le`**
|
| 75 |
+
|
| 76 |
+
The app converts it into **Kannada script** and predicts toxicity.
|
| 77 |
+
"""
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column():
|
| 82 |
+
input_text = gr.Textbox(
|
| 83 |
+
label="Enter Kannada Text (English Transliteration)",
|
| 84 |
+
placeholder="e.g., ninage ashtondu seen illa le",
|
| 85 |
+
lines=2
|
| 86 |
+
)
|
| 87 |
+
with gr.Column():
|
| 88 |
+
transliterated_text = gr.Textbox(
|
| 89 |
+
label="Kannada Script (Preview)",
|
| 90 |
+
interactive=False,
|
| 91 |
+
lines=2
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
preview_btn = gr.Button("Preview Transliteration")
|
| 96 |
+
predict_btn = gr.Button("Predict Toxicity")
|
| 97 |
+
|
| 98 |
+
output_text = gr.Textbox(
|
| 99 |
+
label="Prediction Output",
|
| 100 |
+
interactive=False,
|
| 101 |
+
lines=5
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
preview_btn.click(
|
| 105 |
+
fn=transliterate_to_kannada,
|
| 106 |
+
inputs=input_text,
|
| 107 |
+
outputs=transliterated_text
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
predict_btn.click(
|
| 111 |
+
fn=predict_toxicity,
|
| 112 |
+
inputs=input_text,
|
| 113 |
+
outputs=output_text
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# -------------------- Launch App --------------------
|
| 117 |
+
demo.launch()
|