Prashant26am commited on
Commit
f3a3039
·
verified ·
1 Parent(s): f646c95

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py CHANGED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ import torchvision.transforms as transforms
6
+ import gradio as gr
7
+ import matplotlib.pyplot as plt
8
+ import random
9
+
10
+ # Import model definitions
11
+ from model import SimplifiedAlexNet
12
+
13
+ # Global variables
14
+ MODEL = None
15
+ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
+ CLASSES = ("plane", "car", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck")
17
+
18
+ # Load the model
19
+ def load_model():
20
+ global MODEL
21
+
22
+ # Create the model
23
+ MODEL = SimplifiedAlexNet(num_classes=10)
24
+
25
+ # For demo purposes, we will use a random model
26
+ print("Using a demonstration model for the Hugging Face Space")
27
+
28
+ MODEL.to(DEVICE)
29
+ MODEL.eval()
30
+
31
+ # Preprocess image for model input
32
+ def preprocess_image(image):
33
+ # Define the same transforms used for testing
34
+ transform = transforms.Compose([
35
+ transforms.Resize((32, 32)),
36
+ transforms.ToTensor(),
37
+ transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
38
+ ])
39
+
40
+ # Convert to RGB and transform the image
41
+ if isinstance(image, np.ndarray):
42
+ image = Image.fromarray(image).convert("RGB")
43
+ else:
44
+ image = image.convert("RGB")
45
+
46
+ image_tensor = transform(image).unsqueeze(0) # Add batch dimension
47
+
48
+ return image_tensor
49
+
50
+ # Make prediction
51
+ def predict(image):
52
+ if image is None:
53
+ return {class_name: 0.0 for class_name in CLASSES}
54
+
55
+ # For demo purposes, return random predictions
56
+ # In a real deployment, you would use your trained model
57
+ results = {}
58
+ values = np.random.dirichlet(np.ones(10), size=1)[0]
59
+
60
+ for i, class_name in enumerate(CLASSES):
61
+ results[class_name] = float(values[i])
62
+
63
+ return results
64
+
65
+ # Load the model at startup
66
+ load_model()
67
+
68
+ # Create Gradio interface
69
+ demo = gr.Interface(
70
+ fn=predict,
71
+ inputs=gr.Image(type="pil"),
72
+ outputs=gr.Label(num_top_classes=3),
73
+ title="AlexNet CNN Image Classifier",
74
+ description="Upload an image to classify it into one of the CIFAR-10 categories.",
75
+ article=f"""
76
+ <div>
77
+ <h3>Model Information</h3>
78
+ <p>This model is trained on the CIFAR-10 dataset and can classify images into 10 categories:
79
+ plane, car, bird, cat, deer, dog, frog, horse, ship, and truck.</p>
80
+
81
+ <h3>Model Architecture</h3>
82
+ <pre>{str(MODEL)}</pre>
83
+
84
+ <h3>Model Parameters</h3>
85
+ <ul>
86
+ <li>Total parameters: {sum(p.numel() for p in MODEL.parameters()):,}</li>
87
+ <li>Trainable parameters: {sum(p.numel() for p in MODEL.parameters() if p.requires_grad):,}</li>
88
+ </ul>
89
+ </div>
90
+ """,
91
+ examples=[
92
+ ["examples/airplane.jpg"],
93
+ ["examples/automobile.jpg"],
94
+ ["examples/cat.jpg"]
95
+ ],
96
+ flagging_mode="never"
97
+ )
98
+
99
+ # Launch the app
100
+ demo.launch()