Charles Kabui commited on
Commit
ff111cc
·
1 Parent(s): d33dce7

demo-examples

Browse files
Files changed (2) hide show
  1. analysis.ipynb +25 -159
  2. app.py +2 -1
analysis.ipynb CHANGED
@@ -11,7 +11,7 @@
11
  },
12
  {
13
  "cell_type": "code",
14
- "execution_count": 1,
15
  "metadata": {},
16
  "outputs": [],
17
  "source": [
@@ -245,7 +245,7 @@
245
  " 'vectors'), \n",
246
  " indent=4),\n",
247
  " label_text_size=40,\n",
248
- " label_rectangle_color='white',\n",
249
  " ),\n",
250
  " ]\n",
251
  " for i\n",
@@ -278,6 +278,24 @@
278
  "print_matches(top_matches, 2, start=0)"
279
  ]
280
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  {
282
  "cell_type": "code",
283
  "execution_count": null,
@@ -301,170 +319,18 @@
301
  },
302
  {
303
  "cell_type": "code",
304
- "execution_count": 22,
305
  "metadata": {},
306
- "outputs": [
307
- {
308
- "name": "stdout",
309
- "output_type": "stream",
310
- "text": [
311
- "Running on local URL: http://127.0.0.1:7862\n",
312
- "\n",
313
- "To create a public link, set `share=True` in `launch()`.\n"
314
- ]
315
- },
316
- {
317
- "data": {
318
- "text/html": [
319
- "<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
320
- ],
321
- "text/plain": [
322
- "<IPython.core.display.HTML object>"
323
- ]
324
- },
325
- "metadata": {},
326
- "output_type": "display_data"
327
- },
328
- {
329
- "name": "stderr",
330
- "output_type": "stream",
331
- "text": [
332
- "/Users/charleskabue/miniconda3/envs/dss-env/lib/python3.10/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /Users/runner/work/_temp/anaconda/conda-bld/pytorch_1695391816234/work/aten/src/ATen/native/TensorShape.cpp:3527.)\n",
333
- " return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]\n"
334
- ]
335
- },
336
- {
337
- "name": "stdout",
338
- "output_type": "stream",
339
- "text": [
340
- "Keyboard interruption in main thread... closing server.\n"
341
- ]
342
- },
343
- {
344
- "data": {
345
- "text/plain": []
346
- },
347
- "execution_count": 22,
348
- "metadata": {},
349
- "output_type": "execute_result"
350
- }
351
- ],
352
  "source": [
353
  "from main import app\n",
 
354
  "\n",
355
  "model_path = '../detectron2-layout-parser/model_final.pth'\n",
356
  "config_path = '../detectron2-layout-parser/config.yaml'\n",
357
- "examples = [\n",
358
- " '/Users/charleskabue/document-similarity-search/detectron2-layout-parser/example.1.jpg',\n",
359
- " '/Users/charleskabue/document-similarity-search/detectron2-layout-parser/example.2.jpg',\n",
360
- " '/Users/charleskabue/document-similarity-search/detectron2-layout-parser/example.3.jpg',\n",
361
- " ] * 5\n",
362
- "app(model_path=model_path, config_path=config_path, examples=examples, debug=True)"
363
- ]
364
- },
365
- {
366
- "cell_type": "code",
367
- "execution_count": 16,
368
- "metadata": {},
369
- "outputs": [
370
- {
371
- "name": "stdout",
372
- "output_type": "stream",
373
- "text": [
374
- "Running on local URL: http://127.0.0.1:7861\n",
375
- "\n",
376
- "To create a public link, set `share=True` in `launch()`.\n"
377
- ]
378
- },
379
- {
380
- "data": {
381
- "text/html": [
382
- "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
383
- ],
384
- "text/plain": [
385
- "<IPython.core.display.HTML object>"
386
- ]
387
- },
388
- "metadata": {},
389
- "output_type": "display_data"
390
- },
391
- {
392
- "data": {
393
- "text/plain": []
394
- },
395
- "execution_count": 16,
396
- "metadata": {},
397
- "output_type": "execute_result"
398
- }
399
- ],
400
- "source": [
401
- "\n",
402
- "import gradio as gr\n",
403
- "\n",
404
- "def classify_image(image):\n",
405
- " # Replace with your image classification logic\n",
406
- " # (e.g., using a pre-trained model or custom implementation)\n",
407
- " classification_results = {\"cat\": 0.8, \"dog\": 0.2}\n",
408
- " return classification_results\n",
409
- "\n",
410
- "image_input = gr.components.Image(type=\"pil\") # Allow image upload\n",
411
- "label = gr.components.Label(num_top_classes=3) # Show top 3 predictions\n",
412
  "\n",
413
- "interface = gr.Interface(classify_image, inputs=image_input, outputs=label)\n",
414
- "interface.launch()"
415
- ]
416
- },
417
- {
418
- "cell_type": "code",
419
- "execution_count": 15,
420
- "metadata": {},
421
- "outputs": [
422
- {
423
- "name": "stdout",
424
- "output_type": "stream",
425
- "text": [
426
- "Running on local URL: http://127.0.0.1:7861\n",
427
- "\n",
428
- "To create a public link, set `share=True` in `launch()`.\n"
429
- ]
430
- },
431
- {
432
- "data": {
433
- "text/html": [
434
- "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
435
- ],
436
- "text/plain": [
437
- "<IPython.core.display.HTML object>"
438
- ]
439
- },
440
- "metadata": {},
441
- "output_type": "display_data"
442
- },
443
- {
444
- "name": "stdout",
445
- "output_type": "stream",
446
- "text": [
447
- "Keyboard interruption in main thread... closing server.\n"
448
- ]
449
- },
450
- {
451
- "data": {
452
- "text/plain": []
453
- },
454
- "execution_count": 15,
455
- "metadata": {},
456
- "output_type": "execute_result"
457
- }
458
- ],
459
- "source": [
460
- "with gr.Blocks() as interface:\n",
461
- " document = gr.Image(\n",
462
- " type=\"pil\", label=f\"Document\", interactive=False, show_download_button=True)\n",
463
- " gr.Examples(\n",
464
- " examples=examples,\n",
465
- " inputs=document,\n",
466
- " label='Select any of these test document images')\n",
467
- "interface.launch(debug=True)"
468
  ]
469
  }
470
  ],
 
