Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- hate_speech_demo.py +73 -78
hate_speech_demo.py
CHANGED
@@ -10,6 +10,7 @@ from dotenv import load_dotenv
|
|
10 |
from together import Together
|
11 |
import openai
|
12 |
import json
|
|
|
13 |
|
14 |
# Load environment variables (for local development)
|
15 |
load_dotenv()
|
@@ -741,92 +742,86 @@ def create_gradio_app():
|
|
741 |
# Add loading spinner
|
742 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
743 |
|
744 |
-
|
745 |
-
|
746 |
-
|
|
|
|
|
|
|
747 |
|
748 |
-
# Add
|
749 |
policy_popup_html = """
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
763 |
</div>
|
764 |
</div>
|
765 |
-
</div>
|
766 |
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
// Option 1: Try to move the whole PDF component into our container
|
784 |
-
const fileComponent = document.getElementById('policy-pdf-file');
|
785 |
-
if (fileComponent) {
|
786 |
-
policyContainer.appendChild(fileComponent);
|
787 |
-
return;
|
788 |
-
}
|
789 |
-
|
790 |
-
// Option 2: If we can't find the component by ID, look for it by class
|
791 |
-
const allFileComponents = document.querySelectorAll('.gradio-file');
|
792 |
-
for (const comp of allFileComponents) {
|
793 |
-
const fileLabel = comp.querySelector('label');
|
794 |
-
if (fileLabel && fileLabel.textContent.includes('Hate Speech Policy')) {
|
795 |
-
policyContainer.appendChild(comp);
|
796 |
-
return;
|
797 |
}
|
798 |
-
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
|
808 |
-
|
809 |
-
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
|
|
|
|
|
|
814 |
}
|
815 |
}
|
816 |
-
|
817 |
-
|
818 |
-
policyContainer.innerHTML = `
|
819 |
-
<div style="text-align:center; padding: 20px; background: #f9f9f9; border-radius: 8px;">
|
820 |
-
<p>The policy document couldn't be displayed automatically.</p>
|
821 |
-
<p>Please check the main interface for a file named "Hate Speech Policy.pdf".</p>
|
822 |
-
</div>
|
823 |
-
`;
|
824 |
-
}
|
825 |
-
|
826 |
-
// Make function globally available
|
827 |
-
window.showPolicyDocument = showPolicyDocument;
|
828 |
-
</script>
|
829 |
-
"""
|
830 |
|
831 |
gr.HTML(policy_popup_html)
|
832 |
|
|
|
10 |
from together import Together
|
11 |
import openai
|
12 |
import json
|
13 |
+
from pathlib import Path
|
14 |
|
15 |
# Load environment variables (for local development)
|
16 |
load_dotenv()
|
|
|
742 |
# Add loading spinner
|
743 |
loading_spinner = gr.HTML('<div id="loading-spinner"></div>')
|
744 |
|
745 |
+
pdf_path = Path("Hate Speech Policy.pdf")
|
746 |
+
if pdf_path.exists():
|
747 |
+
pdf_file = gr.File(pdf_path, visible=False, label="Policy PDF")
|
748 |
+
else:
|
749 |
+
# Fallback if PDF doesn't exist
|
750 |
+
pdf_file = gr.File(None, visible=False, label="Policy PDF")
|
751 |
|
752 |
+
# Add policy popup HTML with reliable PDF loading that works on first click
|
753 |
policy_popup_html = """
|
754 |
+
<div id="policy-popup" class="policy-popup">
|
755 |
+
<div class="policy-popup-content">
|
756 |
+
<div class="policy-popup-header">
|
757 |
+
<div class="policy-popup-title">Hate Speech Policy</div>
|
758 |
+
<button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
|
759 |
+
</div>
|
760 |
+
<div class="policy-iframe-container">
|
761 |
+
<!-- PDF viewer iframe with PDF.js as fallback -->
|
762 |
+
<iframe class="policy-iframe" id="policy-iframe"
|
763 |
+
src="https://docs.google.com/viewer?url=${pdf_url}&embedded=true"
|
764 |
+
style="width:100%; height:100%; border:none;"></iframe>
|
765 |
+
|
766 |
+
<!-- Loading indicator -->
|
767 |
+
<div id="pdf-loading" style="display: flex; justify-content: center; align-items: center; height: 100%;">
|
768 |
+
<div style="text-align: center;">
|
769 |
+
<div style="width: 50px; height: 50px; border: 5px solid #f3f3f3;
|
770 |
+
border-top: 5px solid #FCA539; border-radius: 50%;
|
771 |
+
margin: 0 auto 15px auto; animation: spin 1s linear infinite;"></div>
|
772 |
+
<p>Loading policy document...</p>
|
773 |
+
</div>
|
774 |
+
</div>
|
775 |
+
|
776 |
+
<!-- Fallback content -->
|
777 |
+
<div class="policy-fallback" id="policy-fallback" style="display:none;">
|
778 |
+
<p>The policy document couldn't be displayed in the preview.</p>
|
779 |
+
<a href="#" id="policy-download-link" target="_blank">Download Policy PDF</a>
|
780 |
+
</div>
|
781 |
+
</div>
|
782 |
</div>
|
783 |
</div>
|
|
|
784 |
|
785 |
+
<script>
|
786 |
+
// Function to open the policy popup
|
787 |
+
function openPolicyPopup() {
|
788 |
+
const popup = document.getElementById('policy-popup');
|
789 |
+
popup.style.display = 'flex';
|
790 |
+
|
791 |
+
// Find the PDF URL from Gradio's file component
|
792 |
+
const fileComponents = document.querySelectorAll("div.file-component");
|
793 |
+
let pdfUrl = null;
|
794 |
+
|
795 |
+
for (const comp of fileComponents) {
|
796 |
+
const downloadBtn = comp.querySelector("a[download]");
|
797 |
+
if (downloadBtn && downloadBtn.href) {
|
798 |
+
pdfUrl = downloadBtn.href;
|
799 |
+
break;
|
800 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
801 |
}
|
802 |
+
|
803 |
+
if (pdfUrl) {
|
804 |
+
// Encode the URL for Google Viewer
|
805 |
+
const encodedUrl = encodeURIComponent(pdfUrl);
|
806 |
+
const iframe = document.getElementById('policy-iframe');
|
807 |
+
iframe.src = `https://docs.google.com/viewer?url=${encodedUrl}&embedded=true`;
|
808 |
+
|
809 |
+
// Set up the download link
|
810 |
+
document.getElementById('policy-download-link').href = pdfUrl;
|
811 |
+
|
812 |
+
// Hide loading after some time (or when loaded)
|
813 |
+
setTimeout(() => {
|
814 |
+
document.getElementById('pdf-loading').style.display = 'none';
|
815 |
+
}, 3000);
|
816 |
+
} else {
|
817 |
+
// Show fallback if no PDF found
|
818 |
+
document.getElementById('pdf-loading').style.display = 'none';
|
819 |
+
document.getElementById('policy-fallback').style.display = 'block';
|
820 |
+
document.getElementById('policy-iframe').style.display = 'none';
|
821 |
}
|
822 |
}
|
823 |
+
</script>
|
824 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
825 |
|
826 |
gr.HTML(policy_popup_html)
|
827 |
|