Update app.py
Browse files
app.py
CHANGED
@@ -2,30 +2,31 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from tensorflow import keras
|
4 |
|
5 |
-
# Load the pre-trained
|
6 |
-
|
|
|
7 |
|
8 |
-
def
|
|
|
|
|
|
|
9 |
try:
|
10 |
# Ensure the input data has the correct types and ranges
|
11 |
-
|
12 |
-
proton_density = float(proton_density)
|
13 |
-
temperature = float(temperature)
|
14 |
|
15 |
# Ensure the input data is within valid ranges
|
16 |
-
if
|
17 |
-
return {"error": "
|
18 |
|
19 |
# Convert input data to a NumPy array
|
20 |
-
input_data = np.array([
|
21 |
|
22 |
-
# Make predictions using the loaded model
|
23 |
-
|
24 |
|
25 |
# Format the predictions as a dictionary
|
26 |
result = {
|
27 |
-
"
|
28 |
-
"predicted_field_magnitude": float(predictions[0, 1])
|
29 |
}
|
30 |
|
31 |
return result
|
@@ -33,11 +34,21 @@ def predict(year, proton_density, temperature):
|
|
33 |
except Exception as e:
|
34 |
return {"error": str(e)}
|
35 |
|
36 |
-
|
37 |
-
|
|
|
38 |
inputs=["text", "text", "text"], # Year, Proton Density, Temperature
|
39 |
outputs="json",
|
40 |
-
title="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
)
|
42 |
|
43 |
-
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
from tensorflow import keras
|
4 |
|
5 |
+
# Load the pre-trained models
|
6 |
+
speed_model = keras.models.load_model('omni_rnn_0.h5')
|
7 |
+
power_model = keras.models.load_model('power_model.h5') # Load the second model for power prediction
|
8 |
|
9 |
+
def predict_speed(year, proton_density, temperature):
|
10 |
+
# Your existing speed prediction code here
|
11 |
+
|
12 |
+
def predict_power(speed):
|
13 |
try:
|
14 |
# Ensure the input data has the correct types and ranges
|
15 |
+
speed = float(speed)
|
|
|
|
|
16 |
|
17 |
# Ensure the input data is within valid ranges
|
18 |
+
if speed < 0:
|
19 |
+
return {"error": "Speed should be non-negative"}
|
20 |
|
21 |
# Convert input data to a NumPy array
|
22 |
+
input_data = np.array([speed]).reshape(1, -1)
|
23 |
|
24 |
+
# Make predictions using the loaded power model
|
25 |
+
power_prediction = power_model.predict(input_data)
|
26 |
|
27 |
# Format the predictions as a dictionary
|
28 |
result = {
|
29 |
+
"predicted_power": float(power_prediction[0])
|
|
|
30 |
}
|
31 |
|
32 |
return result
|
|
|
34 |
except Exception as e:
|
35 |
return {"error": str(e)}
|
36 |
|
37 |
+
# Create the Gradio interfaces
|
38 |
+
speed_interface = gr.Interface(
|
39 |
+
fn=predict_speed,
|
40 |
inputs=["text", "text", "text"], # Year, Proton Density, Temperature
|
41 |
outputs="json",
|
42 |
+
title="Solar Wind Speed Prediction"
|
43 |
+
)
|
44 |
+
|
45 |
+
power_interface = gr.Interface(
|
46 |
+
fn=predict_power,
|
47 |
+
inputs="text", # Speed prediction from the previous step
|
48 |
+
outputs="json",
|
49 |
+
title="Solar Wind Power Generation Prediction"
|
50 |
)
|
51 |
|
52 |
+
# Launch the interfaces
|
53 |
+
speed_interface.launch()
|
54 |
+
power_interface.launch()
|