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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -19
app.py CHANGED
@@ -95,37 +95,83 @@ transform = transforms.Compose([
95
  def index():
96
  return render_template("index.html")
97
 
 
98
  @app.route("/predict", methods=["POST"])
99
  def predict():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- if "file" not in request.files:
102
- return jsonify({"error": "No file uploaded"}), 400
103
 
104
- file = request.files["file"]
 
 
 
105
 
106
- try:
107
- data = file.read()
108
 
109
- print("Filename:", file.filename)
110
- print("Content-Type:", file.content_type)
111
- print("Bytes length:", len(data))
 
112
 
113
- img = Image.open(io.BytesIO(data))
114
- print("Format:", img.format)
115
- print("Size:", img.size)
116
 
117
- img = img.convert("RGB")
118
 
119
- tensor = transform(img).unsqueeze(0).to(device)
 
 
 
 
 
 
 
 
 
 
120
 
121
- with torch.no_grad():
122
- logits = model(tensor)
 
123
 
124
- return jsonify({"success": True})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
- except Exception as e:
127
- print("ERROR:", repr(e))
128
- return jsonify({"error": str(e)}), 500
129
 
130
  @app.route("/model-info")
131
  def model_info():
 
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")
177
  def model_info():