Meng Chen
commited on
Commit
·
ef35004
1
Parent(s):
1c6d641
update handler
Browse files- .gitattributes +1 -0
- __pycache__/handler.cpython-311.pyc +0 -0
- elevator.jpg +3 -0
- handler.py +0 -1
- test.ipynb +104 -0
.gitattributes
CHANGED
|
@@ -31,3 +31,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 31 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 32 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 33 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 31 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 32 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 33 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
elevator.jpg filter=lfs diff=lfs merge=lfs -text
|
__pycache__/handler.cpython-311.pyc
ADDED
|
Binary file (5.17 kB). View file
|
|
|
elevator.jpg
ADDED
|
Git LFS Details
|
handler.py
CHANGED
|
@@ -23,7 +23,6 @@ class EndpointHandler():
|
|
| 23 |
Return:
|
| 24 |
A :obj:`list` | `dict`: will be serialized and returned
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
if "image" not in data or "text" not in data:
|
| 28 |
return [{"error": "Missing 'image' or 'text' key in input data"}]
|
| 29 |
|
|
|
|
| 23 |
Return:
|
| 24 |
A :obj:`list` | `dict`: will be serialized and returned
|
| 25 |
"""
|
|
|
|
| 26 |
if "image" not in data or "text" not in data:
|
| 27 |
return [{"error": "Missing 'image' or 'text' key in input data"}]
|
| 28 |
|
test.ipynb
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 3,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"name": "stderr",
|
| 10 |
+
"output_type": "stream",
|
| 11 |
+
"text": [
|
| 12 |
+
"Device set to use cpu\n"
|
| 13 |
+
]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"name": "stdout",
|
| 17 |
+
"output_type": "stream",
|
| 18 |
+
"text": [
|
| 19 |
+
"[\n",
|
| 20 |
+
" {\n",
|
| 21 |
+
" \"error\": \"Torch not compiled with CUDA enabled\"\n",
|
| 22 |
+
" }\n",
|
| 23 |
+
"]\n"
|
| 24 |
+
]
|
| 25 |
+
}
|
| 26 |
+
],
|
| 27 |
+
"source": [
|
| 28 |
+
"import base64\n",
|
| 29 |
+
"import json\n",
|
| 30 |
+
"import io\n",
|
| 31 |
+
"from PIL import Image\n",
|
| 32 |
+
"import numpy as np\n",
|
| 33 |
+
"import matplotlib.pyplot as plt\n",
|
| 34 |
+
"from handler import EndpointHandler \n",
|
| 35 |
+
"\n",
|
| 36 |
+
"def encode_image(image_path):\n",
|
| 37 |
+
" \"\"\"Encodes an image to base64.\"\"\"\n",
|
| 38 |
+
" with open(image_path, \"rb\") as image_file:\n",
|
| 39 |
+
" return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"def visualize_mask(mask):\n",
|
| 42 |
+
" \"\"\"Visualizes the segmentation mask using matplotlib.\"\"\"\n",
|
| 43 |
+
" mask = np.array(mask)\n",
|
| 44 |
+
" plt.imshow(mask, cmap=\"jet\", alpha=0.6)\n",
|
| 45 |
+
" plt.colorbar()\n",
|
| 46 |
+
" plt.title(\"Segmentation Mask\")\n",
|
| 47 |
+
" plt.show()\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"if __name__ == \"__main__\":\n",
|
| 50 |
+
" # Initialize handler\n",
|
| 51 |
+
" handler = EndpointHandler()\n",
|
| 52 |
+
"\n",
|
| 53 |
+
" # Provide path to a test image\n",
|
| 54 |
+
" image_path = \"elevator.jpg\" # Replace with a valid image path\n",
|
| 55 |
+
"\n",
|
| 56 |
+
" # Encode image as base64\n",
|
| 57 |
+
" encoded_image = encode_image(image_path)\n",
|
| 58 |
+
"\n",
|
| 59 |
+
" # Define test input\n",
|
| 60 |
+
" test_input = {\n",
|
| 61 |
+
" \"image\": encoded_image,\n",
|
| 62 |
+
" \"text\": \"elevator\"\n",
|
| 63 |
+
" }\n",
|
| 64 |
+
"\n",
|
| 65 |
+
" # Run handler\n",
|
| 66 |
+
" result = handler(test_input)\n",
|
| 67 |
+
"\n",
|
| 68 |
+
" # Print and visualize the result\n",
|
| 69 |
+
" print(json.dumps(result, indent=2))\n",
|
| 70 |
+
"\n",
|
| 71 |
+
" if \"segmentation_mask\" in result[0]:\n",
|
| 72 |
+
" visualize_mask(result[0][\"segmentation_mask\"])"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": null,
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [],
|
| 80 |
+
"source": []
|
| 81 |
+
}
|
| 82 |
+
],
|
| 83 |
+
"metadata": {
|
| 84 |
+
"kernelspec": {
|
| 85 |
+
"display_name": "a11y-agent",
|
| 86 |
+
"language": "python",
|
| 87 |
+
"name": "python3"
|
| 88 |
+
},
|
| 89 |
+
"language_info": {
|
| 90 |
+
"codemirror_mode": {
|
| 91 |
+
"name": "ipython",
|
| 92 |
+
"version": 3
|
| 93 |
+
},
|
| 94 |
+
"file_extension": ".py",
|
| 95 |
+
"mimetype": "text/x-python",
|
| 96 |
+
"name": "python",
|
| 97 |
+
"nbconvert_exporter": "python",
|
| 98 |
+
"pygments_lexer": "ipython3",
|
| 99 |
+
"version": "3.11.10"
|
| 100 |
+
}
|
| 101 |
+
},
|
| 102 |
+
"nbformat": 4,
|
| 103 |
+
"nbformat_minor": 2
|
| 104 |
+
}
|