shaheernges12 commited on
Commit
bb47ff9
·
verified ·
1 Parent(s): 527ba4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +853 -149
app.py CHANGED
@@ -1,154 +1,858 @@
1
- import io
2
- import base64
3
- import torch
4
- from PIL import Image
5
- from fastapi import FastAPI, File, UploadFile, HTTPException
6
- from fastapi.middleware.cors import CORSMiddleware
7
- from pydantic import BaseModel
8
- from ultralytics import YOLO
9
- from transformers import AutoImageProcessor, AutoModelForImageClassification
10
-
11
- # --- App Config ---
12
- app = FastAPI(
13
- title="Food & Vegetable AI API",
14
- description="Separate APIs for ViT Classification and YOLO Detection",
15
- version="2.1.0"
16
- )
17
-
18
- app.add_middleware(
19
- CORSMiddleware,
20
- allow_origins=["*"],
21
- allow_credentials=True,
22
- allow_methods=["*"],
23
- allow_headers=["*"],
24
- )
25
-
26
- class Base64ImageRequest(BaseModel):
27
- image: str
28
-
29
- # --- Model Loading ---
30
- print("Loading models...")
31
-
32
- try:
33
- vit_model = AutoModelForImageClassification.from_pretrained(
34
- "eslamxm/vit-base-food101"
35
- )
36
- vit_processor = AutoImageProcessor.from_pretrained(
37
- "eslamxm/vit-base-food101"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  )
39
 
40
- yolo_model = YOLO("yolo_fruits_and_vegetables_v3.pt")
41
 
42
- print("✓ Models loaded successfully")
43
- except Exception as e:
44
- print(f"✗ Model loading failed: {e}")
45
- vit_model = None
46
- yolo_model = None
47
-
48
-
49
- # --- Utility ---
50
- def load_image_from_bytes(image_bytes: bytes) -> Image.Image:
51
- return Image.open(io.BytesIO(image_bytes)).convert("RGB")
52
 
53
 
