-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
138 lines (115 loc) · 4.42 KB
/
app.py
File metadata and controls
138 lines (115 loc) · 4.42 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
#!/usr/bin/env python3
"""
AstroPi Explorer Dashboard - Railway Optimized Version
"""
import os
import sys
import threading
from datetime import datetime
from flask import Flask, jsonify, render_template
# Create Flask app
app = Flask(__name__)
# Global variables for complex features (loaded in background)
complex_features_loaded = False
loading_status = "Starting..."
def load_complex_features():
"""Load complex features in background thread"""
global complex_features_loaded, loading_status
try:
loading_status = "Loading computer vision libraries..."
print("🔄 Loading OpenCV and computer vision libraries...")
# Import heavy dependencies
import cv2
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
loading_status = "Loading image processing modules..."
print("🔄 Loading image processing modules...")
# Import additional modules
from exif import Image
import pickle
import hashlib
import statistics
from collections import Counter
loading_status = "Initializing ML models..."
print("🔄 Initializing ML models...")
# Try to load ML models (optional)
try:
import tensorflow as tf
print("✅ TensorFlow loaded successfully")
except ImportError:
print("⚠️ TensorFlow not available - ML features disabled")
loading_status = "Loading cache data..."
print("🔄 Loading cache data...")
# Try to load cache (this might take time)
try:
# This would be the cache loading logic from the original app
# For now, just simulate it
import time
time.sleep(2) # Simulate cache loading
print("✅ Cache loading completed")
except Exception as e:
print(f"⚠️ Cache loading failed: {e}")
complex_features_loaded = True
loading_status = "All features loaded successfully!"
print("🎉 All complex features loaded successfully!")
except Exception as e:
loading_status = f"Loading failed: {str(e)}"
print(f"❌ Failed to load complex features: {e}")
import traceback
traceback.print_exc()
@app.route('/')
def index():
"""Main dashboard route"""
try:
return render_template('dashboard_v2_clean.html')
except Exception as e:
return f"Template error: {str(e)}", 500
@app.route('/health')
def health():
"""Health check endpoint for Railway"""
try:
return jsonify({
"status": "healthy",
"message": "AstroPi Explorer Dashboard is running",
"timestamp": datetime.now().isoformat(),
"python_version": sys.version,
"port": os.environ.get('PORT', 'not_set'),
"complex_features_loaded": complex_features_loaded,
"loading_status": loading_status
})
except Exception as e:
return jsonify({
"status": "error",
"message": f"Health check failed: {str(e)}"
}), 500
@app.route('/status')
def status():
"""Detailed status endpoint"""
return jsonify({
"app_status": "running",
"complex_features_loaded": complex_features_loaded,
"loading_status": loading_status,
"timestamp": datetime.now().isoformat()
})
@app.route('/test')
def test():
"""Simple test endpoint"""
return jsonify({"message": "Test endpoint working", "status": "ok"})
if __name__ == '__main__':
print("🚀 Starting AstroPi Explorer Dashboard (Railway Optimized)...")
print("🔧 Initializing Flask application...")
# Get port from environment variable (Railway) or use default
port = int(os.environ.get('PORT', 5003))
debug_mode = os.environ.get('FLASK_ENV', 'development') == 'development'
print(f"📊 Port: {port}")
print(f"🔧 Debug mode: {debug_mode}")
print(f"📊 Open your browser and go to: http://localhost:{port}")
print("✅ Flask app ready to start...")
# Start complex features loading in background
print("🔄 Starting background loading of complex features...")
feature_thread = threading.Thread(target=load_complex_features, daemon=True)
feature_thread.start()
# Start the app
app.run(debug=debug_mode, host='0.0.0.0', port=port)