nonzeroexit commited on
Commit
10e59a5
·
verified ·
1 Parent(s): a504dab

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +58 -55
index.html CHANGED
@@ -992,9 +992,13 @@
992
  </address>
993
  </footer>
994
 
995
- <script>
 
 
996
  const GRADIO_HF_SPACE_RAW_URL = "https://huggingface.co/spaces/nonzeroexit/AMP-Classifier";
997
  const GRADIO_PREDICTION_TARGET_ID = "nonzeroexit/AMP-Classifier";
 
 
998
 
999
  const sequenceInput = document.getElementById('sequence');
1000
  const charCount = document.getElementById('char-count');
@@ -1013,7 +1017,8 @@
1013
  const emailErrorDiv = document.getElementById('email-error');
1014
  const emailStatusDiv = document.getElementById('email-status');
1015
 
1016
- let clientInstance;
 
1017
  let debounceTimer;
1018
  let countUpIntervalId; // For count-up timer
1019
 
@@ -1029,32 +1034,23 @@
1029
 
1030
  // --- Gradio Client Initialization ---
1031
  async function initClient() {
1032
- let gradioClientClass;
1033
- try {
1034
- const module = await import("https://esm.sh/@gradio/client");
1035
- gradioClientClass = module.Client || module.default;
1036
-
1037
- if (!gradioClientClass || typeof gradioClientClass.connect !== 'function') {
1038
- console.error("Gradio module loaded but does not export a 'Client' class with a static 'connect' method or a usable default export for connection:", module);
1039
- throw new Error("Gradio client library export structure not recognizable. This may indicate an extremely new or old Gradio client version has a different API, or a bundling issue with esm.sh.");
1040
- }
1041
- } catch (importError) {
1042
- console.error("Failed to dynamically import Gradio client module from esm.sh:", importError);
1043
- showError("Could not load core prediction library. Please ensure a stable internet connection and try clearing your browser's cache (hard refresh). If the issue persists, the CDN (esm.sh) or Gradio client library itself might have changed or be experiencing issues.");
1044
- if (predictBtn) predictBtn.disabled = true;
1045
- return;
1046
- }
1047
-
1048
  try {
1049
- clientInstance = await gradioClientClass.connect(GRADIO_PREDICTION_TARGET_ID);
 
1050
  console.log("Gradio client for prediction initialized successfully directly to space ID:", GRADIO_PREDICTION_TARGET_ID);
1051
 
1052
- // Initial check for button state after client is ready
 
 
 
 
 
 
1053
  updatePredictButtonState();
1054
 
1055
  } catch (error) {
1056
- console.error("Failed to initialize Gradio client for prediction (direct attempt):", error);
1057
- showError(`Failed to connect to the classifier service. Please check if the Hugging Face Gradio Space (${GRADIO_HF_SPACE_RAW_URL}) is active and publicly accessible. Error details: ${error.message}`);
1058
  if (predictBtn) predictBtn.disabled = true;
1059
  }
1060
  }
@@ -1358,9 +1354,9 @@
1358
  }
1359
 
