Nausad commited on
Commit
fb1400b
·
verified ·
1 Parent(s): e6a0477

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -46
app.py CHANGED
@@ -95,82 +95,50 @@ transform = transforms.Compose([
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")
176
  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
  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
+
 
 
 
128
  with torch.no_grad():
129
  print("Step 3: Starting inference")
130
  logits = model(tensor)
131
  print("Step 4: Inference complete")
132
+
133
  probs = F.softmax(logits, dim=1).squeeze(0)
134
  print("Step 5: Softmax complete")
135
+
136
+ return jsonify({"success": True})
137
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  except Exception as e:
139
  import traceback
 
140
  traceback.print_exc()
141
+ return jsonify({"error": str(e)}), 500
 
 
 
 
 
142
 
143
  @app.route("/model-info")
144
  def model_info():