|
|
import gradio as gr |
|
|
from PIL import Image |
|
|
import torch |
|
|
from transformers import AutoImageProcessor, AutoModelForImageClassification |
|
|
|
|
|
|
|
|
processor = AutoImageProcessor.from_pretrained("prithivMLmods/Weather-Image-Classification") |
|
|
model = AutoModelForImageClassification.from_pretrained("prithivMLmods/Weather-Image-Classification") |
|
|
|
|
|
|
|
|
def classify_weather(image_path): |
|
|
try: |
|
|
image = Image.open(image_path).convert("RGB") |
|
|
|
|
|
inputs = processor(images=[image], return_tensors="pt") |
|
|
with torch.no_grad(): |
|
|
outputs = model(**inputs) |
|
|
logits = outputs.logits.squeeze() |
|
|
probs = torch.softmax(logits, dim=-1).tolist() |
|
|
labels = [model.config.id2label[i] for i in range(len(probs))] |
|
|
return dict(zip(labels, probs)) |
|
|
except Exception as e: |
|
|
return {"Error": str(e)} |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=classify_weather, |
|
|
inputs=gr.Image(type="filepath"), |
|
|
outputs=gr.Label(num_top_classes=5, label="Weather Condition"), |
|
|
title="Weather Image Classification", |
|
|
description="Upload an image to classify the weather condition (sun, rain, snow, fog, or clouds)." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch(show_error=True) |
|
|
|