Patrick Kastner commited on
Commit
4be65d1
·
0 Parent(s):

Initial commit

Browse files

Init

Update test.ipynb

Update README.md

.

Files changed (6) hide show
  1. .gitattributes +2 -0
  2. .gitignore +156 -0
  3. README.md +4 -0
  4. api.py +51 -0
  5. requirements.txt +8 -0
  6. test.ipynb +106 -0
.gitattributes ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
.gitignore ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
105
+ __pypackages__/
106
+
107
+ # Celery stuff
108
+ celerybeat-schedule
109
+ celerybeat.pid
110
+
111
+ # SageMath parsed files
112
+ *.sage.py
113
+
114
+ # Environments
115
+ .env
116
+ .venv
117
+ env/
118
+ venv/
119
+ ENV/
120
+ env.bak/
121
+ venv.bak/
122
+
123
+ # Spyder project settings
124
+ .spyderproject
125
+ .spyproject
126
+
127
+ # Rope project settings
128
+ .ropeproject
129
+
130
+ # mkdocs documentation
131
+ /site
132
+
133
+ # mypy
134
+ .mypy_cache/
135
+ .dmypy.json
136
+ dmypy.json
137
+
138
+ # Pyre type checker
139
+ .pyre/
140
+
141
+ # pytype static type analyzer
142
+ .pytype/
143
+
144
+ # Cython debug symbols
145
+ cython_debug/
146
+
147
+ # PyCharm
148
+ # JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
149
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
150
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
151
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
152
+ #.idea/
153
+
154
+ .space
155
+ .onnx
156
+ model.onnx
README.md ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # GAN-API
2
+
3
+ http://gan-230522.eddy3d.com/test
4
+
api.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, responses
2
+ from PIL import Image
3
+ import io
4
+ import numpy as np
5
+ import onnxruntime as rt
6
+
7
+ app = FastAPI()
8
+
9
+ sess = rt.InferenceSession("model.onnx")
10
+
11
+ @app.get("/test")
12
+ async def test():
13
+ return {"message": "API is working"}
14
+
15
+ @app.api_route("/dummy", methods=["POST"])
16
+ async def dummy_function(file: UploadFile = File(...)):
17
+ print("Dummy function called")
18
+ # Perform some dummy processing or return a dummy response
19
+ return {"message": "Dummy function called"}
20
+
21
+ #@app.api_route("/process_image", methods=["POST"])
22
+ @app.post("/process_image")
23
+ async def process_image(file: UploadFile = File(...)):
24
+ input_data = await file.read()
25
+ input_image = Image.open(io.BytesIO(input_data)).convert("RGB")
26
+
27
+ # Resize and convert to numpy array
28
+ input_array = np.array(input_image.resize((512, 512)))
29
+
30
+ # Normalize to [-1, 1]
31
+ input_array = (input_array.astype(np.float32) / 127.5) - 1
32
+ input_array = np.expand_dims(input_array, axis=0)
33
+
34
+ # Reorder dimensions to match model's expectations
35
+ input_array = input_array.transpose((0, 3, 1, 2)) # Move the color dimension to the correct position
36
+
37
+ inputs = {sess.get_inputs()[0].name: input_array}
38
+ outputs = sess.run(None, inputs)
39
+
40
+ # Denormalize from [-1, 1] to [0, 255]
41
+ output_array = (outputs[0][0].transpose((1, 2, 0)) + 1) * 127.5
42
+ output_array = np.clip(output_array, 0, 255) # Clip values outside of valid range
43
+
44
+ # Convert back to image
45
+ output_image = Image.fromarray(output_array.astype(np.uint8), "RGB")
46
+
47
+ byte_arr = io.BytesIO()
48
+ output_image.save(byte_arr, format='PNG')
49
+ byte_arr.seek(0) # Move cursor to the beginning of the file
50
+
51
+ return responses.StreamingResponse(byte_arr, media_type="image/png")
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ python-multipart
4
+ uvicorn
5
+ onnxruntime
6
+ pillow
7
+ numpy
8
+ requests
test.ipynb ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 13,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import requests\n",
10
+ "from PIL import Image\n",
11
+ "import io\n"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 22,
17
+ "metadata": {},
18
+ "outputs": [
19
+ {
20
+ "name": "stdout",
21
+ "output_type": "stream",
22
+ "text": [
23
+ "b'{\"message\":\"API is working\"}'\n"
24
+ ]
25
+ }
26
+ ],
27
+ "source": [
28
+ "response = requests.get(\"http://gan-230522.eddy3d.com/test\")\n",
29
+ "\n",
30
+ "print(response.content)\n"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 35,
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "# Open your image file in binary mode\n",
40
+ "with open(\"test_input.png\", \"rb\") as f:\n",
41
+ " img_binary = f.read()\n",
42
+ "\n",
43
+ "\n",
44
+ "# Create a file-like object from the image binary data\n",
45
+ "img_file = io.BytesIO(img_binary)\n",
46
+ "\n",
47
+ "# Open the image using PIL\n",
48
+ "img_in = Image.open(img_file)\n",
49
+ "\n",
50
+ "# Display the image\n",
51
+ "img_in.show()\n",
52
+ "\n",
53
+ "\n",
54
+ "# Send the POST request\n",
55
+ "# response = requests.post('http://127.0.0.1:8000/process_image', files={'file': img_binary})\n",
56
+ "response = requests.post(\n",
57
+ " \"https://gan-230522.eddy3d.com/process_image\", files={\"file\": img_binary}\n",
58
+ ")\n",
59
+ "\n",
60
+ "\n",
61
+ "# The response is an image file, so we need to convert it back to an image\n",
62
+ "image = Image.open(io.BytesIO(response.content))\n",
63
+ "\n",
64
+ "# Now you can display the image or save it to a file\n",
65
+ "image.show()\n"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 18,
71
+ "metadata": {},
72
+ "outputs": [
73
+ {
74
+ "name": "stdout",
75
+ "output_type": "stream",
76
+ "text": [
77
+ "b'{\"message\":\"API is working\"}'\n"
78
+ ]
79
+ }
80
+ ],
81
+ "source": []
82
+ }
83
+ ],
84
+ "metadata": {
85
+ "kernelspec": {
86
+ "display_name": "ganapi",
87
+ "language": "python",
88
+ "name": "python3"
89
+ },
90
+ "language_info": {
91
+ "codemirror_mode": {
92
+ "name": "ipython",
93
+ "version": 3
94
+ },
95
+ "file_extension": ".py",
96
+ "mimetype": "text/x-python",
97
+ "name": "python",
98
+ "nbconvert_exporter": "python",
99
+ "pygments_lexer": "ipython3",
100
+ "version": "3.9.16"
101
+ },
102
+ "orig_nbformat": 4
103
+ },
104
+ "nbformat": 4,
105
+ "nbformat_minor": 2
106
+ }