54
- # --- YOLO Detection ---
55
- def run_yolo(image: Image.Image):
56
- if not yolo_model:
57
- raise HTTPException(status_code=500, detail="YOLO model not loaded")
58
-
59
- results = yolo_model(image)
60
-
61
- detections = []
62
- summary = {}
63
-
64
- for r in results:
65
- for i in range(len(r.boxes)):
66
- label = yolo_model.names[int(r.boxes.cls[i])]
67
- detections.append({
68
- "label": label,
69
- "confidence": float(r.boxes.conf[i]),
70
- "bbox": r.boxes.xyxy[i].tolist()
71
- })
72
- summary[label] = summary.get(label, 0) + 1
73
-
74
- return {
75
- "detections": detections,
76
- "summary": summary
77
- }
78
-
79
-
80
- # --- ViT Classification ---
81
- def run_vit(image: Image.Image):
82
- if not vit_model:
83
- raise HTTPException(status_code=500, detail="ViT model not loaded")
84
-
85
- inputs = vit_processor(images=image, return_tensors="pt")
86
-
87
- with torch.no_grad():
88
- outputs = vit_model(**inputs)
89
-
90
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
91
- pred_id = probs.argmax().item()
92
-
93
- return {
94
- "label": vit_model.config.id2label[pred_id],
95
- "confidence": round(probs[0][pred_id].item(), 4)
96
- }
97
-
98
-
99
- # --- Routes ---
100
- @app.get("/")
101
- async def root():
102
- return {
103
- "message": "API running",
104
- "endpoints": ["/predict-vit", "/predict-yolo"]
105
- }
106
-
107
-
108
- # ---------- YOLO Endpoint ----------
109
- @app.post("/predict-yolo")
110
- async def predict_yolo(file: UploadFile = File(...)):
111
- try:
112
- image = load_image_from_bytes(await file.read())
113
- return {"detection": run_yolo(image)}
114
- except Exception as e:
115
- raise HTTPException(status_code=500, detail=str(e))
116
-
117
-
118
- # ---------- ViT Endpoint ----------
119
- @app.post("/predict-vit")
120
- async def predict_vit(file: UploadFile = File(...)):
121
- try:
122
- image = load_image_from_bytes(await file.read())
123
- return {"classification": run_vit(image)}
124
- except Exception as e:
125
- raise HTTPException(status_code=500, detail=str(e))
126
-
127
-
128
- # ---------- Base64 Support (optional) ----------
129
- @app.post("/predict-vit-base64")
130
- async def predict_vit_base64(request: Base64ImageRequest):
131
- try:
132
- _, encoded = request.image.split(",", 1) if "," in request.image else (None, request.image)
133
- image_bytes = base64.b64decode(encoded)
134
- image = load_image_from_bytes(image_bytes)
135
- return {"classification": run_vit(image)}
136
- except Exception as e:
137
- raise HTTPException(status_code=500, detail=f"ViT base64 error: {str(e)}")
138
-
139
-
140
- @app.post("/predict-yolo-base64")
141
- async def predict_yolo_base64(request: Base64ImageRequest):
142
- try:
143
- _, encoded = request.image.split(",", 1) if "," in request.image else (None, request.image)
144
- image_bytes = base64.b64decode(encoded)
145
- image = load_image_from_bytes(image_bytes)
146
- return {"detection": run_yolo(image)}
147
- except Exception as e:
148
- raise HTTPException(status_code=500, detail=f"YOLO base64 error: {str(e)}")
149
-
150
-
151
- # --- Run ---
152
- if __name__ == "__main__":
153
- import uvicorn
154
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import base64
3
+ import torch
4
+ import gradio as gr
5
+
6
+ from PIL import Image
7
+ from fastapi import FastAPI, File, UploadFile, HTTPException
8
+ from fastapi.middleware.cors import CORSMiddleware
9
+ from pydantic import BaseModel
10
+
11
+ from ultralytics import YOLO
12
+ from transformers import (
13
+ AutoImageProcessor,
14
+ AutoModelForImageClassification,
15
+ )
16
+
17
+
18
+ # ============================================================
19
+ # CONFIG
20
+ # ============================================================
21
+
22
+ VIT_MODEL_NAME = "eslamxm/vit-base-food101"
23
+ YOLO_MODEL_PATH = "yolo_fruits_and_vegetables_v3.pt"
24
+
25
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
26
+
27
+ print("=" * 60)
28
+ print("Food & Vegetable AI API")
29
+ print("=" * 60)
30
+ print(f"Device: {DEVICE}")
31
+
32
+
33
+ # ============================================================
34
+ # FASTAPI
35
+ # ============================================================
36
+
37
+ app = FastAPI(
38
+ title="Food & Vegetable AI API",
39
+ description="ViT Food Classification + YOLO Fruit/Vegetable Detection",
40
+ version="3.0.0",
41
+ )
42
+
43
+ app.add_middleware(
44
+ CORSMiddleware,
45
+ allow_origins=["*"],
46
+ allow_credentials=True,
47
+ allow_methods=["*"],
48
+ allow_headers=["*"],
49
+ )
50
+
51
+
52
+ # ============================================================
53
+ # REQUEST MODEL
54
+ # ============================================================
55
+
56
+ class Base64ImageRequest(BaseModel):
57
+ image: str
58
+
59
+
60
+ # ============================================================
61
+ # LOAD MODELS ONCE
62
+ # ============================================================
63
+
64
+ print("\nLoading ViT model...")
65
+
66
+ try:
67
+ vit_processor = AutoImageProcessor.from_pretrained(
68
+ VIT_MODEL_NAME
69
+ )
70
+
71
+ vit_model = AutoModelForImageClassification.from_pretrained(
72
+ VIT_MODEL_NAME
73
+ )
74
+
75
+ vit_model = vit_model.to(DEVICE)
76
+ vit_model.eval()
77
+
78
+ print("✓ ViT loaded")
79
+
80
+ except Exception as e:
81
+ print(f"✗ ViT loading failed: {e}")
82
+ vit_model = None
83
+ vit_processor = None
84
+
85
+
86
+ print("\nLoading YOLO model...")
87
+
88
+ try:
89
+ yolo_model = YOLO(YOLO_MODEL_PATH)
90
+
91
+ # Ultralytics automatically handles CUDA if available,
92
+ # but explicitly selecting the device is clearer.
93
+ if DEVICE == "cuda":
94
+ yolo_model.to("cuda")
95
+
96
+ print("✓ YOLO loaded")
97
+
98
+ except Exception as e:
99
+ print(f"✗ YOLO loading failed: {e}")
100
+ yolo_model = None
101
+
102
+
103
+ print("\nModels ready.")
104
+ print("=" * 60)
105
+
106
+
107
+ # ============================================================
108
+ # IMAGE UTILITY
109
+ # ============================================================
110
+
111
+ def load_image_from_bytes(image_bytes: bytes) -> Image.Image:
112
+ return Image.open(
113
+ io.BytesIO(image_bytes)
114
+ ).convert("RGB")
115
+
116
+
117
+ # ============================================================
118
+ # VIT CLASSIFICATION
119
+ # ============================================================
120
+
121
+ def run_vit(image: Image.Image):
122
+
123
+ if vit_model is None or vit_processor is None:
124
+ raise RuntimeError("ViT model is not loaded.")
125
+
126
+ inputs = vit_processor(
127
+ images=image,
128
+ return_tensors="pt"
129
+ )
130
+
131
+ inputs = {
132
+ key: value.to(DEVICE)
133
+ for key, value in inputs.items()
134
+ }
135
+
136
+ with torch.inference_mode():
137
+
138
+ outputs = vit_model(**inputs)
139
+
140
+ probabilities = torch.nn.functional.softmax(
141
+ outputs.logits,
142
+ dim=-1
143
+ )
144
+
145
+ predicted_id = probabilities.argmax(
146
+ dim=-1
147
+ ).item()
148
+
149
+ confidence = probabilities[
150
+ 0,
151
+ predicted_id
152
+ ].item()
153
+
154
+ label = vit_model.config.id2label[
155
+ predicted_id
156
+ ]
157
+
158
+ # Top 5 predictions
159
+ top_probs, top_ids = torch.topk(
160
+ probabilities,
161
+ k=min(5, probabilities.shape[-1]),
162
+ dim=-1
163
+ )
164
+
165
+ top_predictions = {}
166
+
167
+ for probability, class_id in zip(
168
+ top_probs[0],
169
+ top_ids[0]
170
+ ):
171
+ class_id = class_id.item()
172
+
173
+ top_predictions[
174
+ vit_model.config.id2label[class_id]
175
+ ] = round(
176
+ probability.item(),
177
+ 4
178
+ )
179
+
180
+ return {
181
+ "label": label,
182
+ "confidence": round(confidence, 4),
183
+ "top_predictions": top_predictions,
184
+ }
185
+
186
+
187
+ # ============================================================
188
+ # YOLO DETECTION
189
+ # ============================================================
190
+
191
+ def run_yolo(image: Image.Image):
192
+
193
+ if yolo_model is None:
194
+ raise RuntimeError("YOLO model is not loaded.")
195
+
196
+ results = yolo_model(
197
+ image,
198
+ verbose=False
199
+ )
200
+
201
+ detections = []
202
+ summary = {}
203
+
204
+ annotated_image = image.copy()
205
+
206
+ for result in results:
207
+
208
+ # ----------------------------------------------------
209
+ # DETECTIONS
210
+ # ----------------------------------------------------
211
+
212
+ for i in range(len(result.boxes)):
213
+
214
+ class_id = int(
215
+ result.boxes.cls[i].item()
216
+ )
217
+
218
+ label = yolo_model.names[class_id]
219
+
220
+ confidence = float(
221
+ result.boxes.conf[i].item()
222
+ )
223
+
224
+ bbox = [
225
+ round(float(x), 2)
226
+ for x in result.boxes.xyxy[i].tolist()
227
+ ]
228
+
229
+ detections.append({
230
+ "label": label,
231
+ "confidence": round(
232
+ confidence,
233
+ 4
234
+ ),
235
+ "bbox": bbox,
236
+ })
237
+
238
+ summary[label] = (
239
+ summary.get(label, 0) + 1
240
+ )
241
+
242
+ # ----------------------------------------------------
243
+ # ANNOTATED IMAGE
244
+ # ----------------------------------------------------
245
+
246
+ plotted = result.plot()
247
+
248
+ annotated_image = Image.fromarray(
249
+ plotted[..., ::-1]
250
+ )
251
+
252
+ return {
253
+ "detections": detections,
254
+ "summary": summary,
255
+ "annotated_image": annotated_image,
256
+ }
257
+
258
+
259
+ # ============================================================
260
+ # GRADIO FUNCTIONS
261
+ # ============================================================
262
+
263
+ def gradio_vit(image):
264
+
265
+ if image is None:
266
+ return {
267
+ "error": "Please upload an image."
268
+ }
269
+
270
+ try:
271
+ if not isinstance(image, Image.Image):
272
+ image = Image.fromarray(image)
273
+
274
+ return run_vit(image)
275
+
276
+ except Exception as e:
277
+ return {
278
+ "error": str(e)
279
+ }
280
+
281
+
282
+ def gradio_yolo(image):
283
+
284
+ if image is None:
285
+ return None, {
286
+ "error": "Please upload an image."
287
+ }
288
+
289
+ try:
290
+ if not isinstance(image, Image.Image):
291
+ image = Image.fromarray(image)
292
+
293
+ result = run_yolo(image)
294
+
295
+ return (
296
+ result["annotated_image"],
297
+ {
298
+ "detections": result["detections"],
299
+ "summary": result["summary"],
300
+ }
301
+ )
302
+
303
+ except Exception as e:
304
+
305
+ return (
306
+ None,
307
+ {
308
+ "error": str(e)
309
+ }
310
+ )
311
+
312
+
313
+ def gradio_both(image):
314
+
315
+ if image is None:
316
+ return None, None, {
317
+ "error": "Please upload an image."
318
+ }
319
+
320
+ try:
321
+
322
+ if not isinstance(image, Image.Image):
323
+ image = Image.fromarray(image)
324
+
325
+ # ViT
326
+ vit_result = run_vit(image)
327
+
328
+ # YOLO
329
+ yolo_result = run_yolo(image)
330
+
331
+ return (
332
+ yolo_result["annotated_image"],
333
+ vit_result,
334
+ {
335
+ "detections": yolo_result["detections"],
336
+ "summary": yolo_result["summary"],
337
+ }
338
+ )
339
+
340
+ except Exception as e:
341
+
342
+ return (
343
+ None,
344
+ None,
345
+ {
346
+ "error": str(e)
347
+ }
348
+ )
349
+
350
+
351
+ # ============================================================
352
+ # API ROUTES
353
+ # ============================================================
354
+
355
+ @app.get("/")
356
+ async def root():
357
+
358
+ return {
359
+ "message": "Food & Vegetable AI API running",
360
+ "device": DEVICE,
361
+ "vit_model": VIT_MODEL_NAME,
362
+ "yolo_model": YOLO_MODEL_PATH,
363
+ "endpoints": [
364
+ "/predict-vit",
365
+ "/predict-yolo",
366
+ "/predict-vit-base64",
367
+ "/predict-yolo-base64",
368
+ ],
369
+ }
370
+
371
+
372
+ @app.get("/health")
373
+ async def health():
374
+
375
+ return {
376
+ "status": "ok",
377
+ "device": DEVICE,
378
+ "vit_loaded": vit_model is not None,
379
+ "yolo_loaded": yolo_model is not None,
380
+ }
381
+
382
+
383
+ # ============================================================
384
+ # YOLO API
385
+ # ============================================================
386
+
387
+ @app.post("/predict-yolo")
388
+ async def predict_yolo(
389
+ file: UploadFile = File(...)
390
+ ):
391
+
392
+ try:
393
+
394
+ image_bytes = await file.read()
395
+
396
+ image = load_image_from_bytes(
397
+ image_bytes
398
+ )
399
+
400
+ result = run_yolo(image)
401
+
402
+ # Don't return PIL image through API
403
+ result.pop(
404
+ "annotated_image",
405
+ None
406
+ )
407
+
408
+ return {
409
+ "detection": result
410
+ }
411
+
412
+ except HTTPException:
413
+ raise
414
+
415
+ except Exception as e:
416
+
417
+ raise HTTPException(
418
+ status_code=500,
419
+ detail=str(e)
420
+ )
421
+
422
+
423
+ # ============================================================
424
+ # VIT API
425
+ # ============================================================
426
+
427
+ @app.post("/predict-vit")
428
+ async def predict_vit(
429
+ file: UploadFile = File(...)
430
+ ):
431
+
432
+ try:
433
+
434
+ image_bytes = await file.read()
435
+
436
+ image = load_image_from_bytes(
437
+ image_bytes
438
+ )
439
+
440
+ return {
441
+ "classification": run_vit(image)
442
+ }
443
+
444
+ except HTTPException:
445
+ raise
446
+
447
+ except Exception as e:
448
+
449
+ raise HTTPException(
450
+ status_code=500,
451
+ detail=str(e)
452
+ )
453
+
454
+
455
+ # ============================================================
456
+ # BASE64 VIT
457
+ # ============================================================
458
+
459
+ @app.post("/predict-vit-base64")
460
+ async def predict_vit_base64(
461
+ request: Base64ImageRequest
462
+ ):
463
+
464
+ try:
465
+
466
+ encoded = (
467
+ request.image.split(",", 1)[1]
468
+ if "," in request.image
469
+ else request.image
470
+ )
471
+
472
+ image_bytes = base64.b64decode(
473
+ encoded
474
+ )
475
+
476
+ image = load_image_from_bytes(
477
+ image_bytes
478
+ )
479
+
480
+ return {
481
+ "classification": run_vit(image)
482
+ }
483
+
484
+ except Exception as e:
485
+
486
+ raise HTTPException(
487
+ status_code=500,
488
+ detail=f"ViT base64 error: {str(e)}"
489
+ )
490
+
491
+
492
+ # ============================================================
493
+ # BASE64 YOLO
494
+ # ============================================================
495
+
496
+ @app.post("/predict-yolo-base64")
497
+ async def predict_yolo_base64(
498
+ request: Base64ImageRequest
499
+ ):
500
+
501
+ try:
502
+
503
+ encoded = (
504
+ request.image.split(",", 1)[1]
505
+ if "," in request.image
506
+ else request.image
507
+ )
508
+
509
+ image_bytes = base64.b64decode(
510
+ encoded
511
+ )
512
+
513
+ image = load_image_from_bytes(
514
+ image_bytes
515
+ )
516
+
517
+ result = run_yolo(image)
518
+
519
+ result.pop(
520
+ "annotated_image",
521
+ None
522
+ )
523
+
524
+ return {
525
+ "detection": result
526
+ }
527
+
528
+ except Exception as e:
529
+
530
+ raise HTTPException(
531
+ status_code=500,
532
+ detail=f"YOLO base64 error: {str(e)}"
533
+ )
534
+
535
+
536
+ # ============================================================
537
+ # GRADIO UI
538
+ # ============================================================
539
+
540
+ with gr.Blocks(
541
+ title="Food & Vegetable AI"
542
+ ) as demo:
543
+
544
+ gr.Markdown(
545
+ """
546
+ # 🍎 Food & Vegetable AI
547
+
548
+ Test the **ViT Food101 classifier** and
549
+ **YOLO fruit/vegetable detector**.
550
+
551
+ Upload a food image and run either model independently
552
+ or both models together.
553
+ """
554
+ )
555
+
556
+ with gr.Tab("🔬 Test Both Models"):
557
+
558
+ image_input = gr.Image(
559
+ type="pil",
560
+ label="Upload Food Image"
561
+ )
562
+
563
+ run_button = gr.Button(
564
+ "🚀 Analyze Image",
565
+ variant="primary"
566
+ )
567
+
568
+ with gr.Row():
569
+
570
+ yolo_output_image = gr.Image(
571
+ label="YOLO Detection"
572
+ )
573
+
574
+ vit_output = gr.JSON(
575
+ label="ViT Classification"
576
+ )
577
+
578
+ yolo_output = gr.JSON(
579
+ label="YOLO Results"
580
+ )
581
+
582
+ run_button.click(
583
+ fn=gradio_both,
584
+ inputs=image_input,
585
+ outputs=[
586
+ yolo_output_image,
587
+ vit_output,
588
+ yolo_output,
589
+ ],
590
+ )
591
+
592
+ # --------------------------------------------------------
593
+ # VIT TAB
594
+ # --------------------------------------------------------
595
+
596
+ with gr.Tab("🧠 ViT Classification"):
597
+
598
+ vit_image = gr.Image(
599
+ type="pil",
600
+ label="Upload Food Image"
601
+ )
602
+
603
+ vit_button = gr.Button(
604
+ "Classify Food",
605
+ variant="primary"
606
+ )
607
+
608
+ vit_result = gr.JSON(
609
+ label="Classification Result"
610
+ )
611
+
612
+ vit_button.click(
613
+ fn=gradio_vit,
614
+ inputs=vit_image,
615
+ outputs=vit_result,
616
+ )
617
+
618
+ # --------------------------------------------------------
619
+ # YOLO TAB
620
+ # --------------------------------------------------------
621
+
622
+ with gr.Tab("🔎 YOLO Detection"):
623
+
624
+ yolo_image = gr.Image(
625
+ type="pil",
626
+ label="Upload Food / Vegetable Image"
627
+ )
628
+
629
+ yolo_button = gr.Button(
630
+ "Detect Objects",
631
+ variant="primary"
632
+ )
633
+
634
+ with gr.Row():
635
+
636
+ yolo_result_image = gr.Image(
637
+ label="Detected Objects"
638
+ )
639
+
640
+ yolo_result_json = gr.JSON(
641
+ label="Detection Results"
642
+ )
643
+
644
+ yolo_button.click(
645
+ fn=gradio_yolo,
646
+ inputs=yolo_image,
647
+ outputs=[
648
+ yolo_result_image,
649
+ yolo_result_json,
650
+ ],
651
+ )
652
+
653
+ gr.Markdown(
654
+ f"""
655
+ ---
656
+ **Device:** `{DEVICE}`
657
+
658
+ **ViT:** `{VIT_MODEL_NAME}`
659
+
660
+ **YOLO:** `{YOLO_MODEL_PATH}`
661
+ """
662
+ )
663
+
664
+
665
+ # ============================================================
666
+ # START
667
+ # ============================================================
668
+
669
+ if __name__ == "__main__":
670
+
671
+ import uvicorn
672
+
673
+ # Mount Gradio onto FastAPI
674
+ app = gr.mount_gradio_app(
675
+ app,
676
+ demo,
677
+ path="/gradio"
678
+ )
679
+
680
+ uvicorn.run(
681
+ app,
682
+ host="0.0.0.0",
683
+ port=7860
684
  )
