Nausad commited on
Commit
90bc1a9
·
verified ·
1 Parent(s): 65c75ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -74
app.py CHANGED
@@ -95,82 +95,81 @@ transform = transforms.Compose([
95
  def index():
96
  return render_template("index.html")
97
 
98
- @app.route("/predict", methods=["POST"])
99
  @app.route("/predict", methods=["POST"])
100
  def predict():
101
- if "file" not in request.files:
102
- return jsonify({"error": "No file uploaded"}), 400
103
-
104
- ```
105
- file = request.files["file"]
106
-
107
- if file.filename == "":
108
- return jsonify({"error": "Empty filename"}), 400
109
-
110
- try:
111
- data = file.read()
112
-
113
- print("=" * 60)
114
- print("Filename:", file.filename)
115
- print("Content-Type:", file.content_type)
116
- print("Bytes length:", len(data))
117
-
118
- img = Image.open(io.BytesIO(data))
119
- print("Format:", img.format)
120
- print("Size:", img.size)
121
-
122
- img = img.convert("RGB")
123
- print("Step 1: RGB conversion done")
124
-
125
- tensor = transform(img).unsqueeze(0).to(device)
126
- print("Step 2: Transform done")
127
- print("Tensor shape:", tensor.shape)
128
- print("Tensor dtype:", tensor.dtype)
129
-
130
- t0 = time.time()
131
-
132
- with torch.no_grad():
133
- print("Step 3: Starting inference")
134
- logits = model(tensor)
135
- print("Step 4: Inference complete")
136
-
137
- probs = F.softmax(logits, dim=1).squeeze(0)
138
- print("Step 5: Softmax complete")
139
-
140
- ms = round((time.time() - t0) * 1000, 2)
141
-
142
- results = sorted(
143
- [
144
- {
145
- "class": CLASSES[i],
146
- "probability": float(probs[i])
147
- }
148
- for i in range(10)
149
- ],
150
- key=lambda x: x["probability"],
151
- reverse=True
152
- )
153
-
154
- print("Step 6: Results generated")
155
- print("Top class:", results[0]["class"])
156
- print("Top probability:", results[0]["probability"])
157
-
158
- return jsonify({
159
- "top_class": results[0]["class"],
160
- "top_probability": results[0]["probability"],
161
- "all_predictions": results,
162
- "inference_ms": ms
163
- })
164
-
165
- except Exception as e:
166
- import traceback
167
- print("ERROR OCCURRED:")
168
- traceback.print_exc()
169
-
170
- return jsonify({
171
- "error": str(e)
172
- }), 500
173
- ```
174
 
175
 
176
  @app.route("/model-info")
 
95
  def index():
96
  return render_template("index.html")
97
 
 
98
  @app.route("/predict", methods=["POST"])
99
  def predict():
100
+ if "file" not in request.files:
101
+ return jsonify({"error": "No file uploaded"}), 400
102
+
103
+ ```
104
+ file = request.files["file"]
105
+
106
+ if file.filename == "":
107
+ return jsonify({"error": "Empty filename"}), 400
108
+
109
+ try:
110
+ data = file.read()
111
+
112
+ print("=" * 60)
113
+ print("Filename:", file.filename)
114
+ print("Content-Type:", file.content_type)
115
+ print("Bytes length:", len(data))
116
+
117
+ img = Image.open(io.BytesIO(data))
118
+ print("Format:", img.format)
119
+ print("Size:", img.size)
120
+
121
+ img = img.convert("RGB")
122
+ print("Step 1: RGB conversion done")
123
+
124
+ tensor = transform(img).unsqueeze(0).to(device)
125
+ print("Step 2: Transform done")
126
+ print("Tensor shape:", tensor.shape)
127
+ print("Tensor dtype:", tensor.dtype)
128
+
129
+ t0 = time.time()
130
+
131
+ with torch.no_grad():
132
+ print("Step 3: Starting inference")
133
+ logits = model(tensor)
134
+ print("Step 4: Inference complete")
135
+
136
+ probs = F.softmax(logits, dim=1).squeeze(0)
137
+ print("Step 5: Softmax complete")
138
+
139
+ ms = round((time.time() - t0) * 1000, 2)
140
+
141
+ results = sorted(
142
+ [
143
+ {
144
+ "class": CLASSES[i],
145
+ "probability": float(probs[i])
146
+ }
147
+ for i in range(10)
148
+ ],
149
+ key=lambda x: x["probability"],
150
+ reverse=True
151
+ )
152
+
153
+ print("Step 6: Results generated")
154
+ print("Top class:", results[0]["class"])
155
+ print("Top probability:", results[0]["probability"])
156
+
157
+ return jsonify({
158
+ "top_class": results[0]["class"],
159
+ "top_probability": results[0]["probability"],
160
+ "all_predictions": results,
161
+ "inference_ms": ms
162
+ })
163
+
164
+ except Exception as e:
165
+ import traceback
166
+ print("ERROR OCCURRED:")
167
+ traceback.print_exc()
168
+
169
+ return jsonify({
170
+ "error": str(e)
171
+ }), 500
172
+ ```
173
 
174
 
175
  @app.route("/model-info")