-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (60 loc) · 2.48 KB
/
app.py
File metadata and controls
78 lines (60 loc) · 2.48 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
# app.py
import os
import tensorflow as tf
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from flask import Flask, request, render_template, url_for
from src.preprocess import fetch_and_process_data
# --- Initialization ---
app = Flask(__name__)
# --- Build an absolute path to the model file ---
base_dir = os.path.dirname(os.path.abspath(__file__))
# *** We are now pointing to the file you just moved ***
MODEL_FILENAME = 'astrowave_cnn_final.h5'
MODEL_PATH = os.path.join(base_dir, 'models', MODEL_FILENAME)
print(f"Attempting to load model from: {MODEL_PATH}")
# --- Load the trained model ---
print("Loading trained model...")
model = tf.keras.models.load_model(MODEL_PATH)
print("Model loaded successfully!")
# --- Define Routes ---
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
gps_time = float(request.form['gps_time'])
print(f"Processing data for GPS time: {gps_time}...")
q_transform_data = fetch_and_process_data(gps_time)
if q_transform_data is None:
return render_template('result.html',
prediction_text='Error: Could not fetch or process data for this time.',
image_file=None)
image_for_model = np.expand_dims(q_transform_data, axis=0)
image_for_model = np.expand_dims(image_for_model, axis=-1)
prediction_prob = model.predict(image_for_model)[0][0]
prediction = (prediction_prob > 0.5)
if prediction == 1:
prediction_text = f"SIGNAL DETECTED! (Confidence: {prediction_prob*100:.2f}%)"
else:
prediction_text = f"Noise Detected. (Signal Confidence: {prediction_prob*100:.2f}%)"
static_dir = os.path.join(base_dir, 'static', 'images')
os.makedirs(static_dir, exist_ok=True)
image_filename = f'q_transform_{gps_time}.png'
image_path = os.path.join(static_dir, image_filename)
plt.figure(figsize=(10, 5))
plt.imshow(q_transform_data, aspect='auto', origin='lower', cmap='viridis')
plt.title(f'Q-Transform for GPS Time: {gps_time}')
plt.ylabel('Frequency Bins')
plt.xlabel('Time Bins')
plt.savefig(image_path)
plt.close()
image_url = url_for('static', filename=f'images/{image_filename}')
return render_template('result.html',
prediction_text=prediction_text,
image_file=image_url)
# --- Run the App ---
if __name__ == "__main__":
app.run(debug=True)