my_new_project / app.py
Deepaksiwania12's picture
Update app.py
461d9de
# import gradio as gr
# import torch
# from torchvision import transforms
# from PIL import Image
# # Load your pre-trained model
# # Example: Replace 'YourModelClass' with your actual model class
# from your_module import YourModelClass
# model = YourModelClass()
# model.load_state_dict(torch.load('path/to/your/model_weights.pth'))
# model.eval()
# # Define transformations for input images
# transform = transforms.Compose([
# transforms.Resize((224, 224)),
# transforms.ToTensor(),
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
# ])
# # Define the function for making predictions
# def predict_leaf_health(image):
# try:
# # Open and preprocess the image
# img = Image.open(image)
# img = transform(img)
# img = img.unsqueeze(0) # Add batch dimension
# # Make prediction
# with torch.no_grad():
# output = model(img)
# prediction = torch.argmax(output).item()
# # Map the prediction to class labels (modify as needed)
# class_labels = {0: 'Unhealthy', 1: 'Healthy'}
# result = class_labels.get(prediction, 'Unknown')
# return result
# except Exception as e:
# return f"Error: {str(e)}"
# # Create Gradio Interface
# iface = gr.Interface(
# fn=predict_leaf_health,
# inputs=gr.Image(type="file", label="Upload an image of a plant leaf"),
# outputs="text",
# theme="default",
# title="Plant Leaf Health Classification",
# description="Upload an image of a plant leaf to classify its health.",
# )
# # Launch the Gradio Interface
# iface.launch()
import torch
from torchvision import transforms
from PIL import Image
# Define your model class
class YourModelClass(torch.nn.Module):
# Define your model architecture here
# Create an instance of your model
model = YourModelClass()
# Load the pre-trained weights
model.load_state_dict(torch.load('model_weights.pth'))
model.eval()
def predict_leaf_health(image_path):
try:
# Open and preprocess the image
img = Image.open(image_path)
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
img = transform(img)
img = img.unsqueeze(0) # Add batch dimension
# Make prediction
with torch.no_grad():
output = model(img)
prediction = torch.argmax(output).item()
# Map the prediction to class labels (modify as needed)
class_labels = {0: 'Unhealthy', 1: 'Healthy'}
result = class_labels.get(prediction, 'Unknown')
return result
except Exception as e:
return f"Error: {str(e)}"