-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose.py
More file actions
286 lines (242 loc) · 8.47 KB
/
Copy pathdiagnose.py
File metadata and controls
286 lines (242 loc) · 8.47 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Diagnostic script to identify common issues
Run this to troubleshoot problems with the Sentiment Analysis system
"""
import sys
import os
def check_python_version():
"""Check Python version"""
print("\n" + "="*60)
print("1. Checking Python Version")
print("="*60)
version = sys.version_info
print(f"Python {version.major}.{version.minor}.{version.micro}")
if version.major >= 3 and version.minor >= 8:
print("✓ Python version is compatible (3.8+)")
return True
else:
print("✗ Python version is too old. Need 3.8 or higher")
return False
def check_virtual_env():
"""Check if running in virtual environment"""
print("\n" + "="*60)
print("2. Checking Virtual Environment")
print("="*60)
in_venv = hasattr(sys, 'real_prefix') or (
hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix
)
if in_venv:
print(f"✓ Running in virtual environment: {sys.prefix}")
return True
else:
print("⚠ Not running in virtual environment")
print(" Run: venv\\Scripts\\activate")
return False
def check_dependencies():
"""Check if required packages are installed"""
print("\n" + "="*60)
print("3. Checking Dependencies")
print("="*60)
required = [
'flask',
'flask_cors',
'transformers',
'torch',
'sklearn',
'pandas',
'numpy',
'nltk',
'textblob',
'requests',
'PIL', # pillow
'cv2', # opencv-python
'pytesseract',
'bs4', # beautifulsoup4
'youtube_transcript_api',
]
missing = []
for package in required:
try:
__import__(package)
print(f"✓ {package}")
except ImportError:
print(f"✗ {package} - NOT INSTALLED")
missing.append(package)
if missing:
print(f"\n⚠ Missing packages: {', '.join(missing)}")
print(" Run: pip install -r requirements.txt")
return False
else:
print("\n✓ All required packages are installed")
return True
def check_models():
"""Check if models can be imported"""
print("\n" + "="*60)
print("4. Checking Model Imports")
print("="*60)
try:
from models.sentiment_analyzer import SentimentAnalyzer
print("✓ SentimentAnalyzer")
except Exception as e:
print(f"✗ SentimentAnalyzer: {e}")
return False
try:
from models.fact_checker import FactChecker
print("✓ FactChecker")
except Exception as e:
print(f"✗ FactChecker: {e}")
return False
try:
from models.image_analyzer import ImageAnalyzer
print("✓ ImageAnalyzer")
except Exception as e:
print(f"✗ ImageAnalyzer: {e}")
return False
try:
from models.video_analyzer import VideoAnalyzer
print("✓ VideoAnalyzer")
except Exception as e:
print(f"✗ VideoAnalyzer: {e}")
return False
try:
from models.url_analyzer import URLAnalyzer
print("✓ URLAnalyzer")
except Exception as e:
print(f"✗ URLAnalyzer: {e}")
return False
print("\n✓ All models can be imported")
return True
def check_flask_app():
"""Check if Flask app can be loaded"""
print("\n" + "="*60)
print("5. Checking Flask Application")
print("="*60)
try:
import app
print("✓ Flask app loaded successfully")
return True
except Exception as e:
print(f"✗ Flask app failed to load: {e}")
return False
def check_backend_server():
"""Check if backend server is running"""
print("\n" + "="*60)
print("6. Checking Backend Server")
print("="*60)
try:
import requests
response = requests.get('http://localhost:5000/api/health', timeout=5)
if response.status_code == 200:
print("✓ Backend server is running on port 5000")
print(f" Response: {response.json()}")
return True
else:
print(f"⚠ Backend server responded with status {response.status_code}")
return False
except requests.exceptions.ConnectionError:
print("✗ Backend server is NOT running")
print(" Start it with: python app.py")
return False
except Exception as e:
print(f"✗ Error checking backend: {e}")
return False
def check_tesseract():
"""Check if Tesseract OCR is installed"""
print("\n" + "="*60)
print("7. Checking Tesseract OCR (Optional)")
print("="*60)
try:
import pytesseract
from PIL import Image
# Try to get Tesseract version
version = pytesseract.get_tesseract_version()
print(f"✓ Tesseract OCR is installed (version {version})")
return True
except pytesseract.TesseractNotFoundError:
print("⚠ Tesseract OCR is not installed")
print(" This is optional but recommended for image analysis")
print(" Download: https://github.com/UB-Mannheim/tesseract/wiki")
return False
except Exception as e:
print(f"⚠ Could not check Tesseract: {e}")
return False
def test_simple_analysis():
"""Test a simple text analysis"""
print("\n" + "="*60)
print("8. Testing Simple Text Analysis")
print("="*60)
try:
from models.sentiment_analyzer import SentimentAnalyzer
from models.fact_checker import FactChecker
analyzer = SentimentAnalyzer()
checker = FactChecker()
test_text = "This is a great product!"
sentiment = analyzer.analyze(test_text)
print(f"✓ Sentiment analysis works: {sentiment['label']}")
fact = checker.check(test_text)
print(f"✓ Fact checking works: {fact['label']}")
return True
except Exception as e:
print(f"✗ Analysis failed: {e}")
import traceback
print(traceback.format_exc())
return False
def check_ports():
"""Check if required ports are available"""
print("\n" + "="*60)
print("9. Checking Port Availability")
print("="*60)
import socket
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
port_5000 = is_port_in_use(5000)
port_3000 = is_port_in_use(3000)
if port_5000:
print("✓ Port 5000 is in use (backend should be running)")
else:
print("⚠ Port 5000 is free (backend is not running)")
if port_3000:
print("✓ Port 3000 is in use (frontend should be running)")
else:
print("⚠ Port 3000 is free (frontend is not running)")
return True
def main():
"""Run all diagnostic checks"""
print("="*60)
print("SENTIMENT ANALYSIS DIAGNOSTIC TOOL")
print("="*60)
print("\nThis tool will check your system for common issues.\n")
results = []
# Run all checks
results.append(("Python Version", check_python_version()))
results.append(("Virtual Environment", check_virtual_env()))
results.append(("Dependencies", check_dependencies()))
results.append(("Model Imports", check_models()))
results.append(("Flask App", check_flask_app()))
results.append(("Backend Server", check_backend_server()))
results.append(("Tesseract OCR", check_tesseract()))
results.append(("Simple Analysis", test_simple_analysis()))
results.append(("Port Check", check_ports()))
# Summary
print("\n" + "="*60)
print("DIAGNOSTIC SUMMARY")
print("="*60)
passed = sum(1 for _, result in results if result)
total = len(results)
for check_name, result in results:
status = "✓ PASS" if result else "✗ FAIL"
print(f"{status:10} - {check_name}")
print(f"\nPassed: {passed}/{total}")
if passed == total:
print("\n🎉 All checks passed! Your system is ready.")
else:
print("\n⚠ Some checks failed. Please fix the issues above.")
print("\nCommon solutions:")
print("1. Activate virtual environment: venv\\Scripts\\activate")
print("2. Install dependencies: pip install -r requirements.txt")
print("3. Start backend server: python app.py")
print("4. Start frontend server: cd frontend && npm start")
print("\nFor more help, see TROUBLESHOOTING.md")
if __name__ == "__main__":
main()