685
 
 
686
 
 
 
 
 
 
 
 
 
 
 
687
 
688
 
689
+
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+
700
+
701
+
702
+
703
+
704
+
705
+ # import io
706
+ # import base64
707
+ # import torch
708
+ # from PIL import Image
709
+ # from fastapi import FastAPI, File, UploadFile, HTTPException
710
+ # from fastapi.middleware.cors import CORSMiddleware
711
+ # from pydantic import BaseModel
712
+ # from ultralytics import YOLO
713
+ # from transformers import AutoImageProcessor, AutoModelForImageClassification
714
+
715
+ # # --- App Config ---
716
+ # app = FastAPI(
717
+ # title="Food & Vegetable AI API",
718
+ # description="Separate APIs for ViT Classification and YOLO Detection",
719
+ # version="2.1.0"
720
+ # )
721
+
722
+ # app.add_middleware(
723
+ # CORSMiddleware,
724
+ # allow_origins=["*"],
725
+ # allow_credentials=True,
726
+ # allow_methods=["*"],
727
+ # allow_headers=["*"],
728
+ # )
729
+
730
+ # class Base64ImageRequest(BaseModel):
731
+ # image: str
732
+
733
+ # # --- Model Loading ---
734
+ # print("Loading models...")
735
+
736
+ # try:
737
+ # vit_model = AutoModelForImageClassification.from_pretrained(
738
+ # "eslamxm/vit-base-food101"
739
+ # )
740
+ # vit_processor = AutoImageProcessor.from_pretrained(
741
+ # "eslamxm/vit-base-food101"
742
+ # )
743
+
744
+ # yolo_model = YOLO("yolo_fruits_and_vegetables_v3.pt")
745
+
746
+ # print("✓ Models loaded successfully")
747
+ # except Exception as e:
748
+ # print(f"✗ Model loading failed: {e}")
749
+ # vit_model = None
750
+ # yolo_model = None
751
+
752
+
753
+ # # --- Utility ---
754
+ # def load_image_from_bytes(image_bytes: bytes) -> Image.Image:
755
+ # return Image.open(io.BytesIO(image_bytes)).convert("RGB")
756
+
757
+
758
+ # # --- YOLO Detection ---
759
+ # def run_yolo(image: Image.Image):
760
+ # if not yolo_model:
761
+ # raise HTTPException(status_code=500, detail="YOLO model not loaded")
762
+
763
+ # results = yolo_model(image)
764
+
765
+ # detections = []
766
+ # summary = {}
767
+
768
+ # for r in results:
769
+ # for i in range(len(r.boxes)):
770
+ # label = yolo_model.names[int(r.boxes.cls[i])]
771
+ # detections.append({
772
+ # "label": label,
773
+ # "confidence": float(r.boxes.conf[i]),
774
+ # "bbox": r.boxes.xyxy[i].tolist()
775
+ # })
776
+ # summary[label] = summary.get(label, 0) + 1
777
+
778
+ # return {
779
+ # "detections": detections,
780
+ # "summary": summary
781
+ # }
782
+
783
+
784
+ # # --- ViT Classification ---
785
+ # def run_vit(image: Image.Image):
786
+ # if not vit_model:
787
+ # raise HTTPException(status_code=500, detail="ViT model not loaded")
788
+
789
+ # inputs = vit_processor(images=image, return_tensors="pt")
790
+
791
+ # with torch.no_grad():
792
+ # outputs = vit_model(**inputs)
793
+
794
+ # probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
795
+ # pred_id = probs.argmax().item()
796
+
797
+ # return {
798
+ # "label": vit_model.config.id2label[pred_id],
799
+ # "confidence": round(probs[0][pred_id].item(), 4)
800
+ # }
801
+
802
+
803
+ # # --- Routes ---
804
+ # @app.get("/")
805
+ # async def root():
806
+ # return {
807
+ # "message": "API running",
808
+ # "endpoints": ["/predict-vit", "/predict-yolo"]
809
+ # }
810
+
811
+
812
+ # # ---------- YOLO Endpoint ----------
813
+ # @app.post("/predict-yolo")
814
+ # async def predict_yolo(file: UploadFile = File(...)):
815
+ # try:
816
+ # image = load_image_from_bytes(await file.read())
817
+ # return {"detection": run_yolo(image)}
818
+ # except Exception as e:
819
+ # raise HTTPException(status_code=500, detail=str(e))
820
+
821
+
822
+ # # ---------- ViT Endpoint ----------
823
+ # @app.post("/predict-vit")
824
+ # async def predict_vit(file: UploadFile = File(...)):
825
+ # try:
826
+ # image = load_image_from_bytes(await file.read())
827
+ # return {"classification": run_vit(image)}
828
+ # except Exception as e:
829
+ # raise HTTPException(status_code=500, detail=str(e))
830
+
831
+
832
+ # # ---------- Base64 Support (optional) ----------
833
+ # @app.post("/predict-vit-base64")
834
+ # async def predict_vit_base64(request: Base64ImageRequest):
835
+ # try:
836
+ # _, encoded = request.image.split(",", 1) if "," in request.image else (None, request.image)
837
+ # image_bytes = base64.b64decode(encoded)
838
+ # image = load_image_from_bytes(image_bytes)
839
+ # return {"classification": run_vit(image)}
840
+ # except Exception as e:
841
+ # raise HTTPException(status_code=500, detail=f"ViT base64 error: {str(e)}")
842
+
843
+
844
+ # @app.post("/predict-yolo-base64")
845
+ # async def predict_yolo_base64(request: Base64ImageRequest):
846
+ # try:
847
+ # _, encoded = request.image.split(",", 1) if "," in request.image else (None, request.image)
848
+ # image_bytes = base64.b64decode(encoded)
849
+ # image = load_image_from_bytes(image_bytes)
850
+ # return {"detection": run_yolo(image)}
851
+ # except Exception as e:
852
+ # raise HTTPException(status_code=500, detail=f"YOLO base64 error: {str(e)}")
853
+
854
+
855
+ # # --- Run ---
856
+ # if __name__ == "__main__":
857
+ # import uvicorn
858
+ # uvicorn.run(app, host="0.0.0.0", port=7860)