Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +248 -91
hate_speech_demo.py
CHANGED
|
@@ -318,36 +318,6 @@ 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 |
-
/* 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,57 +441,6 @@ def get_openai_moderation(openai_client, user_input):
|
|
| 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,7 +454,6 @@ def rate_user_input(user_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,7 +465,6 @@ def rate_user_input(user_input):
|
|
| 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,10 +491,10 @@ def rate_user_input(user_input):
|
|
| 574 |
</div>
|
| 575 |
"""
|
| 576 |
|
| 577 |
-
# Create a toggle button (positioned BELOW the contextual results)
|
| 578 |
knowledge_button = f"""
|
| 579 |
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 580 |
-
<a href="#" id="btn-{popup_id}" class="knowledge-button
|
| 581 |
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 582 |
Show supporting evidence
|
| 583 |
</a>
|
|
@@ -594,7 +511,7 @@ def rate_user_input(user_input):
|
|
| 594 |
{knowledge_html}
|
| 595 |
"""
|
| 596 |
|
| 597 |
-
return contextual_html, llama_html, openai_html,
|
| 598 |
|
| 599 |
def random_test_case():
|
| 600 |
try:
|
|
@@ -622,13 +539,252 @@ def create_gradio_app():
|
|
| 622 |
border_color_primary="#E0E0E0"
|
| 623 |
)
|
| 624 |
|
| 625 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
# Add loading spinner
|
| 627 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 628 |
|
| 629 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
|
| 631 |
-
# Define show/hide loading indicator functions
|
| 632 |
def show_loading():
|
| 633 |
return """<script>
|
| 634 |
const spinner = document.getElementById('loading-spinner');
|
|
@@ -641,7 +797,7 @@ def create_gradio_app():
|
|
| 641 |
if (spinner) spinner.style.display = 'none';
|
| 642 |
</script>"""
|
| 643 |
|
| 644 |
-
#
|
| 645 |
random_test_btn.click(
|
| 646 |
show_loading,
|
| 647 |
inputs=None,
|
|
@@ -656,6 +812,7 @@ def create_gradio_app():
|
|
| 656 |
outputs=loading_spinner
|
| 657 |
)
|
| 658 |
|
|
|
|
| 659 |
rate_btn.click(
|
| 660 |
show_loading,
|
| 661 |
inputs=None,
|
|
@@ -663,7 +820,7 @@ def create_gradio_app():
|
|
| 663 |
).then(
|
| 664 |
rate_user_input,
|
| 665 |
inputs=[user_input],
|
| 666 |
-
outputs=[contextual_results, llama_results, openai_results,
|
| 667 |
).then(
|
| 668 |
hide_loading,
|
| 669 |
inputs=None,
|
|
|
|
| 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 |
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 |
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:
|
|
|
|
| 539 |
border_color_primary="#E0E0E0"
|
| 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
|
| 634 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 635 |
|
| 636 |
+
# Create a file component to serve the PDF (hidden from UI)
|
| 637 |
+
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 638 |
+
|
| 639 |
+
# Add policy popup HTML with improved PDF handling
|
| 640 |
+
policy_popup_html = """
|
| 641 |
+
<div id="policy-popup" class="policy-popup">
|
| 642 |
+
<div class="policy-popup-content">
|
| 643 |
+
<div class="policy-popup-header">
|
| 644 |
+
<div class="policy-popup-title">Hate Speech Policy</div>
|
| 645 |
+
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 646 |
+
</div>
|
| 647 |
+
<div class="policy-iframe-container">
|
| 648 |
+
<!-- Primary method: Try Google PDF Viewer -->
|
| 649 |
+
<iframe class="policy-iframe" id="policy-iframe"></iframe>
|
| 650 |
+
|
| 651 |
+
<!-- Fallback content if iframe fails -->
|
| 652 |
+
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
| 653 |
+
<p>The policy document couldn't be displayed in the preview.</p>
|
| 654 |
+
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
| 655 |
+
</div>
|
| 656 |
+
</div>
|
| 657 |
+
</div>
|
| 658 |
+
</div>
|
| 659 |
+
|
| 660 |
+
<script>
|
| 661 |
+
// Function to handle opening the policy popup
|
| 662 |
+
function openPolicyPopup() {
|
| 663 |
+
// Set PDF URL - this approach is more reliable with Gradio
|
| 664 |
+
const pdfFileName = "Hate Speech Policy.pdf";
|
| 665 |
+
|
| 666 |
+
// Try multiple approaches to display the PDF
|
| 667 |
+
// 1. Google PDF viewer (works in most cases)
|
| 668 |
+
const googleViewerUrl = "https://docs.google.com/viewer?embedded=true&url=";
|
| 669 |
+
|
| 670 |
+
// 2. Direct link as fallback
|
| 671 |
+
let directPdfUrl = "";
|
| 672 |
+
|
| 673 |
+
// Find the PDF link by looking for file links in the DOM
|
| 674 |
+
const links = document.querySelectorAll("a");
|
| 675 |
+
for (const link of links) {
|
| 676 |
+
if (link.href && link.href.includes(encodeURIComponent(pdfFileName))) {
|
| 677 |
+
directPdfUrl = link.href;
|
| 678 |
+
break;
|
| 679 |
+
}
|
| 680 |
+
}
|
| 681 |
+
|
| 682 |
+
// Set the iframe source if we found a link
|
| 683 |
+
const iframe = document.getElementById("policy-iframe");
|
| 684 |
+
const fallback = document.getElementById("policy-fallback");
|
| 685 |
+
const downloadLink = document.getElementById("policy-download-link");
|
| 686 |
+
|
| 687 |
+
if (directPdfUrl) {
|
| 688 |
+
// Try Google Viewer first
|
| 689 |
+
iframe.src = googleViewerUrl + encodeURIComponent(directPdfUrl);
|
| 690 |
+
iframe.style.display = "block";
|
| 691 |
+
fallback.style.display = "none";
|
| 692 |
+
|
| 693 |
+
// Set the download link
|
| 694 |
+
downloadLink.href = directPdfUrl;
|
| 695 |
+
|
| 696 |
+
// Provide fallback in case Google Viewer fails
|
| 697 |
+
iframe.onerror = function() {
|
| 698 |
+
iframe.style.display = "none";
|
| 699 |
+
fallback.style.display = "block";
|
| 700 |
+
};
|
| 701 |
+
} else {
|
| 702 |
+
// No direct URL found, show fallback
|
| 703 |
+
iframe.style.display = "none";
|
| 704 |
+
fallback.style.display = "block";
|
| 705 |
+
downloadLink.href = "#";
|
| 706 |
+
downloadLink.textContent = "PDF not available";
|
| 707 |
+
}
|
| 708 |
+
|
| 709 |
+
// Display the popup
|
| 710 |
+
document.getElementById('policy-popup').style.display = 'flex';
|
| 711 |
+
}
|
| 712 |
+
</script>
|
| 713 |
+
"""
|
| 714 |
+
|
| 715 |
+
gr.HTML(policy_popup_html)
|
| 716 |
+
|
| 717 |
+
gr.Markdown("# Hate Speech Rating Oracle [BETA]")
|
| 718 |
+
gr.Markdown(
|
| 719 |
+
"Assess whether user-generated social content contains hate speech using Contextual AI's State-of-the-Art Agentic RAG system. Classifications are steerable and explainable as they are based on a policy document rather than parametric knowledge! This app also returns ratings from LlamaGuard 3.0 and the OpenAI Moderation API for you to compare. This is a demo from Contextual AI researchers. Feedback is welcome as we work with design partners to bring this to production. \n"
|
| 720 |
+
"## Instructions \n"
|
| 721 |
+
"Enter user-generated content to receive an assessment from all three models. Or use our random test case generator to have it pre-filled. \n"
|
| 722 |
+
"## How it works\n"
|
| 723 |
+
"* **Document-Grounded Evaluations**: Every rating is directly tied to our <a href='#' onclick='openPolicyPopup(); return false;'>hate speech policy document</a>, which makes our system far superior to other solutions that lack transparent decision criteria.\n"
|
| 724 |
+
"* **Adaptable Policies**: The policy document serves as a starting point and can be easily adjusted to meet your specific requirements. As policies evolve, the system immediately adapts without requiring retraining.\n"
|
| 725 |
+
"* **Clear Rationales**: Each evaluation includes a detailed explanation referencing specific policy sections, allowing users to understand exactly why content was flagged or approved.\n"
|
| 726 |
+
"* **Continuous Improvement**: The system learns from feedback, addressing any misclassifications by improving retrieval accuracy over time.\n\n"
|
| 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
|
| 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 |
+
<h3 class="result-header">🌟 Contextual Safety Oracle</h3>
|
| 751 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 752 |
+
<a href="#" class="knowledge-button" onclick="openPolicyPopup(); return false;">View policy</a>
|
| 753 |
+
</div>
|
| 754 |
+
</div>
|
| 755 |
+
""")
|
| 756 |
+
contextual_results = gr.HTML('<div class="rating-box contextual-box empty-rating">Rating will appear here</div>')
|
| 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():
|
| 789 |
return """<script>
|
| 790 |
const spinner = document.getElementById('loading-spinner');
|
|
|
|
| 797 |
if (spinner) spinner.style.display = 'none';
|
| 798 |
</script>"""
|
| 799 |
|
| 800 |
+
# Bind random test case button with loading indicator
|
| 801 |
random_test_btn.click(
|
| 802 |
show_loading,
|
| 803 |
inputs=None,
|
|
|
|
| 812 |
outputs=loading_spinner
|
| 813 |
)
|
| 814 |
|
| 815 |
+
# Bind rating button with loading indicator
|
| 816 |
rate_btn.click(
|
| 817 |
show_loading,
|
| 818 |
inputs=None,
|
|
|
|
| 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,
|