1360
  async function classifySequence() {
1361
- if (!clientInstance) {
1362
  showGeneralError("Classifier service is not ready. Please wait a moment and try again.");
1363
- console.error("Gradio client instance is not initialized when classifySequence called.");
1364
  updatePredictButtonState();
1365
  return;
1366
  }
@@ -1424,7 +1420,7 @@
1424
  let limeFeaturesForPdfFromPrediction = [];
1425
 
1426
  try {
1427
- const classificationResult = await clientInstance.predict("/predict", [currentSequence]);
1428
  console.log("Gradio Result (Prediction):", classificationResult);
1429
 
1430
  if (!classificationResult || !classificationResult.data || typeof classificationResult.data[0] !== 'string') {
@@ -1551,7 +1547,7 @@
1551
  }
1552
  }
1553
 
1554
- // --- Send Email Function (Frontend to Backend) ---
1555
  async function sendEmailWithReport(email, pdfBlob, fileName, sequence, prediction, confidence) {
1556
  if (!emailStatusDiv) return;
1557
 
@@ -1563,43 +1559,50 @@
1563
 
1564
  reader.onloadend = async function() {
1565
  const base64data = reader.result;
1566
- const base64PdfContent = base64data.split(',')[1]; // Strip "data:application/pdf;base64," prefix
 
 
 
 
 
 
 
1567
 
1568
  try {
1569
- // **IMPORTANT**: Replace with the actual URL of your backend email sending service.
1570
- // For local testing, it might be 'http://localhost:3000/api/send-report-email'
1571
- // For deployment, it will be your deployed backend's URL.
1572
- const backendEndpoint = 'http://localhost:3000/api/send-report-email';
1573
-
1574
- const response = await fetch(backendEndpoint, {
1575
- method: 'POST',
1576
- headers: {
1577
- 'Content-Type': 'application/json',
1578
- },
1579
- body: JSON.stringify({
1580
- recipientEmail: email,
1581
- pdfContent: base64PdfContent, // Base64 encoded PDF
1582
- fileName: fileName,
1583
- sequence: sequence, // Pass sequence for email body
1584
- prediction: prediction, // Pass prediction for email body
1585
- confidence: confidence // Pass confidence for email body
1586
- }),
1587
- });
1588
-
1589
- if (response.ok) {
1590
- const result = await response.json();
1591
- emailStatusDiv.textContent = result.message || 'Email sent successfully!';
1592
- emailStatusDiv.style.color = 'var(--success-color)';
 
1593
  } else {
1594
- const errorData = await response.json();
1595
- emailStatusDiv.textContent = `Failed to send email: ${errorData.message || response.statusText}`;
1596
  emailStatusDiv.style.color = 'var(--clear-button-bg)';
1597
- console.error('Backend email error:', errorData);
1598
  }
 
1599
  } catch (error) {
1600
  emailStatusDiv.textContent = `Error communicating with email service: ${error.message}`;
1601
  emailStatusDiv.style.color = 'var(--clear-button-bg)';
1602
- console.error('Frontend network error sending email:', error);
1603
  }
1604
  };
1605
 
 
992
  </address>
993
  </footer>
994
 
995
+ <script type="module"> // Changed to type="module" for dynamic import of Gradio Client
996
+ import { Client } from "https://esm.sh/@gradio/client";
997
+
998
  const GRADIO_HF_SPACE_RAW_URL = "https://huggingface.co/spaces/nonzeroexit/AMP-Classifier";
999
  const GRADIO_PREDICTION_TARGET_ID = "nonzeroexit/AMP-Classifier";
1000
+ // *** IMPORTANT: Replace with YOUR actual Hugging Face Space ID for email sending ***
1001
+ const GRADIO_EMAIL_TARGET_ID = "your-username/amp-email-sender";
1002
 
1003
  const sequenceInput = document.getElementById('sequence');
1004
  const charCount = document.getElementById('char-count');
 
1017
  const emailErrorDiv = document.getElementById('email-error');
1018
  const emailStatusDiv = document.getElementById('email-status');
1019
 
1020
+ let clientInstancePrediction; // For the prediction space
1021
+ let clientInstanceEmail; // For the email sending space
1022
  let debounceTimer;
1023
  let countUpIntervalId; // For count-up timer
1024
 
 
1034
 
1035
  // --- Gradio Client Initialization ---
1036
  async function initClient() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1037
  try {
1038
+ // Connect to your prediction space
1039
+ clientInstancePrediction = await Client.connect(GRADIO_PREDICTION_TARGET_ID);
1040
  console.log("Gradio client for prediction initialized successfully directly to space ID:", GRADIO_PREDICTION_TARGET_ID);
1041
 
1042
+ // Connect to your email sending space
1043
+ // If the email sender is in the SAME space as the classifier, you might only need one client and call its function.
1044
+ // For now, assume separate spaces for clarity.
1045
+ clientInstanceEmail = await Client.connect(GRADIO_EMAIL_TARGET_ID);
1046
+ console.log("Gradio client for email sender initialized successfully directly to space ID:", GRADIO_EMAIL_TARGET_ID);
1047
+
1048
+ // Initial check for button state after clients are ready
1049
  updatePredictButtonState();
1050
 
1051
  } catch (error) {
1052
+ console.error("Failed to initialize Gradio client for prediction or email:", error);
1053
+ showError(`Failed to connect to a service. Please check if Hugging Face Gradio Spaces (${GRADIO_PREDICTION_TARGET_ID}, ${GRADIO_EMAIL_TARGET_ID}) are active and publicly accessible. Error details: ${error.message}`);
1054
  if (predictBtn) predictBtn.disabled = true;
1055
  }
1056
  }
 
1354
  }
