Update app.py
Browse files
app.py
CHANGED
@@ -26,11 +26,15 @@ def detect_harmful_content(text):
|
|
26 |
|
27 |
results = response.json()
|
28 |
|
|
|
|
|
|
|
29 |
detected = []
|
30 |
threshold = 0.8 # Set confidence threshold
|
31 |
for result in results:
|
32 |
-
if result
|
33 |
-
|
|
|
34 |
|
35 |
return detected
|
36 |
|
@@ -50,10 +54,15 @@ def generate_mitigation_response(text, detected_categories):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# -----------------------------------------------------------
|
59 |
# STREAMLIT USER INTERFACE
|
@@ -79,3 +88,4 @@ if st.button("Analyze"):
|
|
79 |
st.markdown("### 💡 Mitigation Response")
|
80 |
mitigation_response = generate_mitigation_response(user_input, detected)
|
81 |
st.write(mitigation_response)
|
|
|
|
26 |
|
27 |
results = response.json()
|
28 |
|
29 |
+
if not isinstance(results, list): # Handle unexpected response format
|
30 |
+
return [{"category": "Error", "score": 0, "message": "Invalid response format"}]
|
31 |
+
|
32 |
detected = []
|
33 |
threshold = 0.8 # Set confidence threshold
|
34 |
for result in results:
|
35 |
+
if isinstance(result, dict) and 'label' in result and 'score' in result:
|
36 |
+
if result['score'] >= threshold:
|
37 |
+
detected.append({"category": result['label'], "score": result['score']})
|
38 |
|
39 |
return detected
|
40 |
|
|
|
54 |
response = requests.post(GENERATOR_API_URL, headers=HEADERS, json=payload)
|
55 |
|
56 |
if response.status_code != 200:
|
57 |
+
return "⚠️ Error: Could not generate a response. Please try again later."
|
58 |
|
59 |
generated = response.json()
|
60 |
+
|
61 |
+
# Ensure response contains the generated text
|
62 |
+
if isinstance(generated, list) and len(generated) > 0 and 'generated_text' in generated[0]:
|
63 |
+
return generated[0]['generated_text']
|
64 |
+
else:
|
65 |
+
return "⚠️ No valid response generated. Please try again."
|
66 |
|
67 |
# -----------------------------------------------------------
|
68 |
# STREAMLIT USER INTERFACE
|
|
|
88 |
st.markdown("### 💡 Mitigation Response")
|
89 |
mitigation_response = generate_mitigation_response(user_input, detected)
|
90 |
st.write(mitigation_response)
|
91 |
+
|