Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +142 -131
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 = """
|
|
@@ -441,8 +442,61 @@ def get_openai_moderation(openai_client, user_input):
|
|
| 441 |
return formatted_result, safety_level
|
| 442 |
except Exception as e:
|
| 443 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
|
| 445 |
|
|
|
|
| 446 |
# Updated to only require one input
|
| 447 |
def rate_user_input(user_input):
|
| 448 |
# Initialize APIs with hardcoded keys
|
|
@@ -454,6 +508,7 @@ def rate_user_input(user_input):
|
|
| 454 |
llama_rating, llama_safety = get_llama_guard_rating(together_client, user_input)
|
| 455 |
contextual_rating, contextual_retrieval, contextual_safety = get_contextual_rating(contextual_api, user_input)
|
| 456 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
|
|
|
| 457 |
|
| 458 |
# Format responses carefully to avoid random line breaks
|
| 459 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
|
@@ -465,6 +520,7 @@ def rate_user_input(user_input):
|
|
| 465 |
# Format results with HTML styling
|
| 466 |
llama_html = f"""<div class="rating-box secondary-box {llama_safety}-rating">{llama_rating}</div>"""
|
| 467 |
openai_html = f"""<div class="rating-box secondary-box {openai_safety}-rating">{openai_rating}</div>"""
|
|
|
|
| 468 |
|
| 469 |
# Create the knowledge section (initially hidden) and button
|
| 470 |
knowledge_html = ""
|
|
@@ -491,10 +547,10 @@ def rate_user_input(user_input):
|
|
| 491 |
</div>
|
| 492 |
"""
|
| 493 |
|
| 494 |
-
# Create a toggle button (positioned BELOW the contextual results)
|
| 495 |
knowledge_button = f"""
|
| 496 |
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 497 |
-
<a href="#" id="btn-{popup_id}" class="knowledge-button"
|
| 498 |
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 499 |
Show supporting evidence
|
| 500 |
</a>
|
|
@@ -511,7 +567,7 @@ def rate_user_input(user_input):
|
|
| 511 |
{knowledge_html}
|
| 512 |
"""
|
| 513 |
|
| 514 |
-
return contextual_html, llama_html, openai_html, ""
|
| 515 |
|
| 516 |
def random_test_case():
|
| 517 |
try:
|
|
@@ -540,94 +596,37 @@ def create_gradio_app():
|
|
| 540 |
)
|
| 541 |
|
| 542 |
# Add CSS for the policy popup and custom button color
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
.
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
margin-bottom: 15px;
|
| 575 |
-
border-bottom: 1px solid #eee;
|
| 576 |
-
padding-bottom: 10px;
|
| 577 |
-
}
|
| 578 |
-
|
| 579 |
-
.policy-popup-title {
|
| 580 |
-
font-weight: bold;
|
| 581 |
-
font-size: 18px;
|
| 582 |
-
}
|
| 583 |
-
|
| 584 |
-
.policy-popup-close {
|
| 585 |
-
background-color: #222222;
|
| 586 |
-
color: white;
|
| 587 |
-
border: none;
|
| 588 |
-
border-radius: 4px;
|
| 589 |
-
padding: 5px 10px;
|
| 590 |
-
cursor: pointer;
|
| 591 |
-
}
|
| 592 |
-
|
| 593 |
-
.policy-popup-close:hover {
|
| 594 |
-
background-color: #000000;
|
| 595 |
-
}
|
| 596 |
-
|
| 597 |
-
.policy-iframe-container {
|
| 598 |
-
flex: 1;
|
| 599 |
-
overflow: hidden;
|
| 600 |
-
}
|
| 601 |
-
|
| 602 |
-
.policy-iframe {
|
| 603 |
-
width: 100%;
|
| 604 |
-
height: 100%;
|
| 605 |
-
border: 1px solid #eee;
|
| 606 |
-
}
|
| 607 |
-
|
| 608 |
-
/* Fallback for when PDF can't be displayed in iframe */
|
| 609 |
-
.policy-fallback {
|
| 610 |
-
padding: 20px;
|
| 611 |
-
text-align: center;
|
| 612 |
-
}
|
| 613 |
-
|
| 614 |
-
.policy-fallback a {
|
| 615 |
-
display: inline-block;
|
| 616 |
-
margin-top: 15px;
|
| 617 |
-
padding: 10px 15px;
|
| 618 |
-
background-color: #FCA539;
|
| 619 |
-
color: #000000;
|
| 620 |
-
text-decoration: none;
|
| 621 |
-
border-radius: 4px;
|
| 622 |
-
font-weight: bold;
|
| 623 |
-
}
|
| 624 |
-
|
| 625 |
-
/* Custom gray button style */
|
| 626 |
-
.gray-button {
|
| 627 |
-
background-color: #c4c4c3 !important;
|
| 628 |
-
color: #000000 !important;
|
| 629 |
-
}
|
| 630 |
-
"""
|
| 631 |
|
| 632 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 633 |
# Add loading spinner
|
|
@@ -728,10 +727,10 @@ def create_gradio_app():
|
|
| 728 |
|
| 729 |
"## Contact info \n"
|
| 730 |
"Reach out to Bertie Vidgen, Head of Human Data at Contextual AI, to find out more or sign up as a design partner at bertie@contextual.ai \n"
|
| 731 |
-
"## SAFETY WARNING \n"
|
| 732 |
"Some of the randomly generated test cases contain hateful language that you might find offensive or upsetting."
|
| 733 |
)
|
| 734 |
-
|
| 735 |
with gr.Row():
|
| 736 |
with gr.Column(scale=1):
|
| 737 |
# Random test case button at the top
|
|
@@ -757,32 +756,44 @@ def create_gradio_app():
|
|
| 757 |
|
| 758 |
# Hidden placeholder for retrieved knowledge
|
| 759 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 786 |
|
| 787 |
# Define show/hide loading indicator functions
|
| 788 |
def show_loading():
|
|
@@ -814,18 +825,18 @@ def create_gradio_app():
|
|
| 814 |
|
| 815 |
# Bind rating button with loading indicator
|
| 816 |
rate_btn.click(
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
|
| 830 |
return app
|
| 831 |
|
|
|
|
| 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 = """
|
|
|
|
| 442 |
return formatted_result, safety_level
|
| 443 |
except Exception as e:
|
| 444 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 445 |
+
|
| 446 |
+
# Perspective API rating
|
| 447 |
+
def get_perspective_rating(user_input):
|
| 448 |
+
url = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'
|
| 449 |
+
api_key = PERSPECTIVE_API_KEY
|
| 450 |
+
|
| 451 |
+
params = {'key': api_key}
|
| 452 |
+
data = {
|
| 453 |
+
'comment': {'text': user_input},
|
| 454 |
+
'requestedAttributes': {
|
| 455 |
+
'TOXICITY': {},
|
| 456 |
+
'SEVERE_TOXICITY': {},
|
| 457 |
+
'IDENTITY_ATTACK': {},
|
| 458 |
+
'INSULT': {},
|
| 459 |
+
'PROFANITY': {},
|
| 460 |
+
'THREAT': {},
|
| 461 |
+
'SEXUALLY_EXPLICIT': {}
|
| 462 |
+
}
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
try:
|
| 466 |
+
start_time = time.time()
|
| 467 |
+
response = requests.post(url, params=params, json=data)
|
| 468 |
+
end_time = time.time()
|
| 469 |
+
|
| 470 |
+
response_json = response.json()
|
| 471 |
+
attribute_scores = response_json.get('attributeScores', {})
|
| 472 |
+
|
| 473 |
+
# Collect attributes with scores above 0.5
|
| 474 |
+
high_attributes = {}
|
| 475 |
+
for attr, data in attribute_scores.items():
|
| 476 |
+
score = data.get('summaryScore', {}).get('value', 0)
|
| 477 |
+
if score >= 0.5:
|
| 478 |
+
high_attributes[attr] = score
|
| 479 |
+
|
| 480 |
+
# Determine safety level
|
| 481 |
+
safety_level = "safe"
|
| 482 |
+
if high_attributes:
|
| 483 |
+
safety_level = "unsafe"
|
| 484 |
+
|
| 485 |
+
# Format the output
|
| 486 |
+
formatted_result = f"Safety Status: {'Unsafe' if high_attributes else 'Safe'}\n"
|
| 487 |
+
if high_attributes:
|
| 488 |
+
formatted_result += "Flagged Categories (≥ 0.5):\n"
|
| 489 |
+
for attr, score in high_attributes.items():
|
| 490 |
+
formatted_result += f" - {attr}: {score:.2f}\n"
|
| 491 |
+
else:
|
| 492 |
+
formatted_result += "Flagged Categories: None\n"
|
| 493 |
+
|
| 494 |
+
return formatted_result, safety_level
|
| 495 |
+
except Exception as e:
|
| 496 |
+
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 497 |
|
| 498 |
|
| 499 |
+
# Updated to only require one input
|
| 500 |
# Updated to only require one input
|
| 501 |
def rate_user_input(user_input):
|
| 502 |
# Initialize APIs with hardcoded keys
|
|
|
|
| 508 |
llama_rating, llama_safety = get_llama_guard_rating(together_client, user_input)
|
| 509 |
contextual_rating, contextual_retrieval, contextual_safety = get_contextual_rating(contextual_api, user_input)
|
| 510 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
| 511 |
+
perspective_rating, perspective_safety = get_perspective_rating(user_input)
|
| 512 |
|
| 513 |
# Format responses carefully to avoid random line breaks
|
| 514 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
|
|
|
| 520 |
# Format results with HTML styling
|
| 521 |
llama_html = f"""<div class="rating-box secondary-box {llama_safety}-rating">{llama_rating}</div>"""
|
| 522 |
openai_html = f"""<div class="rating-box secondary-box {openai_safety}-rating">{openai_rating}</div>"""
|
| 523 |
+
perspective_html = f"""<div class="rating-box secondary-box {perspective_safety}-rating">{perspective_rating}</div>"""
|
| 524 |
|
| 525 |
# Create the knowledge section (initially hidden) and button
|
| 526 |
knowledge_html = ""
|
|
|
|
| 547 |
</div>
|
| 548 |
"""
|
| 549 |
|
| 550 |
+
# Create a toggle button (positioned BELOW the contextual results) - Updated to match orange button styling
|
| 551 |
knowledge_button = f"""
|
| 552 |
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 553 |
+
<a href="#" id="btn-{popup_id}" class="knowledge-button orange-button"
|
| 554 |
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 555 |
Show supporting evidence
|
| 556 |
</a>
|
|
|
|
| 567 |
{knowledge_html}
|
| 568 |
"""
|
| 569 |
|
| 570 |
+
return contextual_html, llama_html, openai_html, perspective_html, ""
|
| 571 |
|
| 572 |
def random_test_case():
|
| 573 |
try:
|
|
|
|
| 596 |
)
|
| 597 |
|
| 598 |
# Add CSS for the policy popup and custom button color
|
| 599 |
+
CUSTOM_CSS = CUSTOM_CSS + """
|
| 600 |
+
/* Make safety warning text red */
|
| 601 |
+
.safety-warning-red {
|
| 602 |
+
color: #F44336 !important;
|
| 603 |
+
font-weight: bold;
|
| 604 |
+
}
|
| 605 |
+
|
| 606 |
+
/* Make knowledge button match orange button style */
|
| 607 |
+
.knowledge-button.orange-button {
|
| 608 |
+
background: #FCA539 !important;
|
| 609 |
+
color: #000000 !important;
|
| 610 |
+
font-weight: bold;
|
| 611 |
+
border-radius: 5px;
|
| 612 |
+
padding: 10px 15px;
|
| 613 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 614 |
+
transition: all 0.3s ease;
|
| 615 |
+
display: inline-block;
|
| 616 |
+
text-decoration: none;
|
| 617 |
+
}
|
| 618 |
+
|
| 619 |
+
.knowledge-button.orange-button:hover {
|
| 620 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 621 |
+
transform: translateY(-2px);
|
| 622 |
+
}
|
| 623 |
+
|
| 624 |
+
/* Make the third column of models narrower to fit 3 on one row */
|
| 625 |
+
.model-column {
|
| 626 |
+
max-width: 33% !important;
|
| 627 |
+
flex: 1 !important;
|
| 628 |
+
}
|
| 629 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
|
| 631 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 632 |
# Add loading spinner
|
|
|
|
| 727 |
|
| 728 |
"## Contact info \n"
|
| 729 |
"Reach out to Bertie Vidgen, Head of Human Data at Contextual AI, to find out more or sign up as a design partner at bertie@contextual.ai \n"
|
| 730 |
+
"## <span class='safety-warning-red'>SAFETY WARNING</span> \n"
|
| 731 |
"Some of the randomly generated test cases contain hateful language that you might find offensive or upsetting."
|
| 732 |
)
|
| 733 |
+
#Reach out to Aravind Mohan , Head of Data Science, to find out more or sign up as a design partner at aravind.mohan@contextual.ai
|
| 734 |
with gr.Row():
|
| 735 |
with gr.Column(scale=1):
|
| 736 |
# Random test case button at the top
|
|
|
|
| 756 |
|
| 757 |
# Hidden placeholder for retrieved knowledge
|
| 758 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 759 |
+
|
| 760 |
+
with gr.Row():
|
| 761 |
+
with gr.Column():
|
| 762 |
+
# LlamaGuard section with permanent model card link
|
| 763 |
+
gr.HTML("""
|
| 764 |
+
<div>
|
| 765 |
+
<h3 class="result-header">🦙 LlamaGuard 3.0</h3>
|
| 766 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 767 |
+
<a href="https://github.com/meta-llama/PurpleLlama/blob/main/Llama-Guard3/8B/MODEL_CARD.md"
|
| 768 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 769 |
+
</div>
|
| 770 |
+
</div>
|
| 771 |
+
""")
|
| 772 |
+
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 773 |
+
with gr.Column():
|
| 774 |
+
# OpenAI section with permanent model card link
|
| 775 |
+
gr.HTML("""
|
| 776 |
+
<div>
|
| 777 |
+
<h3 class="result-header">🧷 OpenAI Moderation</h3>
|
| 778 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 779 |
+
<a href="https://platform.openai.com/docs/guides/moderation"
|
| 780 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 781 |
+
</div>
|
| 782 |
+
</div>
|
| 783 |
+
""")
|
| 784 |
+
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 785 |
+
with gr.Column():
|
| 786 |
+
# Perspective API section with permanent model card link
|
| 787 |
+
gr.HTML("""
|
| 788 |
+
<div>
|
| 789 |
+
<h3 class="result-header">👁️ Perspective API</h3>
|
| 790 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 791 |
+
<a href="https://developers.perspectiveapi.com/s/about-the-api"
|
| 792 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 793 |
+
</div>
|
| 794 |
+
</div>
|
| 795 |
+
""")
|
| 796 |
+
perspective_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 797 |
|
| 798 |
# Define show/hide loading indicator functions
|
| 799 |
def show_loading():
|
|
|
|
| 825 |
|
| 826 |
# Bind rating button with loading indicator
|
| 827 |
rate_btn.click(
|
| 828 |
+
show_loading,
|
| 829 |
+
inputs=None,
|
| 830 |
+
outputs=loading_spinner
|
| 831 |
+
).then(
|
| 832 |
+
rate_user_input,
|
| 833 |
+
inputs=[user_input],
|
| 834 |
+
outputs=[contextual_results, llama_results, openai_results, perspective_results, retrieved_knowledge]
|
| 835 |
+
).then(
|
| 836 |
+
hide_loading,
|
| 837 |
+
inputs=None,
|
| 838 |
+
outputs=loading_spinner
|
| 839 |
+
)
|
| 840 |
|
| 841 |
return app
|
| 842 |
|