Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- .gitattributes +2 -0
- Hate Speech Policy.pdf +3 -0
- ai_responses_demo.py +3 -3
- airesponses/.DS_Store +0 -0
- airesponses/Safety Oracle - AI Response Policy.pdf +3 -0
- airesponses/ai_responses_demo.py +239 -18
- hate_speech_demo.py +200 -10
.DS_Store
CHANGED
|
Binary files a/.DS_Store and b/.DS_Store differ
|
|
|
.gitattributes
CHANGED
|
@@ -34,3 +34,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
SST__n_3000__rated_20250331_112456.csv filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
SST__n_3000__rated_20250331_112456.csv filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
Hate[[:space:]]Speech[[:space:]]Policy.pdf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
airesponses/Safety[[:space:]]Oracle[[:space:]]-[[:space:]]AI[[:space:]]Response[[:space:]]Policy.pdf filter=lfs diff=lfs merge=lfs -text
|
Hate Speech Policy.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8d321118d6f435ca0a3988c0e76f8f04f1e86de058fe4cd3b1288ee8377f88ac
|
| 3 |
+
size 166193
|
ai_responses_demo.py
CHANGED
|
@@ -78,11 +78,11 @@ OPENAI_API_KEY = os.getenv("OPENAI_KEY", OPENAI_API_KEY)
|
|
| 78 |
def random_test_case():
|
| 79 |
try:
|
| 80 |
df = pd.read_csv("test_cases.csv")
|
| 81 |
-
if not {"
|
| 82 |
-
raise ValueError("CSV must contain '
|
| 83 |
|
| 84 |
sample = df.sample(1).iloc[0]
|
| 85 |
-
return sample["
|
| 86 |
except Exception as e:
|
| 87 |
return f"Error loading test cases: {e}", ""
|
| 88 |
|
|
|
|
| 78 |
def random_test_case():
|
| 79 |
try:
|
| 80 |
df = pd.read_csv("test_cases.csv")
|
| 81 |
+
if not {"user input", "response"}.issubset(df.columns):
|
| 82 |
+
raise ValueError("CSV must contain 'user input' and 'response' columns.")
|
| 83 |
|
| 84 |
sample = df.sample(1).iloc[0]
|
| 85 |
+
return sample["user input"], sample["response"]
|
| 86 |
except Exception as e:
|
| 87 |
return f"Error loading test cases: {e}", ""
|
| 88 |
|
airesponses/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
airesponses/Safety Oracle - AI Response Policy.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:57dfca817039ffb2aa8a0a70259a895a5b16b3dc2a19768cc89052f4e19d3bae
|
| 3 |
+
size 319498
|
airesponses/ai_responses_demo.py
CHANGED
|
@@ -74,14 +74,45 @@ ORACLE_API_KEY = os.getenv("ORACLE_KEY", ORACLE_API_KEY)
|
|
| 74 |
TOGETHER_API_KEY = os.getenv("TOGETHER_KEY", TOGETHER_API_KEY)
|
| 75 |
OPENAI_API_KEY = os.getenv("OPENAI_KEY", OPENAI_API_KEY)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Custom CSS for styling
|
| 87 |
CUSTOM_CSS = """
|
|
@@ -509,10 +540,6 @@ def rate_user_interaction(user_input, user_response):
|
|
| 509 |
"""
|
| 510 |
|
| 511 |
return contextual_html, llama_html, openai_html, ""
|
| 512 |
-
def random_test_case():
|
| 513 |
-
"""Pick a random test case from the TEST_CASES list"""
|
| 514 |
-
test_case = random.choice(TEST_CASES)
|
| 515 |
-
return test_case["input"], test_case["response"]
|
| 516 |
|
| 517 |
# Simplified Gradio app without complex history functionality
|
| 518 |
def create_gradio_app():
|
|
@@ -532,9 +559,178 @@ def create_gradio_app():
|
|
| 532 |
border_color_primary="#E0E0E0"
|
| 533 |
)
|
| 534 |
|
| 535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
# Add loading spinner
|
| 537 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
|
| 539 |
gr.Markdown(
|
| 540 |
"# AI Responses Policy Rating Oracle\n"
|
|
@@ -554,19 +750,44 @@ def create_gradio_app():
|
|
| 554 |
rate_btn = gr.Button("Rate Content", variant="primary", size="lg", elem_classes=["orange-button"])
|
| 555 |
|
| 556 |
with gr.Column(scale=2):
|
| 557 |
-
#
|
| 558 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
contextual_results = gr.HTML('<div class="rating-box contextual-box empty-rating">Rating will appear here</div>')
|
| 560 |
|
| 561 |
-
# Hidden placeholder for retrieved knowledge
|
| 562 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 563 |
|
| 564 |
with gr.Row():
|
| 565 |
with gr.Column():
|
| 566 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 568 |
with gr.Column():
|
| 569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 571 |
|
| 572 |
# Define show/hide loading indicator functions
|
|
|
|
| 74 |
TOGETHER_API_KEY = os.getenv("TOGETHER_KEY", TOGETHER_API_KEY)
|
| 75 |
OPENAI_API_KEY = os.getenv("OPENAI_KEY", OPENAI_API_KEY)
|
| 76 |
|
| 77 |
+
import pandas as pd
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def random_test_case():
|
| 81 |
+
try:
|
| 82 |
+
# Get the current directory where the script is running
|
| 83 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 84 |
+
csv_path = os.path.join(current_dir, "test cases.csv")
|
| 85 |
+
|
| 86 |
+
# Print the path for debugging
|
| 87 |
+
print(f"Looking for CSV at: {csv_path}")
|
| 88 |
+
|
| 89 |
+
# Check if file exists
|
| 90 |
+
if not os.path.exists(csv_path):
|
| 91 |
+
return f"Error: CSV file not found at {csv_path}", ""
|
| 92 |
+
|
| 93 |
+
# Load the CSV file with explicit encoding
|
| 94 |
+
df = pd.read_csv(csv_path, encoding='utf-8')
|
| 95 |
+
|
| 96 |
+
# Print diagnostic information
|
| 97 |
+
print(f"CSV loaded successfully. Shape: {df.shape}")
|
| 98 |
+
print(f"Columns found: {df.columns.tolist()}")
|
| 99 |
+
|
| 100 |
+
if "user input" not in df.columns or "response" not in df.columns:
|
| 101 |
+
return f"Error: CSV must have 'user input' and 'response' columns, found: {df.columns.tolist()}", ""
|
| 102 |
+
|
| 103 |
+
# Verify there's data to sample from
|
| 104 |
+
if len(df) == 0:
|
| 105 |
+
return "Error: CSV file contains no data rows", ""
|
| 106 |
+
|
| 107 |
+
# Sample a random row
|
| 108 |
+
sample = df.sample(1).iloc[0]
|
| 109 |
+
return sample["user input"], sample["response"]
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
# Include more detail in the error message
|
| 113 |
+
error_msg = f"Error reading CSV: {type(e).__name__}: {str(e)}"
|
| 114 |
+
print(f"[ERROR] {error_msg}")
|
| 115 |
+
return error_msg, ""
|
| 116 |
|
| 117 |
# Custom CSS for styling
|
| 118 |
CUSTOM_CSS = """
|
|
|
|
| 540 |
"""
|
| 541 |
|
| 542 |
return contextual_html, llama_html, openai_html, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
|
| 544 |
# Simplified Gradio app without complex history functionality
|
| 545 |
def create_gradio_app():
|
|
|
|
| 559 |
border_color_primary="#E0E0E0"
|
| 560 |
)
|
| 561 |
|
| 562 |
+
# Add CSS for the policy popup
|
| 563 |
+
custom_css = CUSTOM_CSS + """
|
| 564 |
+
/* Policy preview popup styles */
|
| 565 |
+
.policy-popup {
|
| 566 |
+
display: none;
|
| 567 |
+
position: fixed;
|
| 568 |
+
top: 0;
|
| 569 |
+
left: 0;
|
| 570 |
+
width: 100%;
|
| 571 |
+
height: 100%;
|
| 572 |
+
background-color: rgba(0,0,0,0.7);
|
| 573 |
+
z-index: 1000;
|
| 574 |
+
justify-content: center;
|
| 575 |
+
align-items: center;
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
.policy-popup-content {
|
| 579 |
+
background-color: white;
|
| 580 |
+
width: 80%;
|
| 581 |
+
height: 80%;
|
| 582 |
+
border-radius: 8px;
|
| 583 |
+
padding: 20px;
|
| 584 |
+
position: relative;
|
| 585 |
+
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 586 |
+
display: flex;
|
| 587 |
+
flex-direction: column;
|
| 588 |
+
}
|
| 589 |
+
|
| 590 |
+
.policy-popup-header {
|
| 591 |
+
display: flex;
|
| 592 |
+
justify-content: space-between;
|
| 593 |
+
align-items: center;
|
| 594 |
+
margin-bottom: 15px;
|
| 595 |
+
border-bottom: 1px solid #eee;
|
| 596 |
+
padding-bottom: 10px;
|
| 597 |
+
}
|
| 598 |
+
|
| 599 |
+
.policy-popup-title {
|
| 600 |
+
font-weight: bold;
|
| 601 |
+
font-size: 18px;
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
.policy-popup-close {
|
| 605 |
+
background-color: #222222;
|
| 606 |
+
color: white;
|
| 607 |
+
border: none;
|
| 608 |
+
border-radius: 4px;
|
| 609 |
+
padding: 5px 10px;
|
| 610 |
+
cursor: pointer;
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
.policy-popup-close:hover {
|
| 614 |
+
background-color: #000000;
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
+
.policy-iframe-container {
|
| 618 |
+
flex: 1;
|
| 619 |
+
overflow: hidden;
|
| 620 |
+
}
|
| 621 |
+
|
| 622 |
+
.policy-iframe {
|
| 623 |
+
width: 100%;
|
| 624 |
+
height: 100%;
|
| 625 |
+
border: 1px solid #eee;
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
/* Fallback for when PDF can't be displayed in iframe */
|
| 629 |
+
.policy-fallback {
|
| 630 |
+
padding: 20px;
|
| 631 |
+
text-align: center;
|
| 632 |
+
}
|
| 633 |
+
|
| 634 |
+
.policy-fallback a {
|
| 635 |
+
display: inline-block;
|
| 636 |
+
margin-top: 15px;
|
| 637 |
+
padding: 10px 15px;
|
| 638 |
+
background-color: #FCA539;
|
| 639 |
+
color: #000000;
|
| 640 |
+
text-decoration: none;
|
| 641 |
+
border-radius: 4px;
|
| 642 |
+
font-weight: bold;
|
| 643 |
+
}
|
| 644 |
+
"""
|
| 645 |
+
|
| 646 |
+
# Initialize the app with file serving capabilities
|
| 647 |
+
with gr.Blocks(title="AI Responses Policy Rating Oracle", theme=theme, css=custom_css) as app:
|
| 648 |
# Add loading spinner
|
| 649 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 650 |
+
|
| 651 |
+
# Create a file component to serve the PDF (it will be hidden from UI)
|
| 652 |
+
pdf_file = gr.File("Safety Oracle - AI Response Policy.pdf", visible=False, label="Policy PDF")
|
| 653 |
+
|
| 654 |
+
# Get the file path that Gradio will use to serve the file
|
| 655 |
+
# Note: We'll use a JavaScript approach instead to dynamically get the URL
|
| 656 |
+
|
| 657 |
+
# Add policy popup HTML with improved PDF handling
|
| 658 |
+
policy_popup_html = """
|
| 659 |
+
<div id="policy-popup" class="policy-popup">
|
| 660 |
+
<div class="policy-popup-content">
|
| 661 |
+
<div class="policy-popup-header">
|
| 662 |
+
<div class="policy-popup-title">Contextual AI Safety Policy</div>
|
| 663 |
+
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 664 |
+
</div>
|
| 665 |
+
<div class="policy-iframe-container">
|
| 666 |
+
<!-- Primary method: Try Google PDF Viewer -->
|
| 667 |
+
<iframe class="policy-iframe" id="policy-iframe"></iframe>
|
| 668 |
+
|
| 669 |
+
<!-- Fallback content if iframe fails -->
|
| 670 |
+
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
| 671 |
+
<p>The policy document couldn't be displayed in the preview.</p>
|
| 672 |
+
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
| 673 |
+
</div>
|
| 674 |
+
</div>
|
| 675 |
+
</div>
|
| 676 |
+
</div>
|
| 677 |
+
|
| 678 |
+
<script>
|
| 679 |
+
// Function to handle opening the policy popup
|
| 680 |
+
function openPolicyPopup() {
|
| 681 |
+
// Set PDF URL - this approach is more reliable with Gradio
|
| 682 |
+
const pdfFileName = "Safety Oracle - AI Response Policy.pdf";
|
| 683 |
+
|
| 684 |
+
// Try multiple approaches to display the PDF
|
| 685 |
+
// 1. Google PDF viewer (works in most cases)
|
| 686 |
+
const googleViewerUrl = "https://docs.google.com/viewer?embedded=true&url=";
|
| 687 |
+
|
| 688 |
+
// 2. Direct link as fallback
|
| 689 |
+
let directPdfUrl = "";
|
| 690 |
+
|
| 691 |
+
// Find the PDF link by looking for file links in the DOM
|
| 692 |
+
const links = document.querySelectorAll("a");
|
| 693 |
+
for (const link of links) {
|
| 694 |
+
if (link.href && link.href.includes(encodeURIComponent(pdfFileName))) {
|
| 695 |
+
directPdfUrl = link.href;
|
| 696 |
+
break;
|
| 697 |
+
}
|
| 698 |
+
}
|
| 699 |
+
|
| 700 |
+
// Set the iframe source if we found a link
|
| 701 |
+
const iframe = document.getElementById("policy-iframe");
|
| 702 |
+
const fallback = document.getElementById("policy-fallback");
|
| 703 |
+
const downloadLink = document.getElementById("policy-download-link");
|
| 704 |
+
|
| 705 |
+
if (directPdfUrl) {
|
| 706 |
+
// Try Google Viewer first
|
| 707 |
+
iframe.src = googleViewerUrl + encodeURIComponent(directPdfUrl);
|
| 708 |
+
iframe.style.display = "block";
|
| 709 |
+
fallback.style.display = "none";
|
| 710 |
+
|
| 711 |
+
// Set the download link
|
| 712 |
+
downloadLink.href = directPdfUrl;
|
| 713 |
+
|
| 714 |
+
// Provide fallback in case Google Viewer fails
|
| 715 |
+
iframe.onerror = function() {
|
| 716 |
+
iframe.style.display = "none";
|
| 717 |
+
fallback.style.display = "block";
|
| 718 |
+
};
|
| 719 |
+
} else {
|
| 720 |
+
// No direct URL found, show fallback
|
| 721 |
+
iframe.style.display = "none";
|
| 722 |
+
fallback.style.display = "block";
|
| 723 |
+
downloadLink.href = "#";
|
| 724 |
+
downloadLink.textContent = "PDF not available";
|
| 725 |
+
}
|
| 726 |
+
|
| 727 |
+
// Display the popup
|
| 728 |
+
document.getElementById('policy-popup').style.display = 'flex';
|
| 729 |
+
}
|
| 730 |
+
</script>
|
| 731 |
+
"""
|
| 732 |
+
|
| 733 |
+
gr.HTML(policy_popup_html)
|
| 734 |
|
| 735 |
gr.Markdown(
|
| 736 |
"# AI Responses Policy Rating Oracle\n"
|
|
|
|
| 750 |
rate_btn = gr.Button("Rate Content", variant="primary", size="lg", elem_classes=["orange-button"])
|
| 751 |
|
| 752 |
with gr.Column(scale=2):
|
| 753 |
+
# Contextual Safety Oracle with policy button
|
| 754 |
+
gr.HTML("""
|
| 755 |
+
<div>
|
| 756 |
+
<h3 class="result-header">🌟 Contextual Safety Oracle</h3>
|
| 757 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 758 |
+
<a href="#" class="knowledge-button" onclick="openPolicyPopup(); return false;">View policy</a>
|
| 759 |
+
</div>
|
| 760 |
+
</div>
|
| 761 |
+
""")
|
| 762 |
contextual_results = gr.HTML('<div class="rating-box contextual-box empty-rating">Rating will appear here</div>')
|
| 763 |
|
| 764 |
+
# Hidden placeholder for retrieved knowledge
|
| 765 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 766 |
|
| 767 |
with gr.Row():
|
| 768 |
with gr.Column():
|
| 769 |
+
# LlamaGuard section with permanent model card link
|
| 770 |
+
gr.HTML("""
|
| 771 |
+
<div>
|
| 772 |
+
<h3 class="result-header">🦙 LlamaGuard Rating</h3>
|
| 773 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 774 |
+
<a href="https://github.com/meta-llama/PurpleLlama/blob/main/Llama-Guard3/8B/MODEL_CARD.md"
|
| 775 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 776 |
+
</div>
|
| 777 |
+
</div>
|
| 778 |
+
""")
|
| 779 |
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 780 |
with gr.Column():
|
| 781 |
+
# OpenAI section with permanent model card link
|
| 782 |
+
gr.HTML("""
|
| 783 |
+
<div>
|
| 784 |
+
<h3 class="result-header">🧷 OpenAI Moderation</h3>
|
| 785 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 786 |
+
<a href="https://platform.openai.com/docs/guides/moderation"
|
| 787 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 788 |
+
</div>
|
| 789 |
+
</div>
|
| 790 |
+
""")
|
| 791 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 792 |
|
| 793 |
# Define show/hide loading indicator functions
|
hate_speech_demo.py
CHANGED
|
@@ -523,16 +523,181 @@ def create_gradio_app():
|
|
| 523 |
border_color_primary="#E0E0E0"
|
| 524 |
)
|
| 525 |
|
| 526 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
# Add loading spinner
|
| 528 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 529 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
gr.Markdown("# Hate Speech Policy Rating Oracle")
|
| 531 |
gr.Markdown(
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
)
|
| 536 |
|
| 537 |
with gr.Row():
|
| 538 |
with gr.Column(scale=1):
|
|
@@ -545,19 +710,44 @@ def create_gradio_app():
|
|
| 545 |
rate_btn = gr.Button("Rate Content", variant="primary", size="lg", elem_classes=["orange-button"])
|
| 546 |
|
| 547 |
with gr.Column(scale=2):
|
| 548 |
-
#
|
| 549 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
contextual_results = gr.HTML('<div class="rating-box contextual-box empty-rating">Rating will appear here</div>')
|
| 551 |
|
| 552 |
-
# Hidden placeholder for retrieved knowledge
|
| 553 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 554 |
|
| 555 |
with gr.Row():
|
| 556 |
with gr.Column():
|
| 557 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 559 |
with gr.Column():
|
| 560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 562 |
|
| 563 |
# Define show/hide loading indicator functions
|
|
|
|
| 523 |
border_color_primary="#E0E0E0"
|
| 524 |
)
|
| 525 |
|
| 526 |
+
# Add CSS for the policy popup
|
| 527 |
+
custom_css = CUSTOM_CSS + """
|
| 528 |
+
/* Policy preview popup styles */
|
| 529 |
+
.policy-popup {
|
| 530 |
+
display: none;
|
| 531 |
+
position: fixed;
|
| 532 |
+
top: 0;
|
| 533 |
+
left: 0;
|
| 534 |
+
width: 100%;
|
| 535 |
+
height: 100%;
|
| 536 |
+
background-color: rgba(0,0,0,0.7);
|
| 537 |
+
z-index: 1000;
|
| 538 |
+
justify-content: center;
|
| 539 |
+
align-items: center;
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
.policy-popup-content {
|
| 543 |
+
background-color: white;
|
| 544 |
+
width: 80%;
|
| 545 |
+
height: 80%;
|
| 546 |
+
border-radius: 8px;
|
| 547 |
+
padding: 20px;
|
| 548 |
+
position: relative;
|
| 549 |
+
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
| 550 |
+
display: flex;
|
| 551 |
+
flex-direction: column;
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
.policy-popup-header {
|
| 555 |
+
display: flex;
|
| 556 |
+
justify-content: space-between;
|
| 557 |
+
align-items: center;
|
| 558 |
+
margin-bottom: 15px;
|
| 559 |
+
border-bottom: 1px solid #eee;
|
| 560 |
+
padding-bottom: 10px;
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
.policy-popup-title {
|
| 564 |
+
font-weight: bold;
|
| 565 |
+
font-size: 18px;
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
.policy-popup-close {
|
| 569 |
+
background-color: #222222;
|
| 570 |
+
color: white;
|
| 571 |
+
border: none;
|
| 572 |
+
border-radius: 4px;
|
| 573 |
+
padding: 5px 10px;
|
| 574 |
+
cursor: pointer;
|
| 575 |
+
}
|
| 576 |
+
|
| 577 |
+
.policy-popup-close:hover {
|
| 578 |
+
background-color: #000000;
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
.policy-iframe-container {
|
| 582 |
+
flex: 1;
|
| 583 |
+
overflow: hidden;
|
| 584 |
+
}
|
| 585 |
+
|
| 586 |
+
.policy-iframe {
|
| 587 |
+
width: 100%;
|
| 588 |
+
height: 100%;
|
| 589 |
+
border: 1px solid #eee;
|
| 590 |
+
}
|
| 591 |
+
|
| 592 |
+
/* Fallback for when PDF can't be displayed in iframe */
|
| 593 |
+
.policy-fallback {
|
| 594 |
+
padding: 20px;
|
| 595 |
+
text-align: center;
|
| 596 |
+
}
|
| 597 |
+
|
| 598 |
+
.policy-fallback a {
|
| 599 |
+
display: inline-block;
|
| 600 |
+
margin-top: 15px;
|
| 601 |
+
padding: 10px 15px;
|
| 602 |
+
background-color: #FCA539;
|
| 603 |
+
color: #000000;
|
| 604 |
+
text-decoration: none;
|
| 605 |
+
border-radius: 4px;
|
| 606 |
+
font-weight: bold;
|
| 607 |
+
}
|
| 608 |
+
"""
|
| 609 |
+
|
| 610 |
+
with gr.Blocks(title="Hate Speech Policy Rating Oracle", theme=theme, css=custom_css) as app:
|
| 611 |
# Add loading spinner
|
| 612 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 613 |
|
| 614 |
+
# Create a file component to serve the PDF (hidden from UI)
|
| 615 |
+
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 616 |
+
|
| 617 |
+
# Add policy popup HTML with improved PDF handling
|
| 618 |
+
policy_popup_html = """
|
| 619 |
+
<div id="policy-popup" class="policy-popup">
|
| 620 |
+
<div class="policy-popup-content">
|
| 621 |
+
<div class="policy-popup-header">
|
| 622 |
+
<div class="policy-popup-title">Hate Speech Policy</div>
|
| 623 |
+
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 624 |
+
</div>
|
| 625 |
+
<div class="policy-iframe-container">
|
| 626 |
+
<!-- Primary method: Try Google PDF Viewer -->
|
| 627 |
+
<iframe class="policy-iframe" id="policy-iframe"></iframe>
|
| 628 |
+
|
| 629 |
+
<!-- Fallback content if iframe fails -->
|
| 630 |
+
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
| 631 |
+
<p>The policy document couldn't be displayed in the preview.</p>
|
| 632 |
+
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
| 633 |
+
</div>
|
| 634 |
+
</div>
|
| 635 |
+
</div>
|
| 636 |
+
</div>
|
| 637 |
+
|
| 638 |
+
<script>
|
| 639 |
+
// Function to handle opening the policy popup
|
| 640 |
+
function openPolicyPopup() {
|
| 641 |
+
// Set PDF URL - this approach is more reliable with Gradio
|
| 642 |
+
const pdfFileName = "Hate Speech Policy.pdf";
|
| 643 |
+
|
| 644 |
+
// Try multiple approaches to display the PDF
|
| 645 |
+
// 1. Google PDF viewer (works in most cases)
|
| 646 |
+
const googleViewerUrl = "https://docs.google.com/viewer?embedded=true&url=";
|
| 647 |
+
|
| 648 |
+
// 2. Direct link as fallback
|
| 649 |
+
let directPdfUrl = "";
|
| 650 |
+
|
| 651 |
+
// Find the PDF link by looking for file links in the DOM
|
| 652 |
+
const links = document.querySelectorAll("a");
|
| 653 |
+
for (const link of links) {
|
| 654 |
+
if (link.href && link.href.includes(encodeURIComponent(pdfFileName))) {
|
| 655 |
+
directPdfUrl = link.href;
|
| 656 |
+
break;
|
| 657 |
+
}
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
// Set the iframe source if we found a link
|
| 661 |
+
const iframe = document.getElementById("policy-iframe");
|
| 662 |
+
const fallback = document.getElementById("policy-fallback");
|
| 663 |
+
const downloadLink = document.getElementById("policy-download-link");
|
| 664 |
+
|
| 665 |
+
if (directPdfUrl) {
|
| 666 |
+
// Try Google Viewer first
|
| 667 |
+
iframe.src = googleViewerUrl + encodeURIComponent(directPdfUrl);
|
| 668 |
+
iframe.style.display = "block";
|
| 669 |
+
fallback.style.display = "none";
|
| 670 |
+
|
| 671 |
+
// Set the download link
|
| 672 |
+
downloadLink.href = directPdfUrl;
|
| 673 |
+
|
| 674 |
+
// Provide fallback in case Google Viewer fails
|
| 675 |
+
iframe.onerror = function() {
|
| 676 |
+
iframe.style.display = "none";
|
| 677 |
+
fallback.style.display = "block";
|
| 678 |
+
};
|
| 679 |
+
} else {
|
| 680 |
+
// No direct URL found, show fallback
|
| 681 |
+
iframe.style.display = "none";
|
| 682 |
+
fallback.style.display = "block";
|
| 683 |
+
downloadLink.href = "#";
|
| 684 |
+
downloadLink.textContent = "PDF not available";
|
| 685 |
+
}
|
| 686 |
+
|
| 687 |
+
// Display the popup
|
| 688 |
+
document.getElementById('policy-popup').style.display = 'flex';
|
| 689 |
+
}
|
| 690 |
+
</script>
|
| 691 |
+
"""
|
| 692 |
+
|
| 693 |
+
gr.HTML(policy_popup_html)
|
| 694 |
+
|
| 695 |
gr.Markdown("# Hate Speech Policy Rating Oracle")
|
| 696 |
gr.Markdown(
|
| 697 |
+
"Compare content ratings from LlamaGuard, OpenAI Moderation, and Contextual Safety Oracle \n"
|
| 698 |
+
"**Instructions:** Input a test case and you will retrieve a rating for the content from all three models— or use our random test case generator. \n"
|
| 699 |
+
"**‼️SAFETY WARNING‼️:** Some of the randomly generated test cases may contain offensive or upsetting content."
|
| 700 |
+
)
|
| 701 |
|
| 702 |
with gr.Row():
|
| 703 |
with gr.Column(scale=1):
|
|
|
|
| 710 |
rate_btn = gr.Button("Rate Content", variant="primary", size="lg", elem_classes=["orange-button"])
|
| 711 |
|
| 712 |
with gr.Column(scale=2):
|
| 713 |
+
# Contextual Safety Oracle with policy button
|
| 714 |
+
gr.HTML("""
|
| 715 |
+
<div>
|
| 716 |
+
<h3 class="result-header">🌟 Contextual Safety Oracle</h3>
|
| 717 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 718 |
+
<a href="#" class="knowledge-button" onclick="openPolicyPopup(); return false;">View policy</a>
|
| 719 |
+
</div>
|
| 720 |
+
</div>
|
| 721 |
+
""")
|
| 722 |
contextual_results = gr.HTML('<div class="rating-box contextual-box empty-rating">Rating will appear here</div>')
|
| 723 |
|
| 724 |
+
# Hidden placeholder for retrieved knowledge
|
| 725 |
retrieved_knowledge = gr.HTML('', visible=False)
|
| 726 |
|
| 727 |
with gr.Row():
|
| 728 |
with gr.Column():
|
| 729 |
+
# LlamaGuard section with permanent model card link
|
| 730 |
+
gr.HTML("""
|
| 731 |
+
<div>
|
| 732 |
+
<h3 class="result-header">🦙 LlamaGuard Rating</h3>
|
| 733 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 734 |
+
<a href="https://github.com/meta-llama/PurpleLlama/blob/main/Llama-Guard3/8B/MODEL_CARD.md"
|
| 735 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 736 |
+
</div>
|
| 737 |
+
</div>
|
| 738 |
+
""")
|
| 739 |
llama_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 740 |
with gr.Column():
|
| 741 |
+
# OpenAI section with permanent model card link
|
| 742 |
+
gr.HTML("""
|
| 743 |
+
<div>
|
| 744 |
+
<h3 class="result-header">🧷 OpenAI Moderation</h3>
|
| 745 |
+
<div style="margin-top: -10px; margin-bottom: 10px;">
|
| 746 |
+
<a href="https://platform.openai.com/docs/guides/moderation"
|
| 747 |
+
target="_blank" class="knowledge-button">View model card</a>
|
| 748 |
+
</div>
|
| 749 |
+
</div>
|
| 750 |
+
""")
|
| 751 |
openai_results = gr.HTML('<div class="rating-box secondary-box empty-rating">Rating will appear here</div>')
|
| 752 |
|
| 753 |
# Define show/hide loading indicator functions
|