Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +118 -74
hate_speech_demo.py
CHANGED
|
@@ -533,75 +533,118 @@ def rate_user_input(user_input):
|
|
| 533 |
- OpenAI Moderation
|
| 534 |
- Google Perspective API
|
| 535 |
"""
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 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 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
</div>
|
| 581 |
-
|
| 582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
|
| 584 |
-
#
|
| 585 |
-
|
| 586 |
-
<div
|
| 587 |
-
<
|
| 588 |
-
|
| 589 |
-
Show Retrieved Knowledge
|
| 590 |
-
</a>
|
| 591 |
</div>
|
|
|
|
|
|
|
| 592 |
"""
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
{
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
{knowledge_html}
|
| 602 |
-
"""
|
| 603 |
-
|
| 604 |
-
return contextual_html, llama_html, openai_html, perspective_html, ""
|
| 605 |
|
| 606 |
def random_test_case():
|
| 607 |
try:
|
|
@@ -718,10 +761,12 @@ def create_gradio_app():
|
|
| 718 |
color: #000000 !important;
|
| 719 |
}
|
| 720 |
|
| 721 |
-
/*
|
| 722 |
-
.
|
| 723 |
-
|
| 724 |
-
|
|
|
|
|
|
|
| 725 |
}
|
| 726 |
"""
|
| 727 |
|
|
@@ -852,6 +897,8 @@ def create_gradio_app():
|
|
| 852 |
# Hidden placeholder for retrieved knowledge
|
| 853 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 854 |
|
|
|
|
|
|
|
| 855 |
with gr.Row():
|
| 856 |
with gr.Column():
|
| 857 |
# LlamaGuard section with permanent model card link
|
|
@@ -877,14 +924,11 @@ def create_gradio_app():
|
|
| 877 |
</div>
|
| 878 |
""")
|
| 879 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 880 |
-
|
| 881 |
-
# Add Perspective API section
|
| 882 |
-
with gr.Row():
|
| 883 |
with gr.Column():
|
| 884 |
-
# Perspective API section with
|
| 885 |
gr.HTML("""
|
| 886 |
<div>
|
| 887 |
-
<h3 class="result-header">🔍
|
| 888 |
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 889 |
<a href="https://developers.perspectiveapi.com/s/about-the-api"
|
| 890 |
target="_blank" class="knowledge-button">View API info</a>
|
|
|
|
| 533 |
- OpenAI Moderation
|
| 534 |
- Google Perspective API
|
| 535 |
"""
|
| 536 |
+
try:
|
| 537 |
+
# Initialize APIs with hardcoded keys
|
| 538 |
+
contextual_api = ContextualAPIUtils(api_key=ORACLE_API_KEY)
|
| 539 |
+
together_client = Together(api_key=TOGETHER_API_KEY)
|
| 540 |
+
openai_client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
| 541 |
+
|
| 542 |
+
# Skip empty input
|
| 543 |
+
if not user_input or user_input.strip() == "":
|
| 544 |
+
return (
|
| 545 |
+
'<div class="rating-box contextual-box empty-rating">Please enter content to rate</div>',
|
| 546 |
+
'<div class="rating-box secondary-box empty-rating">Please enter content to rate</div>',
|
| 547 |
+
'<div class="rating-box secondary-box empty-rating">Please enter content to rate</div>',
|
| 548 |
+
'<div class="rating-box secondary-box empty-rating">Please enter content to rate</div>',
|
| 549 |
+
""
|
| 550 |
+
)
|
| 551 |
+
|
| 552 |
+
# Get ratings
|
| 553 |
+
try:
|
| 554 |
+
llama_rating, llama_safety = get_llama_guard_rating(together_client, user_input, user_input)
|
| 555 |
+
except Exception as e:
|
| 556 |
+
llama_rating = f"Error getting LlamaGuard rating: {str(e)}"
|
| 557 |
+
llama_safety = "unsafe"
|
| 558 |
+
|
| 559 |
+
try:
|
| 560 |
+
contextual_rating, contextual_retrieval, contextual_safety = get_contextual_rating(contextual_api, user_input, user_input)
|
| 561 |
+
except Exception as e:
|
| 562 |
+
contextual_rating = f"Error getting Contextual rating: {str(e)}"
|
| 563 |
+
contextual_retrieval = ""
|
| 564 |
+
contextual_safety = "unsafe"
|
| 565 |
+
|
| 566 |
+
try:
|
| 567 |
+
openai_rating, openai_safety = get_openai_moderation(openai_client, user_input, user_input)
|
| 568 |
+
except Exception as e:
|
| 569 |
+
openai_rating = f"Error getting OpenAI rating: {str(e)}"
|
| 570 |
+
openai_safety = "unsafe"
|
| 571 |
+
|
| 572 |
+
try:
|
| 573 |
+
perspective_rating, perspective_safety = get_perspective_rating(user_input)
|
| 574 |
+
except Exception as e:
|
| 575 |
+
perspective_rating = f"Error getting Perspective API rating: {str(e)}"
|
| 576 |
+
perspective_safety = "unsafe"
|
| 577 |
+
|
| 578 |
+
# Format responses carefully to avoid random line breaks
|
| 579 |
+
llama_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', llama_rating)
|
| 580 |
+
contextual_rating = re.sub(r'\.(?=\s+[A-Z])', '.\n', contextual_rating)
|
| 581 |
+
|
| 582 |
+
# Process retrieval text to highlight keywords with better formatting
|
| 583 |
+
if contextual_retrieval:
|
| 584 |
+
try:
|
| 585 |
+
processed_retrieval = process_retrieval_text(contextual_retrieval, user_input)
|
| 586 |
+
except Exception as e:
|
| 587 |
+
processed_retrieval = f"Error processing retrieval text: {str(e)}"
|
| 588 |
+
else:
|
| 589 |
+
processed_retrieval = "No retrieval text found."
|
| 590 |
+
|
| 591 |
+
# Format results with HTML styling
|
| 592 |
+
llama_html = f"""<div class="rating-box secondary-box {llama_safety}-rating">{llama_rating}</div>"""
|
| 593 |
+
openai_html = f"""<div class="rating-box secondary-box {openai_safety}-rating">{openai_rating}</div>"""
|
| 594 |
+
perspective_html = f"""<div class="rating-box secondary-box {perspective_safety}-rating">{perspective_rating}</div>"""
|
| 595 |
+
|
| 596 |
+
# Create the knowledge section (initially hidden) and button
|
| 597 |
+
knowledge_html = ""
|
| 598 |
+
knowledge_button = ""
|
| 599 |
+
|
| 600 |
+
if processed_retrieval and processed_retrieval != "No retrieval text found.":
|
| 601 |
+
# Create unique ID for this instance
|
| 602 |
+
import uuid
|
| 603 |
+
popup_id = f"knowledge-popup-{uuid.uuid4().hex[:8]}"
|
| 604 |
+
|
| 605 |
+
# Create the popup div (initially hidden)
|
| 606 |
+
knowledge_html = f"""
|
| 607 |
+
<div id="{popup_id}" class="knowledge-popup" style="display: none;">
|
| 608 |
+
<div class="knowledge-popup-header">Retrieved Knowledge</div>
|
| 609 |
+
<button class="knowledge-popup-close"
|
| 610 |
+
onclick="this.parentElement.style.display='none';
|
| 611 |
+
document.getElementById('btn-{popup_id}').style.display='inline-block';
|
| 612 |
+
return false;">
|
| 613 |
+
Close
|
| 614 |
+
</button>
|
| 615 |
+
<div class="knowledge-popup-content">
|
| 616 |
+
{processed_retrieval}
|
| 617 |
+
</div>
|
| 618 |
</div>
|
| 619 |
+
"""
|
| 620 |
+
|
| 621 |
+
# Create a toggle button (positioned BELOW the contextual results)
|
| 622 |
+
knowledge_button = f"""
|
| 623 |
+
<div style="margin-top: 10px; margin-bottom: 5px;">
|
| 624 |
+
<a href="#" id="btn-{popup_id}" class="knowledge-button"
|
| 625 |
+
onclick="document.getElementById('{popup_id}').style.display='block'; this.style.display='none'; return false;">
|
| 626 |
+
Show Retrieved Knowledge
|
| 627 |
+
</a>
|
| 628 |
+
</div>
|
| 629 |
+
"""
|
| 630 |
|
| 631 |
+
# Format contextual results with HTML styling
|
| 632 |
+
contextual_html = f"""
|
| 633 |
+
<div class="rating-box contextual-box {contextual_safety}-rating">
|
| 634 |
+
<button class="copy-button" onclick="navigator.clipboard.writeText(this.parentElement.innerText.replace('Copy', ''))">Copy</button>
|
| 635 |
+
{contextual_rating}
|
|
|
|
|
|
|
| 636 |
</div>
|
| 637 |
+
{knowledge_button}
|
| 638 |
+
{knowledge_html}
|
| 639 |
"""
|
| 640 |
+
|
| 641 |
+
return contextual_html, llama_html, openai_html, perspective_html, ""
|
| 642 |
+
|
| 643 |
+
except Exception as e:
|
| 644 |
+
# Global error handler to prevent the entire function from failing
|
| 645 |
+
error_message = f"An error occurred: {str(e)}"
|
| 646 |
+
error_html = f'<div class="rating-box empty-rating">{error_message}</div>'
|
| 647 |
+
return error_html, error_html, error_html, error_html, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
|
| 649 |
def random_test_case():
|
| 650 |
try:
|
|
|
|
| 761 |
color: #000000 !important;
|
| 762 |
}
|
| 763 |
|
| 764 |
+
/* Adjusted secondary boxes */
|
| 765 |
+
.secondary-box {
|
| 766 |
+
min-height: 120px;
|
| 767 |
+
font-size: 0.9em;
|
| 768 |
+
overflow-y: auto;
|
| 769 |
+
max-height: 300px;
|
| 770 |
}
|
| 771 |
"""
|
| 772 |
|
|
|
|
| 897 |
# Hidden placeholder for retrieved knowledge
|
| 898 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 899 |
|
| 900 |
+
# All three other systems in one row
|
| 901 |
+
gr.Markdown("### Other Rating Systems")
|
| 902 |
with gr.Row():
|
| 903 |
with gr.Column():
|
| 904 |
# LlamaGuard section with permanent model card link
|
|
|
|
| 924 |
</div>
|
| 925 |
""")
|
| 926 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
|
|
|
|
|
|
|
|
|
| 927 |
with gr.Column():
|
| 928 |
+
# Perspective API section with info link
|
| 929 |
gr.HTML("""
|
| 930 |
<div>
|
| 931 |
+
<h3 class="result-header">🔍 Perspective API</h3>
|
| 932 |
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 933 |
<a href="https://developers.perspectiveapi.com/s/about-the-api"
|
| 934 |
target="_blank" class="knowledge-button">View API info</a>
|