aman2105 commited on
Commit
09bf646
·
verified ·
1 Parent(s): 44c4c6b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
4
+ # -----------------------------------------------------------
5
+ # SETUP: Hugging Face API and Models
6
+ # -----------------------------------------------------------
7
+ HUGGINGFACE_API_KEY = "your_huggingface_api_key" # Replace with your API key from https://huggingface.co/settings/tokens
8
+ HEADERS = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"}
9
+
10
+ # Hugging Face Model Endpoints (Replace with your fine-tuned models if needed)
11
+ CLASSIFIER_API_URL = "https://api-inference.huggingface.co/models/unitary/unbiased-toxic-roberta"
12
+ GENERATOR_API_URL = "https://api-inference.huggingface.co/models/facebook/blenderbot-3B"
13
+
14
+ # -----------------------------------------------------------
15
+ # FUNCTION DEFINITIONS
16
+ # -----------------------------------------------------------
17
+ def detect_harmful_content(text):
18
+ """
19
+ Detect harmful content in the input text using Hugging Face API.
20
+ """
21
+ payload = {"inputs": text}
22
+ response = requests.post(CLASSIFIER_API_URL, headers=HEADERS, json=payload)
23
+
24
+ if response.status_code != 200:
25
+ return [{"category": "Error", "score": 0, "message": "Failed to fetch response"}]
26
+
27
+ results = response.json()
28
+
29
+ detected = []
30
+ threshold = 0.8 # Set confidence threshold
31
+ for result in results:
32
+ if result.get('score', 0) >= threshold:
33
+ detected.append({"category": result.get('label', 'Unknown'), "score": result.get('score', 0)})
34
+
35
+ return detected
36
+
37
+ def generate_mitigation_response(text, detected_categories):
38
+ """
39
+ Generate a moderation response based on detected harmful categories.
40
+ """
41
+ if not detected_categories:
42
+ return "✅ Content appears safe. No harmful content detected."
43
+
44
+ categories_str = ", ".join([cat["category"] for cat in detected_categories])
45
+ prompt = (f"The following content has been flagged for {categories_str}:\n\n"
46
+ f"\"{text}\"\n\n"
47
+ "Please generate a respectful and informative moderation response.")
48
+
49
+ payload = {"inputs": prompt, "parameters": {"max_length": 150}}
50
+ response = requests.post(GENERATOR_API_URL, headers=HEADERS, json=payload)
51
+
52
+ if response.status_code != 200:
53
+ return "⚠️ Error: Could not generate a response."
54
+
55
+ generated = response.json()
56
+ return generated[0].get('generated_text', "No response generated.")
57
+
58
+ # -----------------------------------------------------------
59
+ # STREAMLIT USER INTERFACE
60
+ # -----------------------------------------------------------
61
+ st.title("🔍 AI-Powered Hate Speech Detection & Mitigation")
62
+ st.markdown("Detects hate speech, misinformation, and cyberbullying in social media posts.")
63
+
64
+ user_input = st.text_area("✏️ Enter the text to analyze:")
65
+
66
+ if st.button("Analyze"):
67
+ if user_input.strip() == "":
68
+ st.error("⚠️ Please enter some text to analyze.")
69
+ else:
70
+ st.markdown("### 📊 Analysis Results")
71
+ detected = detect_harmful_content(user_input)
72
+
73
+ if detected and detected[0].get("category") != "Error":
74
+ for d in detected:
75
+ st.write(f"**Category:** {d['category']} | **Confidence:** {d['score']:.2f}")
76
+ else:
77
+ st.write("✅ No harmful content detected.")
78
+
79
+ st.markdown("### 💡 Mitigation Response")
80
+ mitigation_response = generate_mitigation_response(user_input, detected)
81
+ st.write(mitigation_response)