Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Hugging Face API Token (Set this in Hugging Face Spaces)
|
| 6 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 7 |
+
|
| 8 |
+
# Model endpoints for Hindi and Telugu toxicity detection
|
| 9 |
+
MODEL_ENDPOINTS = {
|
| 10 |
+
"Hindi": "https://api-inference.huggingface.co/models/LingoIITGN/mBERT_toxic_hindi",
|
| 11 |
+
"Telugu": "https://api-inference.huggingface.co/models/LingoIITGN/mBERT_toxic_telugu"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Function to send a request to the Hugging Face API
|
| 15 |
+
def get_toxicity_prediction(text, language):
|
| 16 |
+
if language not in MODEL_ENDPOINTS:
|
| 17 |
+
return "Error: Model not found for the selected language"
|
| 18 |
+
|
| 19 |
+
url = MODEL_ENDPOINTS[language]
|
| 20 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 21 |
+
payload = {"inputs": text}
|
| 22 |
+
|
| 23 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 24 |
+
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
result = response.json()[0] # Extract first prediction
|
| 27 |
+
toxicity_score = result["score"] * 100 # Convert to percentage
|
| 28 |
+
is_toxic = "Toxic" if result["label"].lower() == "toxic" else "Non-Toxic"
|
| 29 |
+
return f"Toxicity Score: {toxicity_score:.2f}%\nClassification: {is_toxic}"
|
| 30 |
+
else:
|
| 31 |
+
return f"Error: {response.text}"
|
| 32 |
+
|
| 33 |
+
# Define Gradio interface
|
| 34 |
+
with gr.Blocks() as app:
|
| 35 |
+
gr.Markdown("# 🛡️ ToxiGuard - Multilingual Toxicity Detector")
|
| 36 |
+
|
| 37 |
+
text_input = gr.Textbox(label="Enter your text")
|
| 38 |
+
language_dropdown = gr.Dropdown(choices=["Hindi", "Telugu"], label="Select Language", value="Hindi")
|
| 39 |
+
|
| 40 |
+
submit_button = gr.Button("Check Toxicity")
|
| 41 |
+
output_text = gr.Textbox(label="Result")
|
| 42 |
+
|
| 43 |
+
submit_button.click(fn=get_toxicity_prediction, inputs=[text_input, language_dropdown], outputs=output_text)
|
| 44 |
+
|
| 45 |
+
# Launch the app
|
| 46 |
+
app.launch()
|