11
  },
12
  {
13
  "cell_type": "code",
14
+ "execution_count": null,
15
  "metadata": {},
16
  "outputs": [],
17
  "source": [
 
245
  " 'vectors'), \n",
246
  " indent=4),\n",
247
  " label_text_size=40,\n",
248
+ " label_fill_color='white',\n",
249
  " ),\n",
250
  " ]\n",
251
  " for i\n",
 
278
  "print_matches(top_matches, 2, start=0)"
279
  ]
280
  },
281
+ {
282
+ "cell_type": "code",
283
+ "execution_count": null,
284
+ "metadata": {},
285
+ "outputs": [],
286
+ "source": [
287
+ "# len([i for i in top_matches if i['cosine_similarity_score'] < 1 and i['cosine_similarity_score'] > 0.99])\n",
288
+ "# len([i for i in top_matches if i['cosine_similarity_score'] >= 1])\n",
289
+ "# import random\n",
290
+ "# import shutil\n",
291
+ "# \n",
292
+ "# for matches in (top_matches[0:2] + random.sample([i for i in top_matches], 8)):\n",
293
+ "# document_image_1 = matches['document_image_1']\n",
294
+ "# document_image_2 = matches['document_image_2']\n",
295
+ "# shutil.copyfile(f'./data/local-data/raw/RVL-CDIP-invoice/{document_image_1}', f'./demo-examples/{document_image_1}')\n",
296
+ "# shutil.copyfile(f'./data/local-data/raw/RVL-CDIP-invoice/{document_image_2}', f'./demo-examples/{document_image_2}')"
297
+ ]
298
+ },
299
  {
300
  "cell_type": "code",
301
  "execution_count": null,
 
319
  },
320
  {
321
  "cell_type": "code",
322
+ "execution_count": null,
323
  "metadata": {},
324
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
  "source": [
326
  "from main import app\n",
327
+ "import os\n",
328
  "\n",
329
  "model_path = '../detectron2-layout-parser/model_final.pth'\n",
330
  "config_path = '../detectron2-layout-parser/config.yaml'\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  "\n",
332
+ "examples = [f'./demo-examples/{filename}' for filename in os.listdir('./demo-examples/')]\n",
333
+ "app(model_path=model_path, config_path=config_path, examples=examples, debug=True)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  ]
335
  }
336
  ],
app.py CHANGED
@@ -14,4 +14,5 @@ from main import app
14
 
15
  model_path = './model/trained_model/model_final.pth'
16
  config_path = './model/trained_model/config.yaml'
17
- app(model_path=model_path, config_path=config_path, debug=True)
 
 
14
 
15
  model_path = './model/trained_model/model_final.pth'
16
  config_path = './model/trained_model/config.yaml'
17
+ examples = [f'./demo-examples/{filename}' for filename in os.listdir('./demo-examples/')]
18
+ app(model_path=model_path, config_path=config_path, examples=examples, debug=True)