File size: 2,817 Bytes
2eeb731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import joblib
import numpy as np
import pandas as pd

# Load the model and scaler
model_info = joblib.load('model/mars_pressure_model.joblib')
model = model_info['model']
scaler = model_info['scaler']
feature_columns = model_info['feature_columns']

def predict_pressure(max_ground_temp, min_ground_temp, max_air_temp, min_air_temp, month):
    # Create a feature dictionary
    features = {
        'max_ground_temp(°C)': float(max_ground_temp),
        'min_ground_temp(°C)': float(min_ground_temp),
        'max_air_temp(°C)': float(max_air_temp),
        'min_air_temp(°C)': float(min_air_temp),
        'earth_month': float(month),
        'earth_day': 15  # Use middle of month as default
    }
    
    # Add season features
    season = (month % 12) // 3
    seasons = ['Winter', 'Spring', 'Summer', 'Fall']
    for s in seasons:
        features[f'season_{s}'] = 1.0 if seasons[season] == s else 0.0
    
    # Create DataFrame with correct column order
    X = pd.DataFrame([features])[feature_columns]
    
    # Scale features
    X_scaled = scaler.transform(X)
    
    # Make prediction
    prediction = model.predict(X_scaled)[0]
    
    # Calculate uncertainty
    uncertainty = model_info['metrics']['prediction_std']
    
    return (
        f"{prediction:.1f} Pa",
        f"({prediction-uncertainty:.1f} to {prediction+uncertainty:.1f} Pa)",
        f"±{uncertainty:.1f} Pa"
    )

# Create the interface
iface = gr.Interface(
    fn=predict_pressure,
    inputs=[
        gr.Slider(-70, 20, value=-13, label="Maximum Ground Temperature (°C)"),
        gr.Slider(-100, -50, value=-75, label="Minimum Ground Temperature (°C)"),
        gr.Slider(-60, 30, value=2, label="Maximum Air Temperature (°C)"),
        gr.Slider(-140, -5, value=-80, label="Minimum Air Temperature (°C)"),
        gr.Slider(1, 12, step=1, value=6, label="Month (1-12)")
    ],
    outputs=[
        gr.Label(label="Predicted Atmospheric Pressure"),
        gr.Label(label="Prediction Range"),
        gr.Label(label="Uncertainty")
    ],
    title="Mars Atmospheric Pressure Predictor",
    description="""
    Predict atmospheric pressure on Mars based on environmental measurements.
    Default values are set to typical conditions observed in the dataset.
    The model has an R² score of 0.397 and provides uncertainty estimates.
    """,
    examples=[
        [-13, -75, 2, -80, 6],  # Average conditions
        [11, -52, 24, -8, 3],   # Warmest conditions
        [-67, -100, -61, -136, 12]  # Coldest conditions
    ]
)

# Launch the app with specific configurations for HuggingFace Spaces
if __name__ == "__main__":
    iface.launch(
        server_name="0.0.0.0",
        server_port=7860,  # Default port for HuggingFace Spaces
        share=False,
        show_error=True
    )