Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +33 -38
hate_speech_demo.py
CHANGED
|
@@ -746,6 +746,8 @@ def create_gradio_app():
|
|
| 746 |
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 747 |
|
| 748 |
# Add policy popup HTML with reliable PDF loading that works on first click
|
|
|
|
|
|
|
| 749 |
policy_popup_html = """
|
| 750 |
<div id="policy-popup" class="policy-popup">
|
| 751 |
<div class="policy-popup-content">
|
|
@@ -780,17 +782,12 @@ def create_gradio_app():
|
|
| 780 |
</div>
|
| 781 |
</div>
|
| 782 |
</div>
|
| 783 |
-
|
| 784 |
<script>
|
| 785 |
// Store the PDF URL globally once found
|
| 786 |
let pdfPolicyUrl = "/file=Hate%20Speech%20Policy.pdf";
|
| 787 |
let pdfLoadAttempted = false;
|
| 788 |
-
|
| 789 |
-
// Find the PDF URL as soon as possible
|
| 790 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 791 |
-
findPdfUrl();
|
| 792 |
-
});
|
| 793 |
-
|
| 794 |
// Function to find the PDF URL in various locations
|
| 795 |
function findPdfUrl() {
|
| 796 |
// Try to find the file component
|
|
@@ -808,7 +805,7 @@ def create_gradio_app():
|
|
| 808 |
const allLinks = document.querySelectorAll("a");
|
| 809 |
for (const link of allLinks) {
|
| 810 |
if (link.href && (link.href.includes("Hate%20Speech%20Policy.pdf") ||
|
| 811 |
-
|
| 812 |
pdfPolicyUrl = link.href;
|
| 813 |
console.log("Found PDF URL from links:", pdfPolicyUrl);
|
| 814 |
return pdfPolicyUrl;
|
|
@@ -819,7 +816,7 @@ def create_gradio_app():
|
|
| 819 |
setTimeout(findPdfUrl, 1000);
|
| 820 |
return null;
|
| 821 |
}
|
| 822 |
-
|
| 823 |
// Function to actually load the PDF
|
| 824 |
function loadPolicyPdf(url) {
|
| 825 |
const iframe = document.getElementById("policy-iframe");
|
|
@@ -857,7 +854,7 @@ def create_gradio_app():
|
|
| 857 |
pdfLoadAttempted = true;
|
| 858 |
};
|
| 859 |
}
|
| 860 |
-
|
| 861 |
// Function to retry loading the PDF
|
| 862 |
function retryPdfLoad() {
|
| 863 |
if (pdfPolicyUrl) {
|
|
@@ -870,37 +867,35 @@ def create_gradio_app():
|
|
| 870 |
}
|
| 871 |
}
|
| 872 |
}
|
| 873 |
-
|
| 874 |
-
// Function to
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 886 |
}
|
| 887 |
-
}
|
| 888 |
-
// Retry in 1s if not yet available
|
| 889 |
-
setTimeout(findPdfUrlAndLoad, 1000);
|
| 890 |
-
}
|
| 891 |
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
pdfLoadAttempted = true;
|
| 897 |
-
}
|
| 898 |
-
}
|
| 899 |
|
| 900 |
-
window.openPolicyPopup = openPolicyPopup;
|
| 901 |
-
</script>
|
| 902 |
-
|
| 903 |
-
|
| 904 |
// Make functions globally available
|
| 905 |
window.openPolicyPopup = openPolicyPopup;
|
| 906 |
window.retryPdfLoad = retryPdfLoad;
|
|
|
|
| 746 |
pdf_file = gr.File("Hate Speech Policy.pdf", visible=False, label="Policy PDF")
|
| 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 |
<div class="policy-popup-content">
|
|
|
|
| 782 |
</div>
|
| 783 |
</div>
|
| 784 |
</div>
|
| 785 |
+
|
| 786 |
<script>
|
| 787 |
// Store the PDF URL globally once found
|
| 788 |
let pdfPolicyUrl = "/file=Hate%20Speech%20Policy.pdf";
|
| 789 |
let pdfLoadAttempted = false;
|
| 790 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 791 |
// Function to find the PDF URL in various locations
|
| 792 |
function findPdfUrl() {
|
| 793 |
// Try to find the file component
|
|
|
|
| 805 |
const allLinks = document.querySelectorAll("a");
|
| 806 |
for (const link of allLinks) {
|
| 807 |
if (link.href && (link.href.includes("Hate%20Speech%20Policy.pdf") ||
|
| 808 |
+
link.href.includes("file=") && link.textContent.includes("Download"))) {
|
| 809 |
pdfPolicyUrl = link.href;
|
| 810 |
console.log("Found PDF URL from links:", pdfPolicyUrl);
|
| 811 |
return pdfPolicyUrl;
|
|
|
|
| 816 |
setTimeout(findPdfUrl, 1000);
|
| 817 |
return null;
|
| 818 |
}
|
| 819 |
+
|
| 820 |
// Function to actually load the PDF
|
| 821 |
function loadPolicyPdf(url) {
|
| 822 |
const iframe = document.getElementById("policy-iframe");
|
|
|
|
| 854 |
pdfLoadAttempted = true;
|
| 855 |
};
|
| 856 |
}
|
| 857 |
+
|
| 858 |
// Function to retry loading the PDF
|
| 859 |
function retryPdfLoad() {
|
| 860 |
if (pdfPolicyUrl) {
|
|
|
|
| 867 |
}
|
| 868 |
}
|
| 869 |
}
|
| 870 |
+
|
| 871 |
+
// Function to open the policy popup
|
| 872 |
+
function openPolicyPopup() {
|
| 873 |
+
document.getElementById('policy-popup').style.display = 'flex';
|
| 874 |
+
|
| 875 |
+
// Find and load the PDF if not already attempted
|
| 876 |
+
if (!pdfLoadAttempted) {
|
| 877 |
+
// First try to use existing URL
|
| 878 |
+
if (pdfPolicyUrl) {
|
| 879 |
+
loadPolicyPdf(pdfPolicyUrl);
|
| 880 |
+
} else {
|
| 881 |
+
// Try to find the URL
|
| 882 |
+
const url = findPdfUrl();
|
| 883 |
+
if (url) {
|
| 884 |
+
loadPolicyPdf(url);
|
| 885 |
+
} else {
|
| 886 |
+
// Start looking for it
|
| 887 |
+
findPdfUrl();
|
| 888 |
+
}
|
| 889 |
+
}
|
| 890 |
+
pdfLoadAttempted = true;
|
| 891 |
+
}
|
| 892 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 893 |
|
| 894 |
+
// Find the PDF URL as soon as possible
|
| 895 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 896 |
+
findPdfUrl();
|
| 897 |
+
});
|
|
|
|
|
|
|
|
|
|
| 898 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 899 |
// Make functions globally available
|
| 900 |
window.openPolicyPopup = openPolicyPopup;
|
| 901 |
window.retryPdfLoad = retryPdfLoad;
|