Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from urllib.request import Request, urlopen
|
4 |
+
|
5 |
+
def display_image_from_url(url, input_image):
|
6 |
+
if url == '' and input_image is None:
|
7 |
+
return None, "", ""
|
8 |
+
|
9 |
+
image = None
|
10 |
+
if url != '':
|
11 |
+
req = Request(
|
12 |
+
url=url,
|
13 |
+
headers={'User-Agent': 'Mozilla/5.0'}
|
14 |
+
)
|
15 |
+
res = urlopen(req)
|
16 |
+
image = Image.open(res)
|
17 |
+
image.load()
|
18 |
+
|
19 |
+
|
20 |
+
if input_image is not None:
|
21 |
+
image = input_image
|
22 |
+
|
23 |
+
parameters = "Parameters have been erased from this image or unsupported format"
|
24 |
+
if 'parameters' in image.info:
|
25 |
+
|
26 |
+
parameters = image.info['parameters']
|
27 |
+
|
28 |
+
custom_notes = ""
|
29 |
+
if 'custom_notes' in image.info:
|
30 |
+
custom_notes = image.info['custom_notes']
|
31 |
+
|
32 |
+
return image, parameters, custom_notes, image.info
|
33 |
+
|
34 |
+
blocks = gr.Blocks(css="#out_image {height: 400px}")
|
35 |
+
with blocks as png_info:
|
36 |
+
with gr.Row():
|
37 |
+
gr.Markdown(
|
38 |
+
"""
|
39 |
+
Report any issues on the [GitHub](https://github.com/andzhik/png-params) page of this project
|
40 |
+
""")
|
41 |
+
with gr.Row().style(equal_height=False):
|
42 |
+
with gr.Column(scale=1):
|
43 |
+
in_url = gr.Textbox(label="Source URL")
|
44 |
+
in_image = gr.Image(label="Source Image", type='pil')
|
45 |
+
with gr.Row():
|
46 |
+
btn_submit = gr.Button("Submit", variant="primary")
|
47 |
+
|
48 |
+
with gr.Column(scale=2):
|
49 |
+
with gr.Accordion("Image is here") as acc_image:
|
50 |
+
out_image = gr.Image(type='pil', elem_id="out_image")
|
51 |
+
|
52 |
+
out_info = gr.Textbox(label="Generation Parameters")
|
53 |
+
|
54 |
+
out_notes = gr.TextArea(label="Custom Notes", interactive=True)
|
55 |
+
# download_file = gr.File()
|
56 |
+
btn_save_notes = gr.Button("Save Notes")
|
57 |
+
# btn_download = gr.Button("Download Image")
|
58 |
+
|
59 |
+
with gr.Accordion("Metadata", open=False):
|
60 |
+
out_meta = gr.Textbox()
|
61 |
+
|
62 |
+
btn_submit.click(fn=display_image_from_url,
|
63 |
+
inputs=[in_url, in_image],
|
64 |
+
outputs=[out_image, out_info, out_notes, out_meta])
|
65 |
+
|
66 |
+
def save_notes(image, custom_notes):
|
67 |
+
print(custom_notes)
|
68 |
+
image.info["custom_notes"] = custom_notes
|
69 |
+
return image
|
70 |
+
|
71 |
+
btn_save_notes.click(fn=save_notes,inputs=[out_image, out_notes], outputs=[out_image])
|
72 |
+
|
73 |
+
# def download_image(image: Image):
|
74 |
+
# print(image.info["custom_notes"])
|
75 |
+
# image.save()
|
76 |
+
|
77 |
+
# btn_download.click(None, [out_image], _js="(image)=>{gradioApp().getElementById('out_image')}")
|
78 |
+
|
79 |
+
png_info.launch()
|