Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +134 -65
hate_speech_demo.py
CHANGED
|
@@ -318,6 +318,36 @@ textarea.svelte-1pie7s6 {
|
|
| 318 |
h1, h2, h3, h4, h5, h6, p, span, div, button, input, textarea, label {
|
| 319 |
font-family: 'All Round Gothic Demi', 'Poppins', sans-serif !important;
|
| 320 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
"""
|
| 322 |
|
| 323 |
# Contextual API class - UPDATED WITH NEW MODEL ID
|
|
@@ -441,6 +471,57 @@ 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
|
|
@@ -454,6 +535,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 +547,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 +574,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 +594,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:
|
|
@@ -727,62 +810,48 @@ def create_gradio_app():
|
|
| 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
|
| 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 |
-
|
| 738 |
-
random_test_btn = gr.Button("🎲 Random Test Case", elem_classes=["orange-button"])
|
| 739 |
-
|
| 740 |
-
# Rate Content button - moved above the input box with gray color
|
| 741 |
-
rate_btn = gr.Button("Rate Content", variant="primary", size="lg", elem_classes=["gray-button"])
|
| 742 |
-
|
| 743 |
-
# Input field below both buttons
|
| 744 |
-
user_input = gr.Textbox(label="Input content to rate:", placeholder="Type content to evaluate here...", lines=6)
|
| 745 |
-
|
| 746 |
-
with gr.Column(scale=2):
|
| 747 |
-
# Contextual Safety Oracle with policy button
|
| 748 |
gr.HTML("""
|
| 749 |
<div>
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 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,18 +883,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 |
|
|
|
|
| 318 |
h1, h2, h3, h4, h5, h6, p, span, div, button, input, textarea, label {
|
| 319 |
font-family: 'All Round Gothic Demi', 'Poppins', sans-serif !important;
|
| 320 |
}
|
| 321 |
+
|
| 322 |
+
/* Make safety warning text red */
|
| 323 |
+
.safety-warning-red {
|
| 324 |
+
color: #F44336 !important;
|
| 325 |
+
font-weight: bold;
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
/* Make knowledge button match orange button style */
|
| 329 |
+
.knowledge-button.orange-button {
|
| 330 |
+
background: #FCA539 !important;
|
| 331 |
+
color: #000000 !important;
|
| 332 |
+
font-weight: bold;
|
| 333 |
+
border-radius: 5px;
|
| 334 |
+
padding: 10px 15px;
|
| 335 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 336 |
+
transition: all 0.3s ease;
|
| 337 |
+
display: inline-block;
|
| 338 |
+
text-decoration: none;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
.knowledge-button.orange-button:hover {
|
| 342 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 343 |
+
transform: translateY(-2px);
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
/* Make the third column of models narrower to fit 3 on one row */
|
| 347 |
+
.model-column {
|
| 348 |
+
max-width: 33% !important;
|
| 349 |
+
flex: 1 !important;
|
| 350 |
+
}
|
| 351 |
"""
|
| 352 |
|
| 353 |
# Contextual API class - UPDATED WITH NEW MODEL ID
|
|
|
|
| 471 |
return formatted_result, safety_level
|
| 472 |
except Exception as e:
|
| 473 |
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 474 |
+
|
| 475 |
+
def get_perspective_rating(user_input):
|
| 476 |
+
url = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze'
|
| 477 |
+
api_key = PERSPECTIVE_API_KEY
|
| 478 |
+
|
| 479 |
+
params = {'key': api_key}
|
| 480 |
+
data = {
|
| 481 |
+
'comment': {'text': user_input},
|
| 482 |
+
'requestedAttributes': {
|
| 483 |
+
'TOXICITY': {},
|
| 484 |
+
'SEVERE_TOXICITY': {},
|
| 485 |
+
'IDENTITY_ATTACK': {},
|
| 486 |
+
'INSULT': {},
|
| 487 |
+
'PROFANITY': {},
|
| 488 |
+
'THREAT': {},
|
| 489 |
+
'SEXUALLY_EXPLICIT': {}
|
| 490 |
+
}
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
try:
|
| 494 |
+
start_time = time.time()
|
| 495 |
+
response = requests.post(url, params=params, json=data)
|
| 496 |
+
end_time = time.time()
|
| 497 |
+
|
| 498 |
+
response_json = response.json()
|
| 499 |
+
attribute_scores = response_json.get('attributeScores', {})
|
| 500 |
+
|
| 501 |
+
# Collect attributes with scores above 0.5
|
| 502 |
+
high_attributes = {}
|
| 503 |
+
for attr, data in attribute_scores.items():
|
| 504 |
+
score = data.get('summaryScore', {}).get('value', 0)
|
| 505 |
+
if score >= 0.5:
|
| 506 |
+
high_attributes[attr] = score
|
| 507 |
+
|
| 508 |
+
# Determine safety level
|
| 509 |
+
safety_level = "safe"
|
| 510 |
+
if high_attributes:
|
| 511 |
+
safety_level = "unsafe"
|
| 512 |
+
|
| 513 |
+
# Format the output
|
| 514 |
+
formatted_result = f"Safety Status: {'Unsafe' if high_attributes else 'Safe'}\n"
|
| 515 |
+
if high_attributes:
|
| 516 |
+
formatted_result += "Flagged Categories (≥ 0.5):\n"
|
| 517 |
+
for attr, score in high_attributes.items():
|
| 518 |
+
formatted_result += f" - {attr}: {score:.2f}\n"
|
| 519 |
+
else:
|
| 520 |
+
formatted_result += "Flagged Categories: None\n"
|
| 521 |
+
|
| 522 |
+
return formatted_result, safety_level
|
| 523 |
+
except Exception as e:
|
| 524 |
+
return f"Safety Status: Error\nError: {str(e)}", "unsafe"
|
| 525 |
|
| 526 |
|
| 527 |
# Updated to only require one input
|
|
|
|
| 535 |
llama_rating, llama_safety = get_llama_guard_rating(together_client, user_input)
|
| 536 |
contextual_rating, contextual_retrieval, contextual_safety = get_contextual_rating(contextual_api, user_input)
|
| 537 |
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input)
|
| 538 |
+
perspective_rating, perspective_safety = get_perspective_rating(user_input)
|
| 539 |
|
| 540 |
# Format responses carefully to avoid random line breaks
|
| 541 |
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
|
|
|
| 547 |
# Format results with HTML styling
|
| 548 |
llama_html = f"""<div class="rating-box secondary-box {llama_safety}-rating">{llama_rating}</div>"""
|
| 549 |
openai_html = f"""<div class="rating-box secondary-box {openai_safety}-rating">{openai_rating}</div>"""
|
| 550 |
+
perspective_html = f"""<div class="rating-box secondary-box {perspective_safety}-rating">{perspective_rating}</div>"""
|
| 551 |
|
| 552 |
# Create the knowledge section (initially hidden) and button
|
| 553 |
knowledge_html = ""
|
|
|
|
| 574 |
</div>
|
| 575 |
"""
|
| 576 |
|
| 577 |
+
# Create a toggle button (positioned BELOW the contextual results) - Updated to match orange button styling
|
| 578 |
knowledge_button = f"""
|
| 579 |
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 580 |
+
<a href="#" id="btn-{popup_id}" class="knowledge-button orange-button"
|
| 581 |
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 582 |
Show supporting evidence
|
| 583 |
</a>
|
|
|
|
| 594 |
{knowledge_html}
|
| 595 |
"""
|
| 596 |
|
| 597 |
+
return contextual_html, llama_html, openai_html, perspective_html, ""
|
| 598 |
|
| 599 |
def random_test_case():
|
| 600 |
try:
|
|
|
|
| 810 |
"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"
|
| 811 |
|
| 812 |
"## Contact info \n"
|
| 813 |
+
"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"
|
| 814 |
+
"## <span class='safety-warning-red'>SAFETY WARNING</span> \n"
|
| 815 |
"Some of the randomly generated test cases contain hateful language that you might find offensive or upsetting."
|
| 816 |
)
|
| 817 |
|
| 818 |
with gr.Row():
|
| 819 |
+
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 820 |
+
# LlamaGuard section with permanent model card link
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
gr.HTML("""
|
| 822 |
<div>
|
| 823 |
+
<h3 class="result-header">🦙 LlamaGuard 3.0</h3>
|
| 824 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 825 |
+
<a href="https://github.com/meta-llama/PurpleLlama/blob/main/Llama-Guard3/8B/MODEL_CARD.md"
|
| 826 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 827 |
+
</div>
|
| 828 |
+
</div>
|
| 829 |
+
""")
|
| 830 |
+
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 831 |
+
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 832 |
+
# OpenAI section with permanent model card link
|
| 833 |
+
gr.HTML("""
|
| 834 |
+
<div>
|
| 835 |
+
<h3 class="result-header">🧷 OpenAI Moderation</h3>
|
| 836 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 837 |
+
<a href="https://platform.openai.com/docs/guides/moderation"
|
| 838 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 839 |
+
</div>
|
| 840 |
+
</div>
|
| 841 |
+
""")
|
| 842 |
+
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 843 |
+
with gr.Column(scale=1, elem_classes=["model-column"]):
|
| 844 |
+
# Perspective API section with permanent model card link
|
| 845 |
+
gr.HTML("""
|
| 846 |
+
<div>
|
| 847 |
+
<h3 class="result-header">👁️ Perspective API</h3>
|
| 848 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 849 |
+
<a href="https://developers.perspectiveapi.com/s/about-the-api"
|
| 850 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 851 |
+
</div>
|
| 852 |
+
</div>
|
| 853 |
+
""")
|
| 854 |
+
perspective_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
|
| 856 |
# Define show/hide loading indicator functions
|
| 857 |
def show_loading():
|
|
|
|
| 883 |
|
| 884 |
# Bind rating button with loading indicator
|
| 885 |
rate_btn.click(
|
| 886 |
+
show_loading,
|
| 887 |
+
inputs=None,
|
| 888 |
+
outputs=loading_spinner
|
| 889 |
+
).then(
|
| 890 |
+
rate_user_input,
|
| 891 |
+
inputs=[user_input],
|
| 892 |
+
outputs=[contextual_results, llama_results, openai_results, perspective_results, retrieved_knowledge]
|
| 893 |
+
).then(
|
| 894 |
+
hide_loading,
|
| 895 |
+
inputs=None,
|
| 896 |
+
outputs=loading_spinner
|
| 897 |
+
)
|
| 898 |
|
| 899 |
return app
|
| 900 |
|