Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import timm
|
| 5 |
+
import torchvision.transforms as transforms
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# ------------------------ Model Definition ------------------------
|
| 9 |
+
class CycloneIntensityModel(nn.Module):
|
| 10 |
+
def __init__(self):
|
| 11 |
+
super(CycloneIntensityModel, self).__init__()
|
| 12 |
+
self.model = timm.create_model('resnet18', pretrained=True)
|
| 13 |
+
in_features = self.model.fc.in_features
|
| 14 |
+
self.model.fc = nn.Linear(in_features, 1)
|
| 15 |
+
|
| 16 |
+
def forward(self, x):
|
| 17 |
+
return self.model(x)
|
| 18 |
+
|
| 19 |
+
# Load trained model
|
| 20 |
+
model = CycloneIntensityModel()
|
| 21 |
+
model_path = "cyclone_intensity.pth"
|
| 22 |
+
try:
|
| 23 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 24 |
+
model.eval()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
raise RuntimeError(f"Unable to load model at {model_path}: {e}")
|
| 27 |
+
|
| 28 |
+
# Image transformations
|
| 29 |
+
transform = transforms.Compose([
|
| 30 |
+
transforms.Resize((224, 224)),
|
| 31 |
+
transforms.ToTensor()
|
| 32 |
+
])
|
| 33 |
+
|
| 34 |
+
# Prediction and classification
|
| 35 |
+
|
| 36 |
+
def classify_cyclone(intensity: float) -> str:
|
| 37 |
+
if 74 <= intensity <= 95:
|
| 38 |
+
return "Less Cyclone (Category 1): Some damage expected."
|
| 39 |
+
elif 96 <= intensity <= 110:
|
| 40 |
+
return "Moderate Cyclone (Category 2): Extensive damage expected."
|
| 41 |
+
elif 111 <= intensity <= 129:
|
| 42 |
+
return "Heavy Cyclone (Category 3): Devastating damage expected."
|
| 43 |
+
elif 130 <= intensity <= 156:
|
| 44 |
+
return "Severe Cyclone (Category 4): Catastrophic damage expected."
|
| 45 |
+
else:
|
| 46 |
+
return "Extreme Cyclone (Category 5): Catastrophic damage expected."
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def predict(image: Image.Image):
|
| 50 |
+
if image is None:
|
| 51 |
+
return "", "No image provided."
|
| 52 |
+
image = image.convert("RGB")
|
| 53 |
+
tensor = transform(image).unsqueeze(0)
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
intensity = model(tensor).item()
|
| 56 |
+
return f"{intensity:.2f}", classify_cyclone(intensity)
|
| 57 |
+
|
| 58 |
+
# ------------------------ Gradio Interface ------------------------
|
| 59 |
+
with gr.Blocks(theme="default") as demo:
|
| 60 |
+
gr.Markdown("# Cyclone Intensity Predictor \nUpload a satellite image to predict cyclone intensity.")
|
| 61 |
+
with gr.Row():
|
| 62 |
+
img_in = gr.Image(type="pil", label="Satellite Image", tool="editor")
|
| 63 |
+
with gr.Column():
|
| 64 |
+
intensity_out = gr.Textbox(label="Predicted Intensity (mph)")
|
| 65 |
+
category_out = gr.Textbox(label="Cyclone Category")
|
| 66 |
+
predict_btn = gr.Button("Predict")
|
| 67 |
+
predict_btn.click(fn=predict, inputs=img_in, outputs=[intensity_out, category_out])
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|