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)