Spaces:
Running
on
Zero
Running
on
Zero
MohamedRashad
commited on
Commit
•
60f5157
1
Parent(s):
339482d
chore: Refactor image enhancement functionality and update requirements for diffusers
Browse files- app.py +33 -89
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,85 +1,36 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
image_description = joy_client.predict(
|
32 |
-
input_image=handle_file(image_path),
|
33 |
-
caption_type="Descriptive",
|
34 |
-
caption_length="long",
|
35 |
-
extra_options=[],
|
36 |
-
name_input="",
|
37 |
-
custom_prompt="",
|
38 |
-
api_name="/stream_chat"
|
39 |
-
)[-1]
|
40 |
-
print(f"{Fore.GREEN}{image_description}{Style.RESET_ALL}")
|
41 |
-
return image_description
|
42 |
-
|
43 |
-
def refine_prompt(image_description):
|
44 |
-
print(f"Improving prompt: {image_description}")
|
45 |
-
qwen_prompt = f"""This is the description of the image: {image_description}
|
46 |
-
|
47 |
-
And those some good AI Art Prompts:
|
48 |
-
- a cat on a windowsill gazing out at a starry night sky and distant city lights
|
49 |
-
- a fisherman casting a line into a peaceful village lake surrounded by quaint cottages
|
50 |
-
- cozy mountain cabin covered in snow, with smoke curling from the chimney and a warm, inviting light spilling through the windows
|
51 |
-
- Mykonos
|
52 |
-
- an orange Lamborghini driving down a hill road at night with a beautiful ocean view in the background, side view, no text
|
53 |
-
- a small Yorkie on a windowsill during a snowy winter night, with a warm, cozy glow from inside and soft snowflakes drifting outside
|
54 |
-
- serene Japanese garden with a koi pond and a traditional tea house, nestled under a canopy of cherry blossoms in full bloom
|
55 |
-
- the most beautiful place in the universe
|
56 |
-
|
57 |
-
Based on what i gave you, Write a great short AI Art Prompt for me that is based on the image description above (Don't write anything else, just the prompt)
|
58 |
-
"""
|
59 |
-
refined_prompt = qwen_client.predict(
|
60 |
-
query=qwen_prompt,
|
61 |
-
history=[],
|
62 |
-
system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
|
63 |
-
api_name="/model_chat"
|
64 |
-
)[1][0][-1]
|
65 |
-
print(f"{Fore.GREEN}{refined_prompt}{Style.RESET_ALL}")
|
66 |
-
return refined_prompt
|
67 |
-
|
68 |
-
@spaces.GPU(duration=75)
|
69 |
-
def img2img_infer(image_path, image_description):
|
70 |
-
pil_image = Image.open(image_path)
|
71 |
-
width, height = pil_image.size
|
72 |
-
for enhanced_image in pipeline.flux_pipe_call_that_returns_an_iterable_of_images(
|
73 |
-
prompt=f'GHIBSKY style, {image_description}',
|
74 |
-
image=pil_image,
|
75 |
-
# guidance_scale=3.5,
|
76 |
-
# num_inference_steps=28,
|
77 |
-
# width=1024,
|
78 |
-
# height=1024,
|
79 |
-
# generator=torch.Generator("cpu").manual_seed(0),
|
80 |
-
output_type="pil",
|
81 |
-
):
|
82 |
-
yield enhanced_image.resize((width, height))
|
83 |
|
84 |
|
85 |
with gr.Blocks(title="Magnific") as demo:
|
@@ -89,17 +40,10 @@ with gr.Blocks(title="Magnific") as demo:
|
|
89 |
with gr.Row():
|
90 |
with gr.Column():
|
91 |
image_path = gr.Image(label="Image", type="filepath")
|
92 |
-
|
93 |
-
describe_btn = gr.Button(value="Describe Image", variant="primary")
|
94 |
-
with gr.Row(equal_height=True):
|
95 |
-
image_description = gr.Textbox(label="Image Description", scale=4)
|
96 |
-
refine_prompt_btn = gr.Button(value="Refine", variant="primary", scale=1)
|
97 |
submit_btn = gr.Button(value="Submit", variant="primary")
|
98 |
enhanced_image = gr.Image(label="Enhanced Image", type="pil")
|
99 |
|
100 |
-
|
101 |
-
refine_prompt_btn.click(refine_prompt, inputs=image_description, outputs=image_description)
|
102 |
-
describe_btn.click(describe_image, inputs=image_path, outputs=image_description)
|
103 |
-
submit_btn.click(img2img_infer, inputs=[image_path, image_description], outputs=enhanced_image)
|
104 |
|
105 |
demo.queue().launch(share=False)
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import FluxPriorReduxPipeline, FluxPipeline
|
3 |
+
from diffusers.utils import load_image
|
4 |
import gradio as gr
|
5 |
import spaces
|
6 |
+
# from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
|
7 |
+
|
8 |
+
pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained("black-forest-labs/FLUX.1-Redux-dev", revision="refs/pr/8", torch_dtype=torch.bfloat16).to("cuda")
|
9 |
+
pipe = FluxPipeline.from_pretrained(
|
10 |
+
"black-forest-labs/FLUX.1-dev" ,
|
11 |
+
text_encoder=None,
|
12 |
+
text_encoder_2=None,
|
13 |
+
torch_dtype=torch.bfloat16
|
14 |
+
)
|
15 |
+
# pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
16 |
+
# pipe.enable_sequential_cpu_offload()
|
17 |
+
|
18 |
+
@spaces.GPU(duration=120)
|
19 |
+
def enhance_image(image_path, keep_aspect_ratio=False):
|
20 |
+
print(image_path)
|
21 |
+
image = load_image(image_path)
|
22 |
+
print(image.size)
|
23 |
+
width, height = image.size if keep_aspect_ratio else (None, None)
|
24 |
+
pipe_prior_output = pipe_prior_redux(image)
|
25 |
+
images = pipe(
|
26 |
+
height=height,
|
27 |
+
width=width,
|
28 |
+
guidance_scale=2.5,
|
29 |
+
num_inference_steps=50,
|
30 |
+
generator=torch.Generator("cpu").manual_seed(0),
|
31 |
+
**pipe_prior_output,
|
32 |
+
).images
|
33 |
+
return images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
|
36 |
with gr.Blocks(title="Magnific") as demo:
|
|
|
40 |
with gr.Row():
|
41 |
with gr.Column():
|
42 |
image_path = gr.Image(label="Image", type="filepath")
|
43 |
+
keep_aspect_ratio = gr.Checkbox(label="Keep Aspect Ratio", value=False)
|
|
|
|
|
|
|
|
|
44 |
submit_btn = gr.Button(value="Submit", variant="primary")
|
45 |
enhanced_image = gr.Image(label="Enhanced Image", type="pil")
|
46 |
|
47 |
+
submit_btn.click(enhance_image, inputs=[image_path, keep_aspect_ratio], outputs=enhanced_image)
|
|
|
|
|
|
|
48 |
|
49 |
demo.queue().launch(share=False)
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
colorama
|
2 |
gradio-client
|
3 |
-
diffusers
|
4 |
torch
|
5 |
transformers
|
6 |
spaces
|
|
|
1 |
colorama
|
2 |
gradio-client
|
3 |
+
git+https://github.com/huggingface/diffusers.git
|
4 |
torch
|
5 |
transformers
|
6 |
spaces
|