firatozdemir commited on
Commit
71dda47
1 Parent(s): a326e04

put image and button side by side

Browse files
Files changed (2) hide show
  1. app.py +6 -9
  2. demo.ipynb +164 -0
app.py CHANGED
@@ -51,14 +51,11 @@ description="Generate fake linear array images."
51
 
52
  with gr.Blocks() as demo:
53
  gr.Markdown(description)
54
- # with gr.Row(equal_height=True):
55
- # button_gen = gr.Button("Generate fake linear image")
56
- # with gr.Row(equal_height=True):
57
- # output_im = gr.Image(type="numpy", shape=(256, 256), image_mode="L", label="fake image", interactive=False) #grayscale image
58
- with gr.Column(scale=1):
59
- button_gen = gr.Button("Generate fake linear image")
60
- with gr.Column(scale=2):
61
- output_im = gr.Image(type="numpy", shape=(256, 256), image_mode="L", label="fake image", interactive=False) #grayscale image
62
  button_gen.click(sample_GAN, inputs=None, outputs=output_im)
63
 
64
- demo.launch(share=False, show_tips=True, enable_queue=True)
 
51
 
52
  with gr.Blocks() as demo:
53
  gr.Markdown(description)
54
+ with gr.Row():
55
+ with gr.Column():
56
+ button_gen = gr.Button("Generate fake linear image")
57
+ with gr.Column():
58
+ output_im = gr.Image(type="numpy", shape=(256, 256), image_mode="L", label="fake image", interactive=False) #grayscale image
 
 
 
59
  button_gen.click(sample_GAN, inputs=None, outputs=output_im)
60
 