1355
 
1356
  async function classifySequence() {
1357
+ if (!clientInstancePrediction) { // Use clientInstancePrediction here
1358
  showGeneralError("Classifier service is not ready. Please wait a moment and try again.");
1359
+ console.error("Gradio client instance for prediction is not initialized when classifySequence called.");
1360
  updatePredictButtonState();
1361
  return;
1362
  }
 
1420
  let limeFeaturesForPdfFromPrediction = [];
1421
 
1422
  try {
1423
+ const classificationResult = await clientInstancePrediction.predict("/predict", [currentSequence]); // Use clientInstancePrediction here
1424
  console.log("Gradio Result (Prediction):", classificationResult);
1425
 
1426
  if (!classificationResult || !classificationResult.data || typeof classificationResult.data[0] !== 'string') {
 
1547
  }
1548
  }
1549
 
1550
+ // --- Send Email Function (Frontend to Gradio Backend) ---
1551
  async function sendEmailWithReport(email, pdfBlob, fileName, sequence, prediction, confidence) {
1552
  if (!emailStatusDiv) return;
1553
 
 
1559
 
1560
  reader.onloadend = async function() {
1561
  const base64data = reader.result;
1562
+ const base64PdfContent = base64data.split(',')[1]; // Strip "data:application/pdf;base64," prefix [13]
1563
+
1564
+ if (!clientInstanceEmail) {
1565
+ emailStatusDiv.textContent = 'Email service is not ready. Please try again.';
1566
+ emailStatusDiv.style.color = 'var(--clear-button-bg)';
1567
+ console.error("Gradio email client instance is not initialized.");
1568
+ return;
1569
+ }
1570
 
1571
  try {
1572
+ // Call the Gradio API endpoint for sending email
1573
+ // The endpoint is typically "/run/your_function_name" for gr.Interface
1574
+ const emailResult = await clientInstanceEmail.predict(
1575
+ "/send_email_report", // This must match the name of the Python function in app.py's gr.Interface
1576
+ [
1577
+ email, // recipient_email: str
1578
+ base64PdfContent, // pdf_content_base64: str
1579
+ fileName, // file_name: str
1580
+ sequence, // sequence: str
1581
+ prediction, // prediction: str
1582
+ confidence // confidence: float
1583
+ ]
1584
+ );
1585
+
1586
+ console.log("Email sending Gradio result:", emailResult);
1587
+
1588
+ if (emailResult && emailResult.data && emailResult.data[0]) {
1589
+ const statusMessage = emailResult.data[0];
1590
+ if (statusMessage.includes("successfully")) {
1591
+ emailStatusDiv.textContent = statusMessage;
1592
+ emailStatusDiv.style.color = 'var(--success-color)';
1593
+ } else {
1594
+ emailStatusDiv.textContent = `Failed to send email: ${statusMessage}`;
1595
+ emailStatusDiv.style.color = 'var(--clear-button-bg)';
1596
+ }
1597
  } else {
1598
+ emailStatusDiv.textContent = 'Failed to send email: Unexpected response from service.';
 
1599
  emailStatusDiv.style.color = 'var(--clear-button-bg)';
 
1600
  }
1601
+
1602
  } catch (error) {
1603
  emailStatusDiv.textContent = `Error communicating with email service: ${error.message}`;
1604
  emailStatusDiv.style.color = 'var(--clear-button-bg)';
1605
+ console.error('Frontend error calling Gradio email API:', error);
1606
  }
1607
  };
1608