rwillats commited on
Commit
a1e309f
·
verified ·
1 Parent(s): 49042cb

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. 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
- # Create a file component to serve the PDF - make this visible and position it where needed
745
- with gr.Row(visible=False) as pdf_container:
746
- pdf_file = gr.File("Hate Speech Policy.pdf", label="Hate Speech Policy Document", elem_id="policy-pdf-file")
 
 
 
747
 
748
- # Add a simplified policy popup
749
  policy_popup_html = """
750
- <div id="policy-popup" class="policy-popup">
751
- <div class="policy-popup-content">
752
- <div class="policy-popup-header">
753
- <div class="policy-popup-title">Hate Speech Policy</div>
754
- <button class="policy-popup-close" onclick="document.getElementById('policy-popup').style.display='none';">Close</button>
755
- </div>
756
- <div class="policy-content">
757
- <p>To view or download the Hate Speech Policy document, please use the button below:</p>
758
- <button id="show-policy-btn" class="evidence-button" style="margin: 20px auto; display: block;"
759
- onclick="showPolicyDocument(); return false;">
760
- Show Policy Document
761
- </button>
762
- <div id="policy-container" style="display:none; margin-top: 20px;"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
763
  </div>
764
  </div>
765
- </div>
766
 
767
- <script>
768
- function showPolicyDocument() {
769
- // Hide the button
770
- document.getElementById('show-policy-btn').style.display = 'none';
771
-
772
- // Show the policy container
773
- const policyContainer = document.getElementById('policy-container');
774
- policyContainer.style.display = 'block';
775
-
776
- // Find the file component and click its download button
777
- // First try to unhide the container if it's hidden
778
- const pdfContainer = document.querySelector('[id$="pdf-container"]');
779
- if (pdfContainer) {
780
- pdfContainer.style.display = 'block';
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
- // Option 3: Fallback - if we can't find the component, show a download link
801
- const downloadLinks = document.querySelectorAll('a[download]');
802
- for (const link of downloadLinks) {
803
- if (link.href && link.href.includes('Hate%20Speech%20Policy.pdf')) {
804
- policyContainer.innerHTML = `
805
- <div style="text-align:center; padding: 20px; background: #f9f9f9; border-radius: 8px;">
806
- <p>Click the button below to download the policy document:</p>
807
- <a href="${link.href}" class="evidence-button" download="Hate Speech Policy.pdf"
808
- style="margin: 10px auto; display: inline-block; text-decoration: none;">
809
- Download Policy PDF
810
- </a>
811
- </div>
812
- `;
813
- return;
 
 
 
814
  }
815
  }
816
-
817
- // Final fallback - if nothing else works
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