61
+ demo.launch(share=True, show_tips=True, enable_queue=True)
demo.ipynb ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "\n",
10
+ "import sys, os\n",
11
+ "import gradio as gr\n",
12
+ "import numpy as np\n",
13
+ "sys.path.append('stylegan3')\n",
14
+ "import utils\n",
15
+ "\n",
16
+ "def to_uint8(im, ndim=2):\n",
17
+ " im -= np.min(im)\n",
18
+ " im /= np.max(im)\n",
19
+ " im *= 255.\n",
20
+ " im = np.asarray(im, dtype=np.uint8)\n",
21
+ " if ndim == 3: \n",
22
+ " if im.ndim == 2:\n",
23
+ " im = np.expand_dims(im, axis=-1)\n",
24
+ " elif im.ndim == 3:\n",
25
+ " if im.shape[0] == 1: \n",
26
+ " np.transpose(im, (1,2,0))\n",
27
+ " im = np.tile(im, (1,1,3)) #make fake RGB\n",
28
+ " return im\n",
29
+ " elif ndim ==2:\n",
30
+ " if im.ndim == 2:\n",
31
+ " return im\n",
32
+ " if im.ndim == 3:\n",
33
+ " if im.shape[0] == 1: #[1, H, W]\n",
34
+ " return im[0,...]\n",
35
+ " elif im.shape[2] == 1: #[H, W, 1]\n",
36
+ " return im[...,0]\n",
37
+ " else:\n",
38
+ " raise AssertionError(f\"Unexpected image passed to to_uint8 with shape: {np.shape(im)}.\")\n"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 11,
44
+ "metadata": {},
45
+ "outputs": [
46
+ {
47
+ "name": "stdout",
48
+ "output_type": "stream",
49
+ "text": [
50
+ "Running on local URL: http://127.0.0.1:7868\n"
51
+ ]
52
+ },
53
+ {
54
+ "name": "stderr",
55
+ "output_type": "stream",
56
+ "text": [
57
+ "2022-10-11 17:33:10.390 INFO paramiko.transport: Connected (version 2.0, client OpenSSH_7.6p1)\n",
58
+ "2022-10-11 17:33:11.271 INFO paramiko.transport: Authentication (publickey) successful!\n"
59
+ ]
60
+ },
61
+ {
62
+ "name": "stdout",
63
+ "output_type": "stream",
64
+ "text": [
65
+ "Running on public URL: https://23528.gradio.app\n",
66
+ "\n",
67
+ "This share link expires in 72 hours. For free permanent hosting, check out Spaces: https://huggingface.co/spaces\n"
68
+ ]
69
+ },
70
+ {
71
+ "data": {
72
+ "text/html": [
73
+ "<div><iframe src=\"https://23528.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
74
+ ],
75
+ "text/plain": [
76
+ "<IPython.core.display.HTML object>"
77
+ ]
78
+ },
79
+ "metadata": {},
80
+ "output_type": "display_data"
81
+ },
82
+ {
83
+ "name": "stdout",
84
+ "output_type": "stream",
85
+ "text": [
86
+ "Tip: The inputs and outputs flagged by the users are stored in the flagging directory, specified by the flagging_dir= kwarg. You can view this data through the interface by setting the examples= kwarg to the flagging directory; for example gr.Interface(..., examples='flagged')\n"
87
+ ]
88
+ },
89
+ {
90
+ "data": {
91
+ "text/plain": [
92
+ "(<gradio.routes.App at 0x7fcded9da880>,\n",
93
+ " 'http://127.0.0.1:7868/',\n",
94
+ " 'https://23528.gradio.app')"
95
+ ]
96
+ },
97
+ "execution_count": 11,
98
+ "metadata": {},
99
+ "output_type": "execute_result"
100
+ }
101
+ ],
102
+ "source": [
103
+ "\n",
104
+ "\n",
105
+ "in_gpu = False\n",
106
+ "num_images = 1\n",
107
+ "G = utils.load_default_gen(in_gpu=in_gpu)\n",
108
+ "sampler = utils.SampleFromGAN(G=G, z_shp=[num_images, G.z_dim], in_gpu=in_gpu)\n",
109
+ "\n",
110
+ "def sample_GAN():\n",
111
+ " im = sampler()\n",
112
+ " im = im.numpy()\n",
113
+ " im = np.transpose(im, (1,2,0))\n",
114
+ " im = np.squeeze(im) #if single channel (yes), drop it.\n",
115
+ " # print(f\"sample_linearBP: im shape: {im.shape}; min: {np.min(im)}, max: {np.max(im)}.\")\n",
116
+ " im = to_uint8(im, ndim=2)\n",
117
+ " # print(f'1. uint image shape: {im.shape}')\n",
118
+ " return im\n",
119
+ "\n",
120
+ "\n",
121
+ "title=\"Generate fake linear array images\"\n",
122
+ "description=\"Generate fake linear array images.\"\n",
123
+ "\n",
124
+ "with gr.Blocks() as demo:\n",
125
+ " gr.Markdown(description)\n",
126
+ " with gr.Row():\n",
127
+ " with gr.Column(scale=2):\n",
128
+ " button_gen = gr.Button(\"Generate fake linear image\")\n",
129
+ " with gr.Column(scale=2):\n",
130
+ " output_im = gr.Image(type=\"numpy\", shape=(256, 256), image_mode=\"L\", label=\"fake image\", interactive=False) #grayscale image\n",
131
+ " button_gen.click(sample_GAN, inputs=None, outputs=output_im)\n",
132
+ " \n",
133
+ "demo.launch(share=True, show_tips=True, enable_queue=True)\n"
134
+ ]
135
+ }
136
+ ],
137
+ "metadata": {
138
+ "kernelspec": {
139
+ "display_name": "Python 3.9.0 ('torch')",
140
+ "language": "python",
141
+ "name": "python3"
142
+ },
143
+ "language_info": {
144
+ "codemirror_mode": {
145
+ "name": "ipython",
146
+ "version": 3
147
+ },
148
+ "file_extension": ".py",
149
+ "mimetype": "text/x-python",
150
+ "name": "python",
151
+ "nbconvert_exporter": "python",
152
+ "pygments_lexer": "ipython3",
153
+ "version": "3.9.0"
154
+ },
155
+ "orig_nbformat": 4,
156
+ "vscode": {
157
+ "interpreter": {
158
+ "hash": "d9a708e2b965f26293af085a2040b513f9ca1674b4051c9a67720a0b5ff04f14"
159
+ }
160
+ }
161
+ },
162
+ "nbformat": 4,
163
+ "nbformat_minor": 2
164
+ }