Advertisement
xosski

Ai img execution trial

Dec 6th, 2024 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. const Tesseract = require('tesseract.js');
  2. const fs = require('fs');
  3.  
  4. // Simulated compliance check function
  5. function checkCompliance(question) {
  6. const allowedCategories = [
  7. "general knowledge",
  8. "education and learning",
  9. "creative writing",
  10. "technical assistance (e.g., coding)",
  11. "general problem-solving",
  12. ];
  13.  
  14. const restrictedCategories = [
  15. "medical diagnosis or prescriptions",
  16. "legal advice or strategies",
  17. "hateful or harmful content",
  18. "explicit or adult content",
  19. "illegal activities",
  20. "misinformation or manipulation",
  21. "copyright violations",
  22. ];
  23.  
  24. const restrictedKeywords = [
  25. "harm",
  26. "illegal",
  27. "violence",
  28. "explicit",
  29. "prescribe",
  30. "scam",
  31. "fraud",
  32. "copyright",
  33. "misinform",
  34. ];
  35.  
  36. for (let keyword of restrictedKeywords) {
  37. if (question.toLowerCase().includes(keyword) && !question.toLowerCase().includes("coding") && !question.toLowerCase().includes("script")) {
  38. return { compliance: false, reason: `Contains restricted keyword: '${keyword}'` };
  39. }
  40. }
  41.  
  42. for (let category of restrictedCategories) {
  43. if (question.toLowerCase().includes(category)) {
  44. return { compliance: false, reason: `Appears related to restricted category: '${category}'` };
  45. }
  46. }
  47.  
  48. return { compliance: true, reason: "The query is within policy guidelines." };
  49. }
  50.  
  51. // Function to extract text from image using Tesseract.js
  52. function extractTextFromImage(imagePath) {
  53. return new Promise((resolve, reject) => {
  54. Tesseract.recognize(imagePath, 'eng', {
  55. logger: (m) => console.log(m), // Optional: shows progress
  56. })
  57. .then(({ data: { text } }) => {
  58. resolve(text);
  59. })
  60. .catch((err) => {
  61. reject(err);
  62. });
  63. });
  64. }
  65.  
  66. // Example usage
  67. const imagePath = 'path_to_your_image.jpg'; // Replace with your image path
  68.  
  69. extractTextFromImage(imagePath)
  70. .then((text) => {
  71. const questionsToEvaluate = text.split('\n'); // Assuming each question is on a new line
  72.  
  73. questionsToEvaluate.forEach((question) => {
  74. if (question.trim()) {
  75. const result = checkCompliance(question);
  76. console.log(`Question: ${question}`);
  77. console.log(`Compliant: ${result.compliance}`);
  78. console.log(`Reason: ${result.reason}`);
  79. console.log('-'.repeat(50));
  80. }
  81. });
  82. })
  83. .catch((error) => {
  84. console.error('Error during text extraction:', error);
  85. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement