Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import wandb
|
| 3 |
+
def predict_image(image):
|
| 4 |
+
image = Image.fromarray(image).convert("RGB")
|
| 5 |
+
image = transform(image)
|
| 6 |
+
image = image.unsqueeze(0) # Add batch dimension
|
| 7 |
+
|
| 8 |
+
with torch.no_grad():
|
| 9 |
+
outputs = model(image)
|
| 10 |
+
probabilities = F.softmax(outputs, dim=1)
|
| 11 |
+
_, predicted = torch.max(outputs, 1)
|
| 12 |
+
|
| 13 |
+
predicted_label = classes[predicted.item()]
|
| 14 |
+
predicted_probability = probabilities[0][predicted.item()].item()
|
| 15 |
+
|
| 16 |
+
# Log to W&B
|
| 17 |
+
wandb.log({"image": [wandb.Image(image, caption="Input Image")],
|
| 18 |
+
"predicted_label": predicted_label,
|
| 19 |
+
"predicted_probability": predicted_probability})
|
| 20 |
+
|
| 21 |
+
return predicted_label, predicted_probability
|
| 22 |
+
|
| 23 |
+
# Class labels
|
| 24 |
+
classes = ['dew', 'fog_smog', 'frost', 'glaze', 'hail', 'lightning', 'rain', 'rainbow', 'rime', 'sandstorm', 'snow']
|
| 25 |
+
|
| 26 |
+
# Create Gradio interface
|
| 27 |
+
interface = gr.Interface(fn=predict_image,
|
| 28 |
+
inputs=gr.components.Image(),
|
| 29 |
+
outputs=[gr.components.Textbox(label="Predicted Label"), gr.components.Textbox(label="Prediction Probability")],title="Weather Detection App")
|
| 30 |
+
|
| 31 |
+
# Launch the interface
|
| 32 |
+
interface.launch()
|
| 33 |
+
|
| 34 |
+
# Save model checkpoint to W&B
|
| 35 |
+
torch.save(model.state_dict(), 'weather_model.pth')
|
| 36 |
+
wandb.save('weather_model.pth')
|