Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- hate_speech_demo.py +147 -143
.DS_Store
CHANGED
|
Binary files a/.DS_Store and b/.DS_Store differ
|
|
|
hate_speech_demo.py
CHANGED
|
@@ -14,7 +14,6 @@ import json
|
|
| 14 |
# Load environment variables (for local development)
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
-
|
| 18 |
# Process retrieval text to highlight relevant parts - IMPROVED FORMATTING
|
| 19 |
def process_retrieval_text(retrieval_text, user_input):
|
| 20 |
"""
|
|
@@ -743,159 +742,164 @@ def create_gradio_app():
|
|
| 743 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 744 |
|
| 745 |
# Create a file component to serve the PDF (hidden from UI)
|
| 746 |
-
pdf_file = gr.File("Hate Speech Policy.pdf", visible=
|
| 747 |
|
| 748 |
# Add policy popup HTML with reliable PDF loading that works on first click
|
| 749 |
-
# Replace the problematic policy_popup_html with this fixed version
|
| 750 |
-
|
| 751 |
policy_popup_html = """
|
| 752 |
-
<div id="policy-popup" class="policy-popup">
|
| 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 |
-
<div
|
| 778 |
-
|
|
|
|
|
|
|
|
|
|
| 779 |
</div>
|
| 780 |
</div>
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
|
|
|
|
|
|
|
|
|
| 800 |
}
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
}
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
<script>
|
| 811 |
-
// Function to find PDF URL from various Gradio components
|
| 812 |
-
function findPdfUrl() {
|
| 813 |
-
// Display loading indicator
|
| 814 |
-
document.getElementById('pdf-loading').style.display = 'flex';
|
| 815 |
-
|
| 816 |
-
// Method 1: Try to find the PDF URL from file components
|
| 817 |
-
const fileComponents = document.querySelectorAll(".file-component");
|
| 818 |
-
for (const comp of fileComponents) {
|
| 819 |
-
const downloadBtn = comp.querySelector("a[download]");
|
| 820 |
-
if (downloadBtn && downloadBtn.href &&
|
| 821 |
-
(downloadBtn.href.includes("Hate Speech Policy.pdf") ||
|
| 822 |
-
downloadBtn.href.includes("Hate%20Speech%20Policy.pdf"))) {
|
| 823 |
-
document.getElementById('pdf-loading').style.display = 'none';
|
| 824 |
-
return downloadBtn.href;
|
| 825 |
}
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 837 |
}
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
|
|
|
| 848 |
}
|
| 849 |
}
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
}
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
}
|
| 882 |
-
|
| 883 |
-
// Make functions globally available
|
| 884 |
-
window.openPolicyPopup = openPolicyPopup;
|
| 885 |
-
|
| 886 |
-
// Initialize when the document is loaded
|
| 887 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 888 |
-
// Find the PDF URL in advance
|
| 889 |
-
const pdfUrl = findPdfUrl();
|
| 890 |
-
|
| 891 |
-
// Set the download link
|
| 892 |
-
const downloadLink = document.getElementById('policy-download-link');
|
| 893 |
-
if (downloadLink) {
|
| 894 |
-
downloadLink.href = pdfUrl;
|
| 895 |
-
}
|
| 896 |
-
});
|
| 897 |
-
</script>
|
| 898 |
-
"""
|
| 899 |
|
| 900 |
gr.HTML(policy_popup_html)
|
| 901 |
|
|
|
|
| 14 |
# Load environment variables (for local development)
|
| 15 |
load_dotenv()
|
| 16 |
|
|
|
|
| 17 |
# Process retrieval text to highlight relevant parts - IMPROVED FORMATTING
|
| 18 |
def process_retrieval_text(retrieval_text, user_input):
|
| 19 |
"""
|
|
|
|
| 742 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
| 743 |
|
| 744 |
# Create a file component to serve the PDF (hidden from UI)
|
| 745 |
+
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 746 |
|
| 747 |
# Add policy popup HTML with reliable PDF loading that works on first click
|
|
|
|
|
|
|
| 748 |
policy_popup_html = """
|
| 749 |
+
<div id="policy-popup" class="policy-popup">
|
| 750 |
+
<div class="policy-popup-content">
|
| 751 |
+
<div class="policy-popup-header">
|
| 752 |
+
<div class="policy-popup-title">Hate Speech Policy</div>
|
| 753 |
+
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
| 754 |
+
</div>
|
| 755 |
+
<div class="policy-iframe-container">
|
| 756 |
+
<!-- PDF viewer iframe -->
|
| 757 |
+
<iframe class="policy-iframe" id="policy-iframe"></iframe>
|
| 758 |
+
|
| 759 |
+
<!-- Loading indicator while PDF loads -->
|
| 760 |
+
<div id="pdf-loading" style="display: flex; justify-content: center; align-items: center; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(255,255,255,0.8);">
|
| 761 |
+
<div style="text-align: center;">
|
| 762 |
+
<div style="width: 50px; height: 50px; border: 5px solid #f3f3f3; border-top: 5px solid #FCA539; border-radius: 50%; margin: 0 auto 15px auto; animation: spin 1s linear infinite;"></div>
|
| 763 |
+
<p>Loading policy document...</p>
|
| 764 |
+
</div>
|
| 765 |
+
</div>
|
| 766 |
+
<style>
|
| 767 |
+
@keyframes spin {
|
| 768 |
+
0% { transform: rotate(0deg); }
|
| 769 |
+
100% { transform: rotate(360deg); }
|
| 770 |
+
}
|
| 771 |
+
</style>
|
| 772 |
+
|
| 773 |
+
<!-- Fallback content if iframe fails -->
|
| 774 |
+
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
| 775 |
+
<p>The policy document couldn't be displayed in the preview.</p>
|
| 776 |
+
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
| 777 |
+
<button onclick="retryPdfLoad()" style="display: block; margin-top: 15px; padding: 8px 15px; background: #FCA539; border: none; border-radius: 4px; cursor: pointer;">Try Again</button>
|
| 778 |
+
</div>
|
| 779 |
</div>
|
| 780 |
</div>
|
| 781 |
+
</div>
|
| 782 |
+
|
| 783 |
+
<script>
|
| 784 |
+
// Store the PDF URL globally once found
|
| 785 |
+
let pdfPolicyUrl = null;
|
| 786 |
+
let pdfLoadAttempted = false;
|
| 787 |
+
|
| 788 |
+
// Find the PDF URL as soon as possible
|
| 789 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 790 |
+
findPdfUrl();
|
| 791 |
+
});
|
| 792 |
+
|
| 793 |
+
// Function to find the PDF URL in various locations
|
| 794 |
+
function findPdfUrl() {
|
| 795 |
+
// Try to find the file component
|
| 796 |
+
const fileComponents = document.querySelectorAll("div.file-component");
|
| 797 |
+
for (const comp of fileComponents) {
|
| 798 |
+
const downloadBtn = comp.querySelector("a[download]");
|
| 799 |
+
if (downloadBtn && downloadBtn.href && downloadBtn.href.includes("file=")) {
|
| 800 |
+
pdfPolicyUrl = downloadBtn.href;
|
| 801 |
+
console.log("Found PDF URL:", pdfPolicyUrl);
|
| 802 |
+
return pdfPolicyUrl;
|
| 803 |
}
|
| 804 |
+
}
|
| 805 |
+
|
| 806 |
+
// Try to find any link with the PDF name
|
| 807 |
+
const allLinks = document.querySelectorAll("a");
|
| 808 |
+
for (const link of allLinks) {
|
| 809 |
+
if (link.href && (link.href.includes("Hate%20Speech%20Policy.pdf") ||
|
| 810 |
+
link.href.includes("file=") && link.textContent.includes("Download"))) {
|
| 811 |
+
pdfPolicyUrl = link.href;
|
| 812 |
+
console.log("Found PDF URL from links:", pdfPolicyUrl);
|
| 813 |
+
return pdfPolicyUrl;
|
| 814 |
}
|
| 815 |
+
}
|
| 816 |
+
|
| 817 |
+
// If we didn't find it, try again in a second
|
| 818 |
+
setTimeout(findPdfUrl, 1000);
|
| 819 |
+
return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
}
|
| 821 |
+
|
| 822 |
+
// Function to actually load the PDF
|
| 823 |
+
function loadPolicyPdf(url) {
|
| 824 |
+
const iframe = document.getElementById("policy-iframe");
|
| 825 |
+
const loadingIndicator = document.getElementById("pdf-loading");
|
| 826 |
+
const fallback = document.getElementById("policy-fallback");
|
| 827 |
+
const downloadLink = document.getElementById("policy-download-link");
|
| 828 |
+
|
| 829 |
+
// Show loading, hide fallback
|
| 830 |
+
loadingIndicator.style.display = "flex";
|
| 831 |
+
fallback.style.display = "none";
|
| 832 |
+
iframe.style.display = "block";
|
| 833 |
+
|
| 834 |
+
// Set the download link
|
| 835 |
+
downloadLink.href = url;
|
| 836 |
+
|
| 837 |
+
// Set iframe source to PDF
|
| 838 |
+
iframe.src = url;
|
| 839 |
+
|
| 840 |
+
// Set a timeout to hide loading regardless of outcome
|
| 841 |
+
setTimeout(function() {
|
| 842 |
+
loadingIndicator.style.display = "none";
|
| 843 |
+
}, 3000);
|
| 844 |
+
|
| 845 |
+
// When iframe loads, hide loading indicator
|
| 846 |
+
iframe.onload = function() {
|
| 847 |
+
loadingIndicator.style.display = "none";
|
| 848 |
+
pdfLoadAttempted = true;
|
| 849 |
+
};
|
| 850 |
+
|
| 851 |
+
// Handle errors
|
| 852 |
+
iframe.onerror = function() {
|
| 853 |
+
loadingIndicator.style.display = "none";
|
| 854 |
+
fallback.style.display = "block";
|
| 855 |
+
iframe.style.display = "none";
|
| 856 |
+
pdfLoadAttempted = true;
|
| 857 |
+
};
|
| 858 |
}
|
| 859 |
+
|
| 860 |
+
// Function to retry loading the PDF
|
| 861 |
+
function retryPdfLoad() {
|
| 862 |
+
if (pdfPolicyUrl) {
|
| 863 |
+
loadPolicyPdf(pdfPolicyUrl);
|
| 864 |
+
} else {
|
| 865 |
+
// Try to find the URL first
|
| 866 |
+
const url = findPdfUrl();
|
| 867 |
+
if (url) {
|
| 868 |
+
loadPolicyPdf(url);
|
| 869 |
+
}
|
| 870 |
}
|
| 871 |
}
|
| 872 |
+
|
| 873 |
+
// Function to handle opening the policy popup
|
| 874 |
+
function openPolicyPopup() {
|
| 875 |
+
// Display the popup immediately
|
| 876 |
+
document.getElementById('policy-popup').style.display = 'flex';
|
| 877 |
+
|
| 878 |
+
// If we've never tried to load the PDF, or it failed previously, try loading it
|
| 879 |
+
if (!pdfLoadAttempted) {
|
| 880 |
+
if (pdfPolicyUrl) {
|
| 881 |
+
// We already have the URL, load the PDF
|
| 882 |
+
loadPolicyPdf(pdfPolicyUrl);
|
| 883 |
+
} else {
|
| 884 |
+
// Try to find the URL first
|
| 885 |
+
const url = findPdfUrl();
|
| 886 |
+
if (url) {
|
| 887 |
+
loadPolicyPdf(url);
|
| 888 |
+
} else {
|
| 889 |
+
// Show fallback if we can't find the URL
|
| 890 |
+
document.getElementById("pdf-loading").style.display = "none";
|
| 891 |
+
document.getElementById("policy-iframe").style.display = "none";
|
| 892 |
+
document.getElementById("policy-fallback").style.display = "block";
|
| 893 |
+
}
|
| 894 |
+
}
|
| 895 |
+
}
|
| 896 |
+
}
|
| 897 |
+
|
| 898 |
+
// Make functions globally available
|
| 899 |
+
window.openPolicyPopup = openPolicyPopup;
|
| 900 |
+
window.retryPdfLoad = retryPdfLoad;
|
| 901 |
+
</script>
|
| 902 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 903 |
|
| 904 |
gr.HTML(policy_popup_html)
|
| 905 |
|