Spaces:
Paused
Paused
| """ | |
| 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) | |