Spaces:
Running
on
Zero
Running
on
Zero
add upload to r2
Browse files- __pycache__/live_preview_helpers.cpython-310.pyc +0 -0
- app.py +45 -6
- requirements.txt +2 -1
__pycache__/live_preview_helpers.cpython-310.pyc
CHANGED
Binary files a/__pycache__/live_preview_helpers.cpython-310.pyc and b/__pycache__/live_preview_helpers.cpython-310.pyc differ
|
|
app.py
CHANGED
@@ -14,6 +14,8 @@ from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_d
|
|
14 |
import copy
|
15 |
import random
|
16 |
import time
|
|
|
|
|
17 |
|
18 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
19 |
|
@@ -49,6 +51,28 @@ class calculateDuration:
|
|
49 |
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
50 |
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
@spaces.GPU(duration=70)
|
53 |
def generate_image(prompt, steps, seed, cfg_scale, width, height, lora_scale, progress):
|
54 |
pipe.to("cuda")
|
@@ -69,7 +93,7 @@ def generate_image(prompt, steps, seed, cfg_scale, width, height, lora_scale, pr
|
|
69 |
yield img
|
70 |
|
71 |
|
72 |
-
def run_lora(prompt, cfg_scale, steps, lora_repo, lora_name, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
73 |
|
74 |
with calculateDuration("Unloading LoRA"):
|
75 |
pipe.unload_lora_weights()
|
@@ -88,13 +112,20 @@ def run_lora(prompt, cfg_scale, steps, lora_repo, lora_name, randomize_seed, see
|
|
88 |
# Consume the generator to get the final image
|
89 |
final_image = None
|
90 |
step_counter = 0
|
|
|
91 |
for image in image_generator:
|
92 |
step_counter+=1
|
93 |
final_image = image
|
94 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
95 |
-
yield image, seed, gr.update(value=progress_bar, visible=True)
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
|
100 |
css="""
|
@@ -129,15 +160,23 @@ with gr.Blocks(css=css) as demo:
|
|
129 |
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
130 |
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
with gr.Column():
|
133 |
progress_bar = gr.Markdown(elem_id="progress",visible=False)
|
134 |
result = gr.Image(label="Result", show_label=False)
|
|
|
135 |
|
136 |
gr.on(
|
137 |
triggers=[run_button.click, prompt.submit],
|
138 |
fn = run_lora,
|
139 |
-
inputs = [prompt, cfg_scale, steps, lora_repo, lora_name, randomize_seed, seed, width, height, lora_scale],
|
140 |
-
outputs=[result, seed, progress_bar]
|
141 |
)
|
142 |
|
143 |
demo.queue().launch()
|
|
|
14 |
import copy
|
15 |
import random
|
16 |
import time
|
17 |
+
import boto3
|
18 |
+
from io import BytesIO
|
19 |
|
20 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
21 |
|
|
|
51 |
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
52 |
|
53 |
|
54 |
+
def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
|
55 |
+
print("upload_image_to_r2", account_id, access_key, secret_key, bucket_name)
|
56 |
+
connectionUrl = f"https://{account_id}.r2.cloudflarestorage.com"
|
57 |
+
|
58 |
+
s3 = boto3.client(
|
59 |
+
's3',
|
60 |
+
endpoint_url=connectionUrl,
|
61 |
+
region_name='auto',
|
62 |
+
aws_access_key_id=access_key,
|
63 |
+
aws_secret_access_key=secret_key
|
64 |
+
)
|
65 |
+
|
66 |
+
current_time = datetime.now().strftime("%Y/%m/%d/%H%M%S")
|
67 |
+
image_file = f"generated_images/{current_time}_{random.randint(0, MAX_SEED)}.png"
|
68 |
+
buffer = BytesIO()
|
69 |
+
image.save(buffer, "PNG")
|
70 |
+
buffer.seek(0)
|
71 |
+
s3.upload_fileobj(buffer, bucket_name, image_file)
|
72 |
+
print("upload finish", image_file)
|
73 |
+
return image_file
|
74 |
+
|
75 |
+
|
76 |
@spaces.GPU(duration=70)
|
77 |
def generate_image(prompt, steps, seed, cfg_scale, width, height, lora_scale, progress):
|
78 |
pipe.to("cuda")
|
|
|
93 |
yield img
|
94 |
|
95 |
|
96 |
+
def run_lora(prompt, cfg_scale, steps, lora_repo, lora_name, randomize_seed, seed, width, height, lora_scale, upload_to_r2, account_id, access_key, secret_key, bucket, progress=gr.Progress(track_tqdm=True)):
|
97 |
|
98 |
with calculateDuration("Unloading LoRA"):
|
99 |
pipe.unload_lora_weights()
|
|
|
112 |
# Consume the generator to get the final image
|
113 |
final_image = None
|
114 |
step_counter = 0
|
115 |
+
final_image = None
|
116 |
for image in image_generator:
|
117 |
step_counter+=1
|
118 |
final_image = image
|
119 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
120 |
+
yield image, seed, gr.update(value=progress_bar, visible=True), json.dumps({"status": "processing"})
|
121 |
+
|
122 |
+
if upload_to_r2:
|
123 |
+
url = upload_image_to_r2(final_image, account_id, access_key, secret_key, bucket)
|
124 |
+
result = {"status": "success", "url": url}
|
125 |
+
else:
|
126 |
+
result = {"status": "success", "message": "Image generated but not uploaded"}
|
127 |
+
|
128 |
+
yield final_image, seed, gr.update(value=progress_bar, visible=False), json.dumps(result)
|
129 |
|
130 |
|
131 |
css="""
|
|
|
160 |
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
161 |
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
162 |
|
163 |
+
upload_to_r2 = gr.Checkbox(label="Upload to R2", value=False)
|
164 |
+
account_id = gr.Textbox(label="Account Id", placeholder="Enter R2 account id")
|
165 |
+
access_key = gr.Textbox(label="Access Key", placeholder="Enter R2 access key here")
|
166 |
+
secret_key = gr.Textbox(label="Secret Key", placeholder="Enter R2 secret key here")
|
167 |
+
bucket = gr.Textbox(label="Bucket Name", placeholder="Enter R2 bucket name here")
|
168 |
+
|
169 |
+
|
170 |
with gr.Column():
|
171 |
progress_bar = gr.Markdown(elem_id="progress",visible=False)
|
172 |
result = gr.Image(label="Result", show_label=False)
|
173 |
+
json_text = gr.Text()
|
174 |
|
175 |
gr.on(
|
176 |
triggers=[run_button.click, prompt.submit],
|
177 |
fn = run_lora,
|
178 |
+
inputs = [prompt, cfg_scale, steps, lora_repo, lora_name, randomize_seed, seed, width, height, lora_scale, upload_to_r2, account_id, access_key, secret_key, bucket],
|
179 |
+
outputs=[result, seed, progress_bar, json_text]
|
180 |
)
|
181 |
|
182 |
demo.queue().launch()
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ git+https://github.com/huggingface/diffusers@3b604e8c384631e1f66a4fd9076ed5e7e2b
|
|
3 |
spaces
|
4 |
transformers
|
5 |
peft
|
6 |
-
sentencepiece
|
|
|
|
3 |
spaces
|
4 |
transformers
|
5 |
peft
|
6 |
+
sentencepiece
|
7 |
+
boto3
|