|
import streamlit as st |
|
import requests |
|
|
|
|
|
|
|
|
|
HUGGINGFACE_API_KEY = "your_huggingface_api_key" |
|
HEADERS = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"} |
|
|
|
|
|
CLASSIFIER_API_URL = "https://api-inference.huggingface.co/models/Hate-speech-CNERG/bert-base-uncased-hatexplain" |
|
GENERATOR_API_URL = "https://api-inference.huggingface.co/models/KAXY/GPT2-against-hate" |
|
|
|
|
|
|
|
|
|
def detect_harmful_content(text): |
|
"""Detects harmful content using a Hugging Face model.""" |
|
payload = {"inputs": text} |
|
response = requests.post(CLASSIFIER_API_URL, headers=HEADERS, json=payload) |
|
|
|
if response.status_code != 200: |
|
return [{"category": "Error", "score": 0, "message": "Failed to fetch response"}] |
|
|
|
results = response.json() |
|
|
|
detected = [] |
|
threshold = 0.5 |
|
for result in results: |
|
if result.get('score', 0) >= threshold: |
|
detected.append({"category": result.get('label', 'Unknown'), "score": result.get('score', 0)}) |
|
|
|
return detected |
|
|
|
def generate_mitigation_response(text, detected_categories): |
|
"""Generates a mitigation response based on detected harmful speech.""" |
|
if not detected_categories: |
|
return "β
Content appears safe. No harmful content detected." |
|
|
|
categories_str = ", ".join([cat["category"] for cat in detected_categories]) |
|
prompt = (f"The following content has been flagged for {categories_str}:\n\n" |
|
f"\"{text}\"\n\n" |
|
"Please generate a respectful and informative moderation response.") |
|
|
|
payload = {"inputs": prompt, "parameters": {"max_length": 150}} |
|
response = requests.post(GENERATOR_API_URL, headers=HEADERS, json=payload) |
|
|
|
if response.status_code != 200: |
|
return "β οΈ Error: Could not generate a response." |
|
|
|
generated = response.json() |
|
return generated[0].get('generated_text', "No response generated.") |
|
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Hate Speech Detector", layout="centered") |
|
|
|
st.title("π AI-Powered Hate Speech Detection & Mitigation") |
|
st.markdown("Detects **hate speech, misinformation, and cyberbullying** in social media posts.") |
|
|
|
|
|
user_input = st.text_area("βοΈ Enter the text to analyze:", height=150) |
|
|
|
if st.button("Analyze"): |
|
if user_input.strip() == "": |
|
st.error("β οΈ Please enter some text to analyze.") |
|
else: |
|
st.markdown("### π Analysis Results") |
|
detected = detect_harmful_content(user_input) |
|
|
|
if detected and detected[0].get("category") != "Error": |
|
for d in detected: |
|
st.write(f"**Category:** {d['category']} | **Confidence:** {d['score']:.2f}") |
|
else: |
|
st.write("β
No harmful content detected.") |
|
|
|
st.markdown("### π‘ Mitigation Response") |
|
mitigation_response = generate_mitigation_response(user_input, detected) |
|
st.write(mitigation_response) |
|
|
|
|
|
|