File size: 5,832 Bytes
5d99375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
"""
Model Verification Script

This script tests all models used in the application to ensure they're working correctly.
"""

import sys
from typing import Dict, Any

def test_sentence_transformers():
    """Test sentence transformer models."""
    results = {}
    
    models_to_test = [
        "sentence-transformers/all-MiniLM-L6-v2",
        "BAAI/bge-m3"
    ]
    
    try:
        from sentence_transformers import SentenceTransformer
        
        for model_name in models_to_test:
            try:
                print(f"Testing {model_name}...")
                model = SentenceTransformer(model_name)
                embeddings = model.encode(["This is a test sentence."], show_progress_bar=False)
                
                if embeddings is not None and len(embeddings) > 0:
                    results[model_name] = "βœ… PASS"
                    print(f"  βœ… {model_name} working correctly")
                else:
                    results[model_name] = "❌ FAIL - No embeddings generated"
                    print(f"  ❌ {model_name} failed to generate embeddings")
                    
            except Exception as e:
                results[model_name] = f"❌ FAIL - {str(e)}"
                print(f"  ❌ {model_name} failed: {e}")
                
    except ImportError:
        results["sentence-transformers"] = "❌ FAIL - Package not installed"
        print("❌ sentence-transformers package not installed")
    
    return results

def test_cross_encoder():
    """Test cross-encoder model."""
    results = {}
    model_name = "cross-encoder/ms-marco-MiniLM-L-6-v2"
    
    try:
        from sentence_transformers import CrossEncoder
        
        print(f"Testing {model_name}...")
        model = CrossEncoder(model_name)
        scores = model.predict([("test query", "test document")])
        
        if scores is not None and len(scores) > 0:
            results[model_name] = "βœ… PASS"
            print(f"  βœ… {model_name} working correctly")
        else:
            results[model_name] = "❌ FAIL - No scores generated"
            print(f"  ❌ {model_name} failed to generate scores")
            
    except ImportError:
        results["cross-encoder"] = "❌ FAIL - sentence-transformers not installed"
        print("❌ sentence-transformers package not installed")
    except Exception as e:
        results[model_name] = f"❌ FAIL - {str(e)}"
        print(f"  ❌ {model_name} failed: {e}")
    
    return results

def test_transformers_pipeline():
    """Test transformers pipeline."""
    results = {}
    model_name = "MoritzLaurer/deberta-v3-base-zeroshot-v2.0"
    
    try:
        from transformers import pipeline
        
        print(f"Testing {model_name}...")
        classifier = pipeline(
            "zero-shot-classification",
            model=model_name,
            device=-1  # CPU
        )
        
        result = classifier(
            "This is a test sentence about policy.",
            ["policy", "technology", "sports"]
        )
        
        if result and 'labels' in result and len(result['labels']) > 0:
            results[model_name] = "βœ… PASS"
            print(f"  βœ… {model_name} working correctly")
        else:
            results[model_name] = "❌ FAIL - No classification result"
            print(f"  ❌ {model_name} failed to classify")
            
    except ImportError:
        results["transformers"] = "❌ FAIL - transformers package not installed"
        print("❌ transformers package not installed")
    except Exception as e:
        results[model_name] = f"❌ FAIL - {str(e)}"
        print(f"  ❌ {model_name} failed: {e}")
    
    return results

def test_application_modules():
    """Test that application modules can be imported."""
    results = {}
    
    modules_to_test = [
        "utils.encoding_input",
        "utils.loading_embeddings", 
        "utils.retrieve_n_rerank",
        "utils.sentiment_analysis",
        "utils.coherence_bbscore",
        "utils.model_generation",
        "utils.generation_streaming"
    ]
    
    for module_name in modules_to_test:
        try:
            __import__(module_name)
            results[module_name] = "βœ… PASS"
            print(f"βœ… {module_name} imported successfully")
        except ImportError as e:
            results[module_name] = f"❌ FAIL - {str(e)}"
            print(f"❌ {module_name} import failed: {e}")
        except Exception as e:
            results[module_name] = f"❌ FAIL - {str(e)}"
            print(f"❌ {module_name} error: {e}")
    
    return results

def main():
    """Run all tests."""
    print("πŸ§ͺ Model Verification Test Suite")
    print("=" * 50)
    
    all_results = {}
    
    print("\nπŸ“¦ Testing Sentence Transformers...")
    all_results.update(test_sentence_transformers())
    
    print("\nπŸ”„ Testing Cross Encoder...")
    all_results.update(test_cross_encoder())
    
    print("\nπŸ€– Testing Transformers Pipeline...")
    all_results.update(test_transformers_pipeline())
    
    print("\nπŸ“š Testing Application Modules...")
    all_results.update(test_application_modules())
    
    # Summary
    print("\n" + "=" * 50)
    print("πŸ“‹ TEST SUMMARY")
    print("=" * 50)
    
    passed = 0
    failed = 0
    
    for name, result in all_results.items():
        print(f"{result} {name}")
        if "βœ… PASS" in result:
            passed += 1
        else:
            failed += 1
    
    print(f"\nπŸ“Š Results: {passed} passed, {failed} failed")
    
    if failed == 0:
        print("πŸŽ‰ All tests passed! The application is ready to deploy.")
        return 0
    else:
        print("⚠️  Some tests failed. Please check the errors above.")
        return 1

if __name__ == "__main__":
    sys.exit(main())