Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +132 -143
hate_speech_demo.py
CHANGED
|
@@ -88,7 +88,6 @@ 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 |
-
PERSPECTIVE_API_KEY = os.environ.get("PERSPECTIVE_API_KEY", "")
|
| 92 |
|
| 93 |
# Custom CSS for styling
|
| 94 |
CUSTOM_CSS = """
|
|
@@ -442,61 +441,8 @@ def get_openai_moderation(openai_client, user_input):
|
|
| 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,7 +454,6 @@ def rate_user_input(user_input):
|
|
| 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,7 +465,6 @@ def rate_user_input(user_input):
|
|
| 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,10 +491,10 @@ def rate_user_input(user_input):
|
|
| 547 |
</div>
|
| 548 |
"""
|
| 549 |
|
| 550 |
-
# Create a toggle button (positioned BELOW the contextual results)
|
| 551 |
knowledge_button = f"""
|
| 552 |
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 553 |
-
<a href="#" id="btn-{popup_id}" class="knowledge-button
|
| 554 |
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 555 |
Show supporting evidence
|
| 556 |
</a>
|
|
@@ -567,7 +511,7 @@ def rate_user_input(user_input):
|
|
| 567 |
{knowledge_html}
|
| 568 |
"""
|
| 569 |
|
| 570 |
-
return contextual_html, llama_html, openai_html,
|
| 571 |
|
| 572 |
def random_test_case():
|
| 573 |
try:
|
|
@@ -596,37 +540,94 @@ def create_gradio_app():
|
|
| 596 |
)
|
| 597 |
|
| 598 |
# Add CSS for the policy popup and custom button color
|
| 599 |
-
|
| 600 |
-
/*
|
| 601 |
-
.
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
|
| 631 |
with gr.Blocks(title="Hate Speech Rating Oracle", theme=theme, css=custom_css) as app:
|
| 632 |
# Add loading spinner
|
|
@@ -726,11 +727,11 @@ def create_gradio_app():
|
|
| 726 |
"Our approach combines Contextual's state-of-the-art <a href='https://contextual.ai/blog/introducing-instruction-following-reranker/' target='_blank'>steerable reranker</a>, <a href='https://contextual.ai/blog/introducing-grounded-language-model/' target='_blank'>world's most grounded language model</a>, and <a href='https://contextual.ai/blog/combining-rag-and-specialization/' target='_blank'>tuning for agent specialization</a> to achieve superhuman performance in content evaluation tasks. This technology enables consistent, fine-grained assessments across any content type and format.\n\n"
|
| 727 |
|
| 728 |
"## Contact info \n"
|
| 729 |
-
"Reach out to
|
| 730 |
-
"##
|
| 731 |
"Some of the randomly generated test cases contain hateful language that you might find offensive or upsetting."
|
| 732 |
)
|
| 733 |
-
|
| 734 |
with gr.Row():
|
| 735 |
with gr.Column(scale=1):
|
| 736 |
# Random test case button at the top
|
|
@@ -756,44 +757,32 @@ def create_gradio_app():
|
|
| 756 |
|
| 757 |
# Hidden placeholder for retrieved knowledge
|
| 758 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 759 |
-
|
| 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 |
-
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,18 +814,18 @@ def create_gradio_app():
|
|
| 825 |
|
| 826 |
# Bind rating button with loading indicator
|
| 827 |
rate_btn.click(
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
).then(
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
).then(
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
)
|
| 840 |
|
| 841 |
return app
|
| 842 |
|
|
|
|
| 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 |
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 |
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 |
# 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 |
</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 |
{knowledge_html}
|
| 512 |
"""
|
| 513 |
|
| 514 |
+
return contextual_html, llama_html, openai_html, ""
|
| 515 |
|
| 516 |
def random_test_case():
|
| 517 |
try:
|
|
|
|
| 540 |
)
|
| 541 |
|
| 542 |
# Add CSS for the policy popup and custom button color
|
| 543 |
+
custom_css = CUSTOM_CSS + """
|
| 544 |
+
/* Policy preview popup styles */
|
| 545 |
+
.policy-popup {
|
| 546 |
+
display: none;
|
| 547 |
+
position: fixed;
|
| 548 |
+
top: 0;
|
| 549 |
+
left: 0;
|
| 550 |
+
width: 100%;
|
| 551 |
+
height: 100%;
|
| 552 |
+
background-color: rgba(0,0,0,0.7);
|
| 553 |
+
z-index: 1000;
|
| 554 |
+
justify-content: center;
|
| 555 |
+
align-items: center;
|
| 556 |
+
}
|
| 557 |
+
|
| 558 |
+
.policy-popup-content {
|
| 559 |
+
background-color: white;
|
| 560 |
+
width: 80%;
|
| 561 |
+
height: 80%;
|
| 562 |
+
border-radius: 8px;
|
| 563 |
+
padding: 20px;
|
| 564 |
+
position: relative;
|
| 565 |
+
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 566 |
+
display: flex;
|
| 567 |
+
flex-direction: column;
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
.policy-popup-header {
|
| 571 |
+
display: flex;
|
| 572 |
+
justify-content: space-between;
|
| 573 |
+
align-items: center;
|
| 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
|
|
|
|
| 727 |
"Our approach combines Contextual's state-of-the-art <a href='https://contextual.ai/blog/introducing-instruction-following-reranker/' target='_blank'>steerable reranker</a>, <a href='https://contextual.ai/blog/introducing-grounded-language-model/' target='_blank'>world's most grounded language model</a>, and <a href='https://contextual.ai/blog/combining-rag-and-specialization/' target='_blank'>tuning for agent specialization</a> to achieve superhuman performance in content evaluation tasks. This technology enables consistent, fine-grained assessments across any content type and format.\n\n"
|
| 728 |
|
| 729 |
"## Contact info \n"
|
| 730 |
+
"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 \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 |
|
| 758 |
# Hidden placeholder for retrieved knowledge
|
| 759 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 760 |
+
|
| 761 |
+
with gr.Row():
|
| 762 |
+
with gr.Column():
|
| 763 |
+
# LlamaGuard section with permanent model card link
|
| 764 |
+
gr.HTML("""
|
| 765 |
+
<div>
|
| 766 |
+
<h3 class="result-header">🦙 LlamaGuard 3.0</h3>
|
| 767 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 768 |
+
<a href="https://github.com/meta-llama/PurpleLlama/blob/main/Llama-Guard3/8B/MODEL_CARD.md"
|
| 769 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 770 |
+
</div>
|
| 771 |
+
</div>
|
| 772 |
+
""")
|
| 773 |
+
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 774 |
+
with gr.Column():
|
| 775 |
+
# OpenAI section with permanent model card link
|
| 776 |
+
gr.HTML("""
|
| 777 |
+
<div>
|
| 778 |
+
<h3 class="result-header">🧷 OpenAI Moderation</h3>
|
| 779 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 780 |
+
<a href="https://platform.openai.com/docs/guides/moderation"
|
| 781 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 782 |
+
</div>
|
| 783 |
+
</div>
|
| 784 |
+
""")
|
| 785 |
+
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 786 |
|
| 787 |
# Define show/hide loading indicator functions
|
| 788 |
def show_loading():
|
|
|
|
| 814 |
|
| 815 |
# Bind rating button with loading indicator
|
| 816 |
rate_btn.click(
|
| 817 |
+
show_loading,
|
| 818 |
+
inputs=None,
|
| 819 |
+
outputs=loading_spinner
|
| 820 |
+
).then(
|
| 821 |
+
rate_user_input,
|
| 822 |
+
inputs=[user_input],
|
| 823 |
+
outputs=[contextual_results, llama_results, openai_results, retrieved_knowledge]
|
| 824 |
+
).then(
|
| 825 |
+
hide_loading,
|
| 826 |
+
inputs=None,
|
| 827 |
+
outputs=loading_spinner
|
| 828 |
+
)
|
| 829 |
|
| 830 |
return app
|
| 831 |
|