elgammal commited on
Commit
7ed6fbe
·
1 Parent(s): 74f1205

first commit

Browse files
Files changed (2) hide show
  1. app.py +39 -0
  2. playform.py +111 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from playform import playform,playform_load
4
+
5
+
6
+ @playform
7
+ def sepia(input_img,url_params):
8
+ #print('inside fn',url_params)
9
+ sepia_filter = np.array([
10
+ [0.393, 0.769, 0.189],
11
+ [0.349, 0.686, 0.168],
12
+ [0.272, 0.534, 0.131]
13
+ ])
14
+ sepia_img = input_img.dot(sepia_filter.T)
15
+ sepia_img /= sepia_img.max()
16
+ return sepia_img,url_params
17
+
18
+ with gr.Blocks() as demo:
19
+
20
+ #code block needed to get url parameters
21
+ url_params = gr.JSON({}, visible=False, label="URL Params")
22
+ demo.load(lambda x: x,inputs = [url_params],outputs=[url_params],_js=playform_load)
23
+ #end code block needed to get url parameters
24
+
25
+
26
+ with gr.Row():
27
+ with gr.Column():
28
+ img_input = gr.Image(shape=(200, 200))
29
+ with gr.Column():
30
+ img_output = gr.Image(shape=(200, 200))
31
+
32
+ btn = gr.Button("submit")
33
+ btn.click(fn=sepia, inputs=[img_input, url_params], outputs=[img_output,url_params])
34
+ #btn.click(fn=sepia, inputs=img_input, outputs=img_output)
35
+
36
+
37
+
38
+ if __name__=='__main__':
39
+ demo.launch(debug=True)
playform.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # this is to test actual playform
2
+
3
+ import functools
4
+ import requests
5
+
6
+ import io
7
+ import numpy
8
+ from PIL import Image
9
+
10
+
11
+ api_url_base = "http://127.0.0.1:7000/api/app/projects/"
12
+ api_url_tail = "/generate/"
13
+
14
+ playform_load = """
15
+ function(url_params) {
16
+ console.log(url_params);
17
+ const params = new URLSearchParams(window.location.search);
18
+ url_params = Object.fromEntries(params);
19
+ return [url_params];
20
+ }
21
+ """
22
+
23
+
24
+ def get_session_param(param):
25
+ if isinstance(param,list):
26
+ api_url=api_url_base+str(param[0]["id"])+api_url_tail
27
+ token = param[0]["token"]
28
+ else:
29
+ api_url = ""
30
+ token = ""
31
+
32
+ return api_url, token
33
+
34
+ def prep_image(image):
35
+ # takes a numpy image and prepare it to be sent
36
+ if image.dtype == numpy.uint8:
37
+ img = Image.fromarray(image)
38
+ else:
39
+ img = Image.fromarray((image*255).astype(numpy.uint8))
40
+ return img
41
+
42
+
43
+ def send_image(img,api_url,token):
44
+ # takes a PIL image and send it
45
+
46
+ output=io.BytesIO()
47
+ img.save(output, format='JPEG')
48
+ f=output.getvalue()
49
+
50
+ files = [('image',('image.jpg',f,'image/jpeg'))]
51
+
52
+ # Set the headers for the request
53
+ headers = {'Authorization': f'Token {token}'}
54
+
55
+ payload = {}
56
+
57
+ print(api_url,token)
58
+
59
+ try:
60
+ response = requests.post(api_url,data=payload, headers=headers,files = files)
61
+ print("request sent")
62
+ print(response.text)
63
+ except:
64
+ print("request failed")
65
+
66
+ return
67
+
68
+ def playform(func):
69
+ @functools.wraps(func)
70
+ def wrapper_playform(*args, **kwargs):
71
+
72
+ ret_value = func(*args, **kwargs)
73
+
74
+ #print(f'return type {type(ret_value)}')
75
+
76
+ value = ret_value[0]
77
+
78
+ # print(f'input type: {type(args)}')
79
+ # param= args[-1]
80
+ # print(f'param: , {param}')
81
+
82
+
83
+ api_url, token = get_session_param(args[-1])
84
+ # print(api_url)
85
+ # print(token)
86
+
87
+ # modified code to deal with multiple output
88
+ if isinstance(value,list):
89
+ print(f'number of outputs: {len(value)}')
90
+ for k in value:
91
+ if isinstance(k,numpy.ndarray):
92
+ # print(f'output: ')
93
+ # print(f'output type {type(k)}')
94
+ # print(f'output dim {k.shape}')
95
+ # print(f'data type {k.dtype}')
96
+ # print(f'max value {k.max()} min value {k.min()}' )
97
+
98
+ img = prep_image(k)
99
+ send_image(img,api_url,token)
100
+
101
+ elif isinstance(value,numpy.ndarray ):
102
+ # print('numpy array')
103
+ img = prep_image(value)
104
+ send_image(img,api_url,token)
105
+ else:
106
+ print('unpredicted return type')
107
+
108
+
109
+
110
+ return ret_value
111
+ return wrapper_playform