Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -166,7 +166,7 @@ class APIError(Exception):
|
|
166 |
pass
|
167 |
|
168 |
|
169 |
-
def create_request(prompt, image, guidance_scale=5.0, image_guidance_scale=4.0, seed=-1):
|
170 |
"""
|
171 |
Create an image editing request to the API.
|
172 |
|
@@ -176,6 +176,7 @@ def create_request(prompt, image, guidance_scale=5.0, image_guidance_scale=4.0,
|
|
176 |
guidance_scale (float): Strength of instruction following
|
177 |
image_guidance_scale (float): Strength of image preservation
|
178 |
seed (int): Seed for reproducibility, -1 for random
|
|
|
179 |
|
180 |
Returns:
|
181 |
tuple: (task_id, seed) - Task ID if successful and the seed used
|
@@ -230,7 +231,8 @@ def create_request(prompt, image, guidance_scale=5.0, image_guidance_scale=4.0,
|
|
230 |
"custom_params": {
|
231 |
"sample_steps": 28,
|
232 |
"guidance_scale": guidance_scale,
|
233 |
-
"image_guidance_scale": image_guidance_scale
|
|
|
234 |
},
|
235 |
},
|
236 |
"version": API_VERSION,
|
@@ -620,13 +622,25 @@ def create_ui():
|
|
620 |
label="Image Preservation Strength"
|
621 |
)
|
622 |
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
628 |
|
629 |
-
|
|
|
|
|
|
|
|
|
630 |
|
631 |
progress = gr.Progress(track_tqdm=False)
|
632 |
|
@@ -663,7 +677,7 @@ def create_ui():
|
|
663 |
return instruction
|
664 |
|
665 |
# Generate function with progress updates
|
666 |
-
def generate_with_progress(image, instruction, seed, guidance_scale, image_guidance_scale, progress=gr.Progress()):
|
667 |
logger.info(f"Starting image generation with instruction='{instruction[:50]}...', seed={seed}")
|
668 |
|
669 |
try:
|
@@ -682,7 +696,8 @@ def create_ui():
|
|
682 |
image=image,
|
683 |
guidance_scale=guidance_scale,
|
684 |
image_guidance_scale=image_guidance_scale,
|
685 |
-
seed=seed
|
|
|
686 |
)
|
687 |
|
688 |
# Poll for results
|
@@ -735,6 +750,7 @@ def create_ui():
|
|
735 |
"model": API_MODEL_NAME,
|
736 |
"guidance_scale": str(guidance_scale),
|
737 |
"image_guidance_scale": str(image_guidance_scale),
|
|
|
738 |
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
739 |
"generated_by": "HiDream-E1-Full Editor"
|
740 |
}
|
@@ -748,6 +764,7 @@ def create_ui():
|
|
748 |
"seed": used_seed,
|
749 |
"guidance_scale": guidance_scale,
|
750 |
"image_guidance_scale": image_guidance_scale,
|
|
|
751 |
"generated_at": time.strftime("%Y-%m-%d %H:%M:%S")
|
752 |
}
|
753 |
|
@@ -781,12 +798,12 @@ def create_ui():
|
|
781 |
|
782 |
generate_btn.click(
|
783 |
fn=generate_with_progress,
|
784 |
-
inputs=[input_image, instruction, seed, guidance_scale, image_guidance_scale],
|
785 |
outputs=[output_image, image_info]
|
786 |
)
|
787 |
|
788 |
# Define a combined function to refine instruction and then generate image
|
789 |
-
def refine_and_generate(image, instruction, seed, guidance_scale, image_guidance_scale, progress=gr.Progress()):
|
790 |
try:
|
791 |
# First refine the instruction
|
792 |
if not image or not instruction:
|
@@ -804,7 +821,7 @@ def create_ui():
|
|
804 |
|
805 |
# Then generate with the refined instruction
|
806 |
progress(0.2, desc="Instruction refined, generating image...")
|
807 |
-
generated_image, image_info = generate_with_progress(image, refined_instruction, seed, guidance_scale, image_guidance_scale, progress)
|
808 |
return generated_image, image_info, refined_instruction
|
809 |
except Exception as e:
|
810 |
logger.error(f"Error in refine_and_generate: {str(e)}")
|
@@ -815,14 +832,14 @@ def create_ui():
|
|
815 |
# Examples
|
816 |
gr.Examples(
|
817 |
examples=[
|
818 |
-
["assets/test_1.png", "convert the image into a Ghibli style",82706, 5, 4],
|
819 |
-
["assets/test_1.png", "change the image into Disney Pixar style",82706, 5, 4],
|
820 |
-
["assets/test_1.png", "add a sunglasses to the girl",82706, 5, 2],
|
821 |
-
["assets/test_2.jpg", "convert this image into a ink sketch image",82706, 5, 2],
|
822 |
-
["assets/test_2.jpg", "add butterfly",82706, 5, 2],
|
823 |
-
["assets/test_2.jpg", "remove the wooden sign",82706, 5, 2],
|
824 |
],
|
825 |
-
inputs=[input_image, instruction, seed, guidance_scale, image_guidance_scale],
|
826 |
outputs=[output_image, image_info, instruction],
|
827 |
fn=refine_and_generate,
|
828 |
cache_examples=True,
|
|
|
166 |
pass
|
167 |
|
168 |
|
169 |
+
def create_request(prompt, image, guidance_scale=5.0, image_guidance_scale=4.0, seed=-1, refine_strength=0.3):
|
170 |
"""
|
171 |
Create an image editing request to the API.
|
172 |
|
|
|
176 |
guidance_scale (float): Strength of instruction following
|
177 |
image_guidance_scale (float): Strength of image preservation
|
178 |
seed (int): Seed for reproducibility, -1 for random
|
179 |
+
refine_strength (float): Strength of refinement
|
180 |
|
181 |
Returns:
|
182 |
tuple: (task_id, seed) - Task ID if successful and the seed used
|
|
|
231 |
"custom_params": {
|
232 |
"sample_steps": 28,
|
233 |
"guidance_scale": guidance_scale,
|
234 |
+
"image_guidance_scale": image_guidance_scale,
|
235 |
+
"refine_strength": refine_strength
|
236 |
},
|
237 |
},
|
238 |
"version": API_VERSION,
|
|
|
622 |
label="Image Preservation Strength"
|
623 |
)
|
624 |
|
625 |
+
with gr.Row():
|
626 |
+
refine_strength = gr.Slider(
|
627 |
+
minimum=0.0,
|
628 |
+
maximum=1.0,
|
629 |
+
step=0.01,
|
630 |
+
value=0.3,
|
631 |
+
label="Refinement Strength"
|
632 |
+
)
|
633 |
+
seed = gr.Number(
|
634 |
+
label="Seed (use -1 for random)",
|
635 |
+
value=82706,
|
636 |
+
precision=0
|
637 |
+
)
|
638 |
|
639 |
+
gr.Markdown("""
|
640 |
+
<div style="padding: 8px; margin: 15px 0; background-color: #FFF3CD; border-left: 5px solid #FFDD57; color: #856404;">
|
641 |
+
<strong>Refinement Strength:</strong> Controls balance between editing and refinement. Higher values enhance details but may reduce editing strength. Set to 0 to disable refinement.
|
642 |
+
</div>
|
643 |
+
""")
|
644 |
|
645 |
progress = gr.Progress(track_tqdm=False)
|
646 |
|
|
|
677 |
return instruction
|
678 |
|
679 |
# Generate function with progress updates
|
680 |
+
def generate_with_progress(image, instruction, seed, guidance_scale, image_guidance_scale, refine_strength, progress=gr.Progress()):
|
681 |
logger.info(f"Starting image generation with instruction='{instruction[:50]}...', seed={seed}")
|
682 |
|
683 |
try:
|
|
|
696 |
image=image,
|
697 |
guidance_scale=guidance_scale,
|
698 |
image_guidance_scale=image_guidance_scale,
|
699 |
+
seed=seed,
|
700 |
+
refine_strength=refine_strength
|
701 |
)
|
702 |
|
703 |
# Poll for results
|
|
|
750 |
"model": API_MODEL_NAME,
|
751 |
"guidance_scale": str(guidance_scale),
|
752 |
"image_guidance_scale": str(image_guidance_scale),
|
753 |
+
"refine_strength": str(refine_strength),
|
754 |
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
755 |
"generated_by": "HiDream-E1-Full Editor"
|
756 |
}
|
|
|
764 |
"seed": used_seed,
|
765 |
"guidance_scale": guidance_scale,
|
766 |
"image_guidance_scale": image_guidance_scale,
|
767 |
+
"refine_strength": refine_strength,
|
768 |
"generated_at": time.strftime("%Y-%m-%d %H:%M:%S")
|
769 |
}
|
770 |
|
|
|
798 |
|
799 |
generate_btn.click(
|
800 |
fn=generate_with_progress,
|
801 |
+
inputs=[input_image, instruction, seed, guidance_scale, image_guidance_scale, refine_strength],
|
802 |
outputs=[output_image, image_info]
|
803 |
)
|
804 |
|
805 |
# Define a combined function to refine instruction and then generate image
|
806 |
+
def refine_and_generate(image, instruction, seed, guidance_scale, image_guidance_scale, refine_strength, progress=gr.Progress()):
|
807 |
try:
|
808 |
# First refine the instruction
|
809 |
if not image or not instruction:
|
|
|
821 |
|
822 |
# Then generate with the refined instruction
|
823 |
progress(0.2, desc="Instruction refined, generating image...")
|
824 |
+
generated_image, image_info = generate_with_progress(image, refined_instruction, seed, guidance_scale, image_guidance_scale, refine_strength, progress)
|
825 |
return generated_image, image_info, refined_instruction
|
826 |
except Exception as e:
|
827 |
logger.error(f"Error in refine_and_generate: {str(e)}")
|
|
|
832 |
# Examples
|
833 |
gr.Examples(
|
834 |
examples=[
|
835 |
+
["assets/test_1.png", "convert the image into a Ghibli style",82706, 5, 4, 0.3],
|
836 |
+
["assets/test_1.png", "change the image into Disney Pixar style",82706, 5, 4, 0.3],
|
837 |
+
["assets/test_1.png", "add a sunglasses to the girl",82706, 5, 2, 0.3],
|
838 |
+
["assets/test_2.jpg", "convert this image into a ink sketch image",82706, 5, 2, 0.3],
|
839 |
+
["assets/test_2.jpg", "add butterfly",82706, 5, 2, 0.3],
|
840 |
+
["assets/test_2.jpg", "remove the wooden sign",82706, 5, 2, 0.3],
|
841 |
],
|
842 |
+
inputs=[input_image, instruction, seed, guidance_scale, image_guidance_scale, refine_strength],
|
843 |
outputs=[output_image, image_info, instruction],
|
844 |
fn=refine_and_generate,
|
845 |
cache_examples=True,
|