Spaces:
Paused
Paused
File size: 4,347 Bytes
401184c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
"""
Install dependencies for professional hallucination detector
Run this before using the new hallucination detection features
"""
import subprocess
import sys
def install_dependencies():
"""Install required packages for hallucination detection"""
print("=" * 60)
print("π§ Installing Hallucination Detector Dependencies")
print("=" * 60)
packages = [
"sentence-transformers>=2.2.0",
"scikit-learn>=1.3.0",
"torch>=2.0.0",
"transformers>=4.30.0"
]
for package in packages:
print(f"\nπ¦ Installing {package}...")
try:
subprocess.check_call([
sys.executable, "-m", "pip", "install", package
])
print(f"β
{package} installed successfully")
except subprocess.CalledProcessError as e:
print(f"β Failed to install {package}: {e}")
return False
print("\n" + "=" * 60)
print("β
All dependencies installed successfully!")
print("=" * 60)
return True
def download_models():
"""Pre-download models to cache"""
print("\n" + "=" * 60)
print("π§ Downloading Models (this may take a few minutes)...")
print("=" * 60)
try:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# Download Vectara model
print("\nπ₯ Downloading Vectara HHEM model...")
try:
AutoTokenizer.from_pretrained("vectara/hallucination_evaluation_model")
AutoModelForSequenceClassification.from_pretrained("vectara/hallucination_evaluation_model")
print("β
Vectara model downloaded")
except Exception as e:
print(f"β οΈ Vectara model download failed: {e}")
# Download NLI model
print("\nπ₯ Downloading DeBERTa NLI model...")
try:
from transformers import pipeline
pipeline("text-classification", model="microsoft/deberta-large-mnli")
print("β
NLI model downloaded")
except Exception as e:
print(f"β οΈ NLI model download failed: {e}")
print("\n" + "=" * 60)
print("β
Models downloaded successfully!")
print("=" * 60)
except ImportError as e:
print(f"β Cannot download models: {e}")
print("Please install transformers first")
return False
return True
def test_installation():
"""Test if installation works"""
print("\n" + "=" * 60)
print("π§ͺ Testing Installation...")
print("=" * 60)
try:
from hallucination_detector import HybridHallucinationDetector
print("\nπ Creating test detector...")
detector = HybridHallucinationDetector(use_vectara=True, use_nli=True)
print("\nπ Running test detection...")
test_doc = "Python is a programming language created by Guido van Rossum in 1991."
test_gen = "Python was created by Guido van Rossum."
result = detector.detect(test_gen, test_doc)
print(f"\nβ
Test result: {result}")
print("\n" + "=" * 60)
print("β
Installation test passed!")
print("=" * 60)
return True
except Exception as e:
print(f"\nβ Installation test failed: {e}")
print("\nPlease check the error messages above.")
return False
if __name__ == "__main__":
print("\nπ Starting installation...\n")
# Step 1: Install dependencies
if not install_dependencies():
print("\nβ Installation failed at dependency stage")
sys.exit(1)
# Step 2: Download models
if not download_models():
print("\nβ οΈ Model download had issues, but you can continue")
# Step 3: Test installation
if test_installation():
print("\n" + "=" * 60)
print("π Installation Complete!")
print("=" * 60)
print("\nYou can now use the professional hallucination detector.")
print("\nTo test it, run:")
print(" python test_hallucination_detector.py")
print("\n" + "=" * 60)
else:
print("\nβ Installation completed with errors")
print("The system will fallback to LLM-based detection")
sys.exit(1)
|