Sqxww commited on
Commit
2066d76
·
1 Parent(s): 70616db

feat: change image style

Browse files
Files changed (3) hide show
  1. .gitignore +25 -0
  2. app.py +79 -0
  3. requirements.txt +0 -0
.gitignore ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Binaries for programs and plugins
2
+ *.exe
3
+ *.exe~
4
+ *.dll
5
+ *.so
6
+ *.dylib
7
+
8
+ # Test binary, built with `go test -c`
9
+ *.test
10
+
11
+ # Output of the go coverage tool, specifically when used with LiteIDE
12
+ *.out
13
+ .idea/
14
+ nacos/
15
+ logs/
16
+ build/
17
+
18
+ # IntelliJ related
19
+ *.iml
20
+ *.ipr
21
+ *.iws
22
+ *.db
23
+
24
+ .DS_Store
25
+ .vscode/
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ prefix = "https://smartfeed-custom-tools.hf.space/file="
5
+
6
+
7
+ def change_image_style(image_url, style_image_url):
8
+ data = {
9
+ "image_url": image_url,
10
+ "style_image_url": style_image_url
11
+ }
12
+
13
+ response = requests.post(
14
+ "https://api.hkhappymobile.com/tools/change-image-style",
15
+ json=data,
16
+ headers={"content-type": "application/json"}
17
+ )
18
+
19
+ if response.status_code == 200:
20
+ resultImageUrl = response.json().get("data").get("image_url")
21
+ return resultImageUrl
22
+ else:
23
+ raise Exception(f"Error: {response.status_code} - {response.text}")
24
+
25
+ def generate_image(input_image, style_images):
26
+
27
+ if not input_image:
28
+ raise gr.Error(f"Please upload an input image! Refer to step 1️⃣")
29
+
30
+ if style_images is None:
31
+ raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
32
+
33
+ inputImageUrl = prefix + input_image
34
+
35
+ result_images = list[str]()
36
+ for style_image in style_images:
37
+ if not style_image:
38
+ raise gr.Error(f"Cannot find any style image! Please refer to step 1️⃣")
39
+ styleImageUrl = prefix + style_image[0]
40
+ result_images.append(change_image_style(inputImageUrl, styleImageUrl))
41
+
42
+ return result_images
43
+
44
+
45
+ def swap_to_gallery(images):
46
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
47
+
48
+ def remove_back_to_files():
49
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
50
+
51
+ with gr.Blocks() as demo:
52
+ with gr.Row():
53
+ with gr.Column():
54
+ input_image = gr.Image(label="Input Image", type="filepath", interactive=True)
55
+
56
+ files = gr.File(
57
+ label="Drag (Select) 1 or more style images",
58
+ file_types=["image"],
59
+ file_count="multiple"
60
+ )
61
+ uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=200)
62
+ with gr.Column(visible=False) as clear_button:
63
+ remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
64
+ submit = gr.Button("Submit")
65
+
66
+ with gr.Column():
67
+ gallery = gr.Gallery(label="Generated Images")
68
+
69
+ files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
70
+ remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
71
+
72
+ submit.click(
73
+ fn=generate_image,
74
+ inputs=[input_image, uploaded_files],
75
+ outputs=[gallery]
76
+ )
77
+
78
+
79
+ demo.launch()
requirements.txt ADDED
File without changes