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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"""
Postman Testing Guide for GradCAM API
Complete instructions for testing all GradCAM endpoints
"""

# ==============================================================================
# POSTMAN SETUP AND TESTING GUIDE FOR GRADCAM API
# ==============================================================================

POSTMAN_COLLECTION_JSON = '''
{
  "info": {
    "name": "GradCAM Deepfake Detection API",
    "description": "Test collection for GradCAM visualization endpoints",
    "version": "1.0.0"
  },
  "variable": [
    {
      "key": "base_url",
      "value": "http://localhost:5000",
      "type": "string"
    }
  ],
  "item": [
    {
      "name": "1. Test Service Status",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{base_url}}/api/gradcam/test",
          "host": ["{{base_url}}"],
          "path": ["api", "gradcam", "test"]
        },
        "description": "Test if GradCAM service is running and model is loaded"
      }
    },
    {
      "name": "2. Get Model Information",
      "request": {
        "method": "GET", 
        "header": [],
        "url": {
          "raw": "{{base_url}}/api/gradcam/model-info",
          "host": ["{{base_url}}"],
          "path": ["api", "gradcam", "model-info"]
        },
        "description": "Get detailed information about GradCAM capabilities and available layers"
      }
    },
    {
      "name": "3. Single Image Analysis",
      "request": {
        "method": "POST",
        "header": [],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "image",
              "type": "file",
              "src": ""
            },
            {
              "key": "return_image",
              "value": "true",
              "type": "text"
            },
            {
              "key": "layer_name",
              "value": "",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{base_url}}/api/gradcam/analyze",
          "host": ["{{base_url}}"],
          "path": ["api", "gradcam", "analyze"]
        },
        "description": "Analyze single image with GradCAM visualization"
      }
    },
    {
      "name": "4. Batch Image Analysis",
      "request": {
        "method": "POST",
        "header": [],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "images",
              "type": "file",
              "src": ""
            },
            {
              "key": "images",
              "type": "file", 
              "src": ""
            },
            {
              "key": "max_images",
              "value": "5",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{base_url}}/api/gradcam/analyze-batch",
          "host": ["{{base_url}}"],
          "path": ["api", "gradcam", "analyze-batch"]
        },
        "description": "Batch analysis of multiple images"
      }
    },
    {
      "name": "5. Backend Status Check",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{base_url}}/",
          "host": ["{{base_url}}"],
          "path": [""]
        },
        "description": "Check overall backend status and available endpoints"
      }
    }
  ]
}
'''

# ==============================================================================
# STEP-BY-STEP TESTING INSTRUCTIONS
# ==============================================================================

TESTING_INSTRUCTIONS = """
πŸ“‹ POSTMAN TESTING GUIDE - GradCAM API
====================================

πŸš€ SETUP
--------
1. Start your Flask backend:
   cd "d:\\SGP\\DeepFake\\Deepfake-Detection\\Webapp\\Model_Load"
   python app.py

2. Backend should be running on: http://localhost:5000

3. Import Postman Collection:
   - Copy the JSON collection above
   - In Postman: Import > Raw Text > Paste JSON > Import

πŸ“ TEST SEQUENCE
---------------

TEST 1: Service Status Check
---------------------------
β€’ Method: GET
β€’ URL: http://localhost:5000/api/gradcam/test
β€’ Headers: None required
β€’ Body: None

Expected Response:
{
  "status": "GradCAM Analysis Service Active",
  "model_loaded": true,
  "face_detector_ready": true,
  "memory_optimization": "Enabled",
  "capabilities": [...],
  "test_successful": true
}

βœ… Success: Status 200, test_successful = true
❌ Fail: Status 500, check model loading


TEST 2: Model Information
------------------------
β€’ Method: GET  
β€’ URL: http://localhost:5000/api/gradcam/model-info
β€’ Headers: None required
β€’ Body: None

Expected Response:
{
  "model_name": "XceptionNet with GradCAM",
  "description": "Visual explanation of deepfake detection...",
  "capabilities": {...},
  "available_layers": [...],
  "recommended_layers": [...]
}

βœ… Success: Status 200, layers list populated
❌ Fail: Status 500, model not loaded


TEST 3: Single Image Analysis  
-----------------------------
β€’ Method: POST
β€’ URL: http://localhost:5000/api/gradcam/analyze
β€’ Headers: None required (auto-detected)

Body (form-data):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Key             β”‚ Type     β”‚ Value                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ image           β”‚ file     β”‚ [Select image with face]        β”‚
β”‚ return_image    β”‚ text     β”‚ true                            β”‚
β”‚ layer_name      β”‚ text     β”‚ (leave empty for auto-detect)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Expected Response:
{
  "success": true,
  "prediction": {
    "classification": "DEEPFAKE" or "AUTHENTIC",
    "is_deepfake": true/false,
    "confidence": 0.85,
    "raw_score": 0.7234
  },
  "face_detection": {
    "face_found": true,
    "face_confidence": 0.99,
    "face_coordinates": {...}
  },
  "gradcam_analysis": {
    "layer_used": "block14_sepconv1_act",
    "heatmap_statistics": {...}
  },
  "visualization": {
    "format": "PNG (base64 encoded)",
    "data": "iVBORw0KGgoAAAANSUhEUgAA...",
    "description": "GradCAM visualization..."
  },
  "interpretation": {...},
  "metadata": {...}
}

βœ… Success: Status 200, success = true, visualization data present
❌ Fail: Status 400, no face detected or invalid image


TEST 4: Batch Analysis
---------------------
β€’ Method: POST
β€’ URL: http://localhost:5000/api/gradcam/analyze-batch
β€’ Headers: None required

Body (form-data):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Key             β”‚ Type     β”‚ Value                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ images          β”‚ file     β”‚ [Select first image]            β”‚
β”‚ images          β”‚ file     β”‚ [Select second image]           β”‚
β”‚ images          β”‚ file     β”‚ [Select more images...]         β”‚
β”‚ max_images      β”‚ text     β”‚ 5                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Note: Add multiple 'images' keys for multiple files

Expected Response:
{
  "success": true,
  "batch_summary": {
    "total_files": 3,
    "processed_successfully": 2,
    "failed_files": 1,
    "deepfake_detected": 1,
    "authentic_detected": 1,
    "average_confidence": 0.78
  },
  "results": [...],
  "failed_files": [...],
  "metadata": {...}
}

βœ… Success: Status 200, batch_summary populated
❌ Fail: Status 400, no valid images provided


TEST 5: Backend Compatibility
-----------------------------
β€’ Method: GET
β€’ URL: http://localhost:5000/
β€’ Headers: None required
β€’ Body: None

Expected Response:
{
  "status": "ok",
  "message": "Deepfake Detection API Backend is running",
  "available_endpoints": {
    "gradcam_analysis": "/api/gradcam/analyze",
    "gradcam_batch": "/api/gradcam/analyze-batch",
    "gradcam_info": "/api/gradcam/model-info",
    "gradcam_test": "/api/gradcam/test"
  },
  "new_features": {
    "gradcam_visualization": "Visual explanation of AI decisions"
  }
}

βœ… Success: Status 200, gradcam endpoints listed
❌ Fail: Backend not running or integration failed
"""

