Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +67 -15
hate_speech_demo.py
CHANGED
|
@@ -88,6 +88,7 @@ def process_retrieval_text(retrieval_text, user_input):
|
|
| 88 |
ORACLE_API_KEY = os.environ.get("ORACLE_API_KEY", "")
|
| 89 |
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY", "")
|
| 90 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
|
|
|
|
| 91 |
|
| 92 |
# Custom CSS for styling
|
| 93 |
CUSTOM_CSS = """
|
|
@@ -462,6 +463,43 @@ def get_openai_moderation(openai_client, user_input):
|
|
| 462 |
return formatted_result, safety_level
|
| 463 |
except Exception as e:
|
| 464 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
|
| 467 |
# Updated to only require one input
|
|
@@ -475,6 +513,8 @@ def rate_user_input(user_input):
|
|
| 475 |
llama_rating, llama_safety = get_llama_guard_rating(together_client, user_input)
|
| 476 |
contextual_rating, contextual_retrieval, contextual_safety = get_contextual_rating(contextual_api, user_input)
|
| 477 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
|
|
|
|
|
|
| 478 |
|
| 479 |
# Format responses carefully to avoid random line breaks
|
| 480 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
|
@@ -486,7 +526,8 @@ def rate_user_input(user_input):
|
|
| 486 |
# Format results with HTML styling
|
| 487 |
llama_html = f"""<div class="rating-box secondary-box {llama_safety}-rating">{llama_rating}</div>"""
|
| 488 |
openai_html = f"""<div class="rating-box secondary-box {openai_safety}-rating">{openai_rating}</div>"""
|
| 489 |
-
|
|
|
|
| 490 |
# Create the knowledge section (initially hidden) and button
|
| 491 |
knowledge_html = ""
|
| 492 |
knowledge_button = ""
|
|
@@ -517,7 +558,7 @@ def rate_user_input(user_input):
|
|
| 517 |
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 518 |
<a href="#" id="btn-{popup_id}" class="evidence-button"
|
| 519 |
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 520 |
-
Show supporting evidence
|
| 521 |
</a>
|
| 522 |
</div>
|
| 523 |
"""
|
|
@@ -532,7 +573,7 @@ def rate_user_input(user_input):
|
|
| 532 |
{knowledge_html}
|
| 533 |
"""
|
| 534 |
|
| 535 |
-
return contextual_html, llama_html, openai_html, ""
|
| 536 |
|
| 537 |
def random_test_case():
|
| 538 |
try:
|
|
@@ -804,6 +845,17 @@ def create_gradio_app():
|
|
| 804 |
</div>
|
| 805 |
""")
|
| 806 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 807 |
|
| 808 |
# Define show/hide loading indicator functions
|
| 809 |
def show_loading():
|
|
@@ -835,18 +887,18 @@ def create_gradio_app():
|
|
| 835 |
|
| 836 |
# Bind rating button with loading indicator
|
| 837 |
rate_btn.click(
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
|
| 851 |
return app
|
| 852 |
|
|
|
|
| 88 |
ORACLE_API_KEY = os.environ.get("ORACLE_API_KEY", "")
|
| 89 |
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY", "")
|
| 90 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
|
| 91 |
+
PERSPECTIVE_API_KEY = os.environ.get("PERSPECTIVE_API_KEY", "")
|
| 92 |
|
| 93 |
# Custom CSS for styling
|
| 94 |
CUSTOM_CSS = """
|
|
|
|
| 463 |
return formatted_result, safety_level
|
| 464 |
except Exception as e:
|
| 465 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 466 |
+
|
| 467 |
+
# Perspective API
|
| 468 |
+
|
| 469 |
+
def get_perspective_rating(api_key, user_input):
|
| 470 |
+
url = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'
|
| 471 |
+
params = {'key': api_key}
|
| 472 |
+
data = {
|
| 473 |
+
'comment': {'text': user_input},
|
| 474 |
+
'requestedAttributes': {
|
| 475 |
+
'TOXICITY': {}, 'SEVERE_TOXICITY': {}, 'IDENTITY_ATTACK': {},
|
| 476 |
+
'INSULT': {}, 'PROFANITY': {}, 'THREAT': {}, 'SEXUALLY_EXPLICIT': {}
|
| 477 |
+
}
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
try:
|
| 481 |
+
response = requests.post(url, params=params, data=json.dumps(data))
|
| 482 |
+
response.raise_for_status()
|
| 483 |
+
response_json = response.json()
|
| 484 |
+
|
| 485 |
+
scores = {
|
| 486 |
+
attr: response_json['attributeScores'][attr]['summaryScore']['value']
|
| 487 |
+
for attr in data['requestedAttributes']
|
| 488 |
+
if attr in response_json.get('attributeScores', {})
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
flagged_attrs = {k: v for k, v in scores.items() if v > 0.5}
|
| 492 |
+
|
| 493 |
+
if flagged_attrs:
|
| 494 |
+
rating_text = "Safety Status: Unsafe\nFlagged Attributes:\n"
|
| 495 |
+
for attr, val in flagged_attrs.items():
|
| 496 |
+
rating_text += f" - {attr}: {val:.2f}\n"
|
| 497 |
+
return rating_text, "unsafe"
|
| 498 |
+
else:
|
| 499 |
+
return "Safety Status: Safe", "safe"
|
| 500 |
+
|
| 501 |
+
except Exception as e:
|
| 502 |
+
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 503 |
|
| 504 |
|
| 505 |
# Updated to only require one input
|
|
|
|
| 513 |
llama_rating, llama_safety = get_llama_guard_rating(together_client, user_input)
|
| 514 |
contextual_rating, contextual_retrieval, contextual_safety = get_contextual_rating(contextual_api, user_input)
|
| 515 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
| 516 |
+
perspective_rating, perspective_safety = get_perspective_rating(PERSPECTIVE_API_KEY, user_input)
|
| 517 |
+
|
| 518 |
|
| 519 |
# Format responses carefully to avoid random line breaks
|
| 520 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
|
|
|
| 526 |
# Format results with HTML styling
|
| 527 |
llama_html = f"""<div class="rating-box secondary-box {llama_safety}-rating">{llama_rating}</div>"""
|
| 528 |
openai_html = f"""<div class="rating-box secondary-box {openai_safety}-rating">{openai_rating}</div>"""
|
| 529 |
+
perspective_html = f"""<div class="rating-box secondary-box {perspective_safety}-rating">{perspective_rating}</div>"""
|
| 530 |
+
|
| 531 |
# Create the knowledge section (initially hidden) and button
|
| 532 |
knowledge_html = ""
|
| 533 |
knowledge_button = ""
|
|
|
|
| 558 |
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 559 |
<a href="#" id="btn-{popup_id}" class="evidence-button"
|
| 560 |
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 561 |
+
📄 Show supporting evidence
|
| 562 |
</a>
|
| 563 |
</div>
|
| 564 |
"""
|
|
|
|
| 573 |
{knowledge_html}
|
| 574 |
"""
|
| 575 |
|
| 576 |
+
return contextual_html, llama_html, openai_html, perspective_html, ""
|
| 577 |
|
| 578 |
def random_test_case():
|
| 579 |
try:
|
|
|
|
| 845 |
</div>
|
| 846 |
""")
|
| 847 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 848 |
+
with gr.Column():
|
| 849 |
+
# Perspective API section
|
| 850 |
+
gr.HTML("""
|
| 851 |
+
<div>
|
| 852 |
+
<h3 class="result-header">🧪 Perspective API</h3>
|
| 853 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 854 |
+
<a href="https://developers.perspectiveapi.com/s/docs" target="_blank" class="knowledge-button">View docs</a>
|
| 855 |
+
</div>
|
| 856 |
+
</div>
|
| 857 |
+
""")
|
| 858 |
+
perspective_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 859 |
|
| 860 |
# Define show/hide loading indicator functions
|
| 861 |
def show_loading():
|
|
|
|
| 887 |
|
| 888 |
# Bind rating button with loading indicator
|
| 889 |
rate_btn.click(
|
| 890 |
+
show_loading,
|
| 891 |
+
inputs=None,
|
| 892 |
+
outputs=loading_spinner
|
| 893 |
+
).then(
|
| 894 |
+
rate_user_input,
|
| 895 |
+
inputs=[user_input],
|
| 896 |
+
outputs=[contextual_results, llama_results, openai_results, perspective_results]
|
| 897 |
+
).then(
|
| 898 |
+
hide_loading,
|
| 899 |
+
inputs=None,
|
| 900 |
+
outputs=loading_spinner
|
| 901 |
+
)
|
| 902 |
|
| 903 |
return app
|
| 904 |
|