Update app.py
Browse files
app.py
CHANGED
|
@@ -8,24 +8,24 @@ processor = AutoImageProcessor.from_pretrained("prithivMLmods/Weather-Image-Clas
|
|
| 8 |
model = AutoModelForImageClassification.from_pretrained("prithivMLmods/Weather-Image-Classification")
|
| 9 |
|
| 10 |
# Inference function
|
| 11 |
-
def classify_weather(
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
inputs = processor(images=[image_input], return_tensors="pt")
|
| 15 |
|
|
|
|
| 16 |
with torch.no_grad():
|
| 17 |
outputs = model(**inputs)
|
| 18 |
logits = outputs.logits.squeeze()
|
| 19 |
probs = torch.softmax(logits, dim=-1).tolist()
|
| 20 |
labels = [model.config.id2label[i] for i in range(len(probs))]
|
| 21 |
return dict(zip(labels, probs))
|
| 22 |
-
except Exception:
|
| 23 |
-
return {"Error":
|
| 24 |
|
| 25 |
# Gradio interface
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=classify_weather,
|
| 28 |
-
inputs=gr.Image(type="
|
| 29 |
outputs=gr.Label(num_top_classes=5, label="Weather Condition"),
|
| 30 |
title="Weather Image Classification",
|
| 31 |
description="Upload an image to classify the weather condition (sun, rain, snow, fog, or clouds)."
|
|
|
|
| 8 |
model = AutoModelForImageClassification.from_pretrained("prithivMLmods/Weather-Image-Classification")
|
| 9 |
|
| 10 |
# Inference function
|
| 11 |
+
def classify_weather(image_path):
|
| 12 |
try:
|
| 13 |
+
image = Image.open(image_path).convert("RGB")
|
|
|
|
| 14 |
|
| 15 |
+
inputs = processor(images=[image], return_tensors="pt")
|
| 16 |
with torch.no_grad():
|
| 17 |
outputs = model(**inputs)
|
| 18 |
logits = outputs.logits.squeeze()
|
| 19 |
probs = torch.softmax(logits, dim=-1).tolist()
|
| 20 |
labels = [model.config.id2label[i] for i in range(len(probs))]
|
| 21 |
return dict(zip(labels, probs))
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return {"Error": str(e)}
|
| 24 |
|
| 25 |
# Gradio interface
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=classify_weather,
|
| 28 |
+
inputs=gr.Image(type="filepath"), # ✅ File path input
|
| 29 |
outputs=gr.Label(num_top_classes=5, label="Weather Condition"),
|
| 30 |
title="Weather Image Classification",
|
| 31 |
description="Upload an image to classify the weather condition (sun, rain, snow, fog, or clouds)."
|