# ==============================================================================
# COMMON ERROR CODES AND SOLUTIONS
# ==============================================================================

ERROR_TROUBLESHOOTING = """
πŸ”§ TROUBLESHOOTING GUIDE
========================

ERROR 1: Connection Refused
---------------------------
Symptoms: Can't connect to localhost:5000
Solutions:
β€’ Ensure Flask app is running: python app.py
β€’ Check if port 5000 is available
β€’ Verify no firewall blocking
β€’ Try: curl http://localhost:5000/

ERROR 2: Model Not Loaded (Status 500)
--------------------------------------  
Symptoms: test_successful = false, model_loaded = false
Solutions:
β€’ Check if xceptionnet.keras exists in models/ folder
β€’ Verify TensorFlow installation: pip install tensorflow
β€’ Check file permissions on model file
β€’ Review server logs for detailed error

ERROR 3: No Face Detected (Status 400)
--------------------------------------
Symptoms: "No face detected" error in analysis
Solutions:
β€’ Use images with clear, visible faces
β€’ Ensure face is well-lit and facing camera
β€’ Try different images with larger faces
β€’ Check MTCNN installation: pip install mtcnn

ERROR 4: Memory Error (Status 500)
----------------------------------
Symptoms: Out of memory during processing
Solutions:
β€’ Reduce image size before upload (< 2MB recommended)
β€’ Reduce batch size (max_images parameter)
β€’ Restart Flask server to clear memory
β€’ Check available system RAM

ERROR 5: Invalid Image Format
-----------------------------
Symptoms: "Invalid image file" error
Solutions:
β€’ Use supported formats: JPG, JPEG, PNG
β€’ Ensure file is not corrupted
β€’ Check file size (< 10MB recommended)
β€’ Try re-saving image in different format

ERROR 6: Import Errors (Status 500)
-----------------------------------
Symptoms: Module import failures in logs
Solutions:
β€’ Install missing dependencies: pip install -r requirements.txt
β€’ Check virtual environment activation
β€’ Verify Python version compatibility (3.7+)
β€’ Update packages: pip install --upgrade tensorflow opencv-python

PERFORMANCE TIPS:
β€’ Use images 800x600 or smaller for faster processing
β€’ JPEG format is typically faster than PNG
β€’ Batch processing is more efficient for multiple images
β€’ Clear browser cache if using web interface
"""

# ==============================================================================
# SAMPLE CURL COMMANDS (Alternative to Postman)
# ==============================================================================

CURL_EXAMPLES = '''
🌐 CURL COMMAND EXAMPLES
========================

# Test 1: Service Status
curl -X GET http://localhost:5000/api/gradcam/test

# Test 2: Model Info  
curl -X GET http://localhost:5000/api/gradcam/model-info

# Test 3: Single Image Analysis
curl -X POST http://localhost:5000/api/gradcam/analyze \\
  -F "image=@/path/to/your/image.jpg" \\
  -F "return_image=true" \\
  -F "layer_name="

# Test 4: Batch Analysis
curl -X POST http://localhost:5000/api/gradcam/analyze-batch \\
  -F "images=@/path/to/image1.jpg" \\
  -F "images=@/path/to/image2.jpg" \\
  -F "max_images=5"

# Test 5: Backend Status
curl -X GET http://localhost:5000/
'''

if __name__ == "__main__":
    print("πŸ“– POSTMAN TESTING GUIDE FOR GRADCAM API")
    print("="*50)
    
    # Save collection to file
    with open("GradCAM_API_Collection.json", "w") as f:
        f.write(POSTMAN_COLLECTION_JSON)
    
    print("βœ… Postman collection saved to: GradCAM_API_Collection.json")
    print("\nπŸ“‹ TESTING INSTRUCTIONS:")
    print(TESTING_INSTRUCTIONS)
    print("\nπŸ”§ TROUBLESHOOTING:")
    print(ERROR_TROUBLESHOOTING)
    print("\n🌐 CURL ALTERNATIVES:")
    print(CURL_EXAMPLES)