Update app.py
Browse files
app.py
CHANGED
|
@@ -2,69 +2,68 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
import timm
|
| 5 |
-
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
# ------------------------ Model Definition ------------------------
|
| 9 |
class CycloneIntensityModel(nn.Module):
|
| 10 |
def __init__(self):
|
| 11 |
-
super(
|
| 12 |
-
self.
|
| 13 |
-
in_features = self.
|
| 14 |
-
self.
|
| 15 |
|
| 16 |
def forward(self, x):
|
| 17 |
-
return self.
|
| 18 |
|
| 19 |
-
#
|
| 20 |
model = CycloneIntensityModel()
|
| 21 |
model_path = "cyclone_intensity.pth"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
model.eval()
|
| 25 |
-
except Exception as e:
|
| 26 |
-
raise RuntimeError(f"Unable to load model at {model_path}: {e}")
|
| 27 |
|
| 28 |
-
#
|
| 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 "
|
| 39 |
elif 96 <= intensity <= 110:
|
| 40 |
-
return "
|
| 41 |
elif 111 <= intensity <= 129:
|
| 42 |
-
return "
|
| 43 |
elif 130 <= intensity <= 156:
|
| 44 |
-
return "
|
| 45 |
else:
|
| 46 |
-
return "
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
tensor = transform(image).unsqueeze(0)
|
| 54 |
with torch.no_grad():
|
| 55 |
intensity = model(tensor).item()
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
# ------------------------ Gradio
|
| 59 |
-
with gr.Blocks(
|
| 60 |
-
gr.Markdown("# Cyclone Intensity Predictor
|
| 61 |
with gr.Row():
|
| 62 |
-
|
| 63 |
with gr.Column():
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
if __name__
|
| 70 |
-
demo.launch(
|
|
|
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
import timm
|
| 5 |
+
from torchvision import transforms
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
# ------------------------ Model Definition ------------------------
|
| 9 |
class CycloneIntensityModel(nn.Module):
|
| 10 |
def __init__(self):
|
| 11 |
+
super().__init__()
|
| 12 |
+
self.backbone = timm.create_model('resnet18', pretrained=True)
|
| 13 |
+
in_features = self.backbone.fc.in_features
|
| 14 |
+
self.backbone.fc = nn.Linear(in_features, 1)
|
| 15 |
|
| 16 |
def forward(self, x):
|
| 17 |
+
return self.backbone(x)
|
| 18 |
|
| 19 |
+
# Instantiate & load
|
| 20 |
model = CycloneIntensityModel()
|
| 21 |
model_path = "cyclone_intensity.pth"
|
| 22 |
+
model.load_state_dict(torch.load(model_path, map_location='cpu'))
|
| 23 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# ------------------------ Transforms & Classifier ------------------------
|
| 26 |
transform = transforms.Compose([
|
| 27 |
transforms.Resize((224, 224)),
|
| 28 |
+
transforms.ToTensor(),
|
| 29 |
])
|
| 30 |
|
|
|
|
|
|
|
| 31 |
def classify_cyclone(intensity: float) -> str:
|
| 32 |
if 74 <= intensity <= 95:
|
| 33 |
+
return "Category 1 – Some damage expected."
|
| 34 |
elif 96 <= intensity <= 110:
|
| 35 |
+
return "Category 2 – Extensive damage expected."
|
| 36 |
elif 111 <= intensity <= 129:
|
| 37 |
+
return "Category 3 – Devastating damage expected."
|
| 38 |
elif 130 <= intensity <= 156:
|
| 39 |
+
return "Category 4 – Catastrophic damage expected."
|
| 40 |
else:
|
| 41 |
+
return "Category 5 – Catastrophic damage expected."
|
|
|
|
| 42 |
|
| 43 |
+
# ------------------------ Prediction fn ------------------------
|
| 44 |
+
def predict_intensity(img: Image.Image):
|
| 45 |
+
img = img.convert("RGB")
|
| 46 |
+
tensor = transform(img).unsqueeze(0)
|
|
|
|
| 47 |
with torch.no_grad():
|
| 48 |
intensity = model(tensor).item()
|
| 49 |
+
category = classify_cyclone(intensity)
|
| 50 |
+
return f"{intensity:.2f} mph", category
|
| 51 |
|
| 52 |
+
# ------------------------ Gradio UI ------------------------
|
| 53 |
+
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("## Cyclone Intensity Predictor")
|
| 55 |
with gr.Row():
|
| 56 |
+
input_img = gr.Image(type="pil", label="Upload Satellite Image")
|
| 57 |
with gr.Column():
|
| 58 |
+
btn = gr.Button("Predict")
|
| 59 |
+
output_int = gr.Textbox(label="Predicted Wind Speed")
|
| 60 |
+
output_cat = gr.Textbox(label="Cyclone Category")
|
| 61 |
+
|
| 62 |
+
btn.click(
|
| 63 |
+
fn=predict_intensity,
|
| 64 |
+
inputs=input_img,
|
| 65 |
+
outputs=[output_int, output_cat]
|
| 66 |
+
)
|
| 67 |
|
| 68 |
+
if __name__=="__main__":
|
| 69 |
+
demo.launch()
|