Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from urllib.parse import urlparse
|
3 |
+
import requests
|
4 |
+
import time
|
5 |
+
import os
|
6 |
+
import spaces
|
7 |
+
import torch
|
8 |
+
|
9 |
+
zero = torch.Tensor([0]).cuda()
|
10 |
+
print(zero.device) # <-- 'cpu' π€
|
11 |
+
|
12 |
+
names = ['prompt', 'negative_prompt', 'subject', 'number_of_outputs', 'number_of_images_per_pose', 'randomise_poses', 'output_format', 'output_quality', 'seed']
|
13 |
+
|
14 |
+
@spaces.GPU
|
15 |
+
def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
|
16 |
+
print(zero.device) # <-- 'cuda:0' π€
|
17 |
+
headers = {'Content-Type': 'application/json'}
|
18 |
+
|
19 |
+
payload = {"input": {}}
|
20 |
+
|
21 |
+
|
22 |
+
base_url = "http://0.0.0.0:7860"
|
23 |
+
for i, key in enumerate(names):
|
24 |
+
value = args[i]
|
25 |
+
if value and (os.path.exists(str(value))):
|
26 |
+
value = f"{base_url}/file=" + value
|
27 |
+
if value is not None and value != "":
|
28 |
+
payload["input"][key] = value
|
29 |
+
|
30 |
+
response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
|
31 |
+
|
32 |
+
|
33 |
+
if response.status_code == 201:
|
34 |
+
follow_up_url = response.json()["urls"]["get"]
|
35 |
+
response = requests.get(follow_up_url, headers=headers)
|
36 |
+
while response.json()["status"] != "succeeded":
|
37 |
+
if response.json()["status"] == "failed":
|
38 |
+
raise gr.Error("The submission failed!")
|
39 |
+
response = requests.get(follow_up_url, headers=headers)
|
40 |
+
time.sleep(1)
|
41 |
+
if response.status_code == 200:
|
42 |
+
json_response = response.json()
|
43 |
+
#If the output component is JSON return the entire output response
|
44 |
+
if(outputs[0].get_config()["name"] == "json"):
|
45 |
+
return json_response["output"]
|
46 |
+
predict_outputs = parse_outputs(json_response["output"])
|
47 |
+
processed_outputs = process_outputs(predict_outputs)
|
48 |
+
return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
|
49 |
+
else:
|
50 |
+
if(response.status_code == 409):
|
51 |
+
raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
|
52 |
+
raise gr.Error(f"The submission failed! Error: {response.status_code}")
|
53 |
+
|
54 |
+
title = "Demo for consistent-character cog image by fofr"
|
55 |
+
description = "Create images of a given character in different poses β’ running cog image by fofr"
|
56 |
+
|
57 |
+
css="""
|
58 |
+
#col-container{
|
59 |
+
margin: 0 auto;
|
60 |
+
max-width: 1400px;
|
61 |
+
text-align: left;
|
62 |
+
}
|
63 |
+
"""
|
64 |
+
with gr.Blocks(css=css) as app:
|
65 |
+
with gr.Column(elem_id="col-container"):
|
66 |
+
gr.HTML(f"""
|
67 |
+
<h2 style="text-align: center;">Consistent Character Workflow</h2>
|
68 |
+
<p style="text-align: center;">{description}</p>
|
69 |
+
""")
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
with gr.Column(scale=1):
|
73 |
+
prompt = gr.Textbox(
|
74 |
+
label="Prompt", info='''Describe the subject. Include clothes and hairstyle for more consistency.'''
|
75 |
+
)
|
76 |
+
|
77 |
+
subject = gr.Image(
|
78 |
+
label="Subject", type="filepath"
|
79 |
+
)
|
80 |
+
|
81 |
+
submit_btn = gr.Button("Submit")
|
82 |
+
|
83 |
+
with gr.Accordion(label="Advanced Settings", open=False):
|
84 |
+
|
85 |
+
negative_prompt = gr.Textbox(
|
86 |
+
label="Negative Prompt", info='''Things you do not want to see in your image''',
|
87 |
+
value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry"
|
88 |
+
)
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
|
92 |
+
number_of_outputs = gr.Slider(
|
93 |
+
label="Number Of Outputs", info='''The number of images to generate.''', value=2,
|
94 |
+
minimum=1, maximum=4, step=1,
|
95 |
+
)
|
96 |
+
|
97 |
+
number_of_images_per_pose = gr.Slider(
|
98 |
+
label="Number Of Images Per Pose", info='''The number of images to generate for each pose.''', value=1,
|
99 |
+
minimum=1, maximum=4, step=1,
|
100 |
+
)
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
|
104 |
+
randomise_poses = gr.Checkbox(
|
105 |
+
label="Randomise Poses", info='''Randomise the poses used.''', value=True
|
106 |
+
)
|
107 |
+
|
108 |
+
output_format = gr.Dropdown(
|
109 |
+
choices=['webp', 'jpg', 'png'], label="output_format", info='''Format of the output images''', value="webp"
|
110 |
+
)
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
|
114 |
+
output_quality = gr.Number(
|
115 |
+
label="Output Quality", info='''Quality of the output images, from 0 to 100. 100 is best quality, 0 is lowest quality.''', value=80
|
116 |
+
)
|
117 |
+
|
118 |
+
seed = gr.Number(
|
119 |
+
label="Seed", info='''Set a seed for reproducibility. Random by default.''', value=None
|
120 |
+
)
|
121 |
+
|
122 |
+
with gr.Column(scale=1.5):
|
123 |
+
consistent_results = gr.Gallery(label="Consistent Results")
|
124 |
+
|
125 |
+
inputs = [prompt, negative_prompt, subject, number_of_outputs, number_of_images_per_pose, randomise_poses, output_format, output_quality, seed]
|
126 |
+
outputs = [consistent_results]
|
127 |
+
|
128 |
+
submit_btn.click(
|
129 |
+
fn = predict,
|
130 |
+
inputs = inputs,
|
131 |
+
outputs = outputs,
|
132 |
+
show_api = False
|
133 |
+
)
|
134 |
+
|
135 |
+
app.queue(max_size=12, api_open=False).launch(share=False, show_api=False)
|