Upload 2 files
Browse files- Weather.py +49 -0
- requirements.txt +5 -0
Weather.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 7 |
+
|
| 8 |
+
# ----------------------
|
| 9 |
+
# Load model + processor
|
| 10 |
+
# ----------------------
|
| 11 |
+
processor = AutoImageProcessor.from_pretrained("prithivMLmods/Weather-Image-Classification")
|
| 12 |
+
model = AutoModelForImageClassification.from_pretrained("prithivMLmods/Weather-Image-Classification")
|
| 13 |
+
|
| 14 |
+
# ----------------------
|
| 15 |
+
# Inference function
|
| 16 |
+
# ----------------------
|
| 17 |
+
def classify_weather(image_input):
|
| 18 |
+
# Only NumPy array supported for Gradio input
|
| 19 |
+
if isinstance(image_input, np.ndarray):
|
| 20 |
+
image = Image.fromarray(image_input.astype('uint8')).convert("RGB")
|
| 21 |
+
else:
|
| 22 |
+
raise TypeError("Only NumPy array input is supported for this Gradio interface.")
|
| 23 |
+
|
| 24 |
+
# Preprocess
|
| 25 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 26 |
+
|
| 27 |
+
# Inference
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**inputs)
|
| 30 |
+
logits = outputs.logits
|
| 31 |
+
predicted_class_id = logits.argmax(-1).item()
|
| 32 |
+
predicted_label = model.config.id2label[predicted_class_id]
|
| 33 |
+
|
| 34 |
+
return predicted_label
|
| 35 |
+
|
| 36 |
+
# ----------------------
|
| 37 |
+
# Gradio interface
|
| 38 |
+
# ----------------------
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=classify_weather,
|
| 41 |
+
inputs=gr.Image(type="numpy"), # NumPy array input
|
| 42 |
+
outputs=gr.Label(num_top_classes=5, label="Weather Condition"),
|
| 43 |
+
title="Weather Image Classification",
|
| 44 |
+
description="Upload an image to classify the weather condition (sun, rain, snow, fog, or clouds)."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Launch the app
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|
| 4 |
+
Pillow
|
| 5 |
+
requests
|