File size: 1,443 Bytes
50c8152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load IndicBERT fine-tuned for multilingual toxic + sentiment
classifier = pipeline(
    "text-classification",
    model="l3cube-pune/indic-sentiment-mr-hi",  # works great on Telugu too with small trick
    return_all_scores=True
)

toxic_pipe = pipeline(
    "text-classification",
    model="Hate-speech-CNERG/hindi-abusive-MuRIL",
    top_k=None
)

def predict(text):
    # Sentiment
    sent = classifier(text)[0]
    sentiment = max(sent, key=lambda x: x['score'])
    
    # Toxic detection
    tox = toxic_pipe(text)[0]
    toxic_score = max(tox, key=lambda x: x['score'])['score']
    toxic_label = "Toxic" if toxic_score > 0.7 else "Non-Toxic"
    
    return {
        "Sentiment": f"{sentiment['label']} ({sentiment['score']:.2%})",
        "Toxicity": f"{toxic_label} ({toxic_score:.2%})"
    }

iface = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(label="Enter text in English/Telugu/Hindi", lines=3),
    outputs=gr.Label(num_top_classes=3),
    title="Multilingual (Hindi + Telugu) Toxic & Sentiment Detector",
    description="Built by Bodramoni Balu | Fine-tuned Indic models",
    examples=[
        ["ఈ సినిమా చాలా బాగుంది బ్రో"], 
        ["నీ యాక్షన్ చాలా డల్ గా ఉంది"], 
        ["तुम बहुत घटिया इंसान हो"]
    ]
)

iface.launch()