MohamedRashad commited on
Commit
60f5157
1 Parent(s): 339482d

chore: Refactor image enhancement functionality and update requirements for diffusers

Browse files
Files changed (2) hide show
  1. app.py +33 -89
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,85 +1,36 @@
 
 
 
1
  import gradio as gr
2
  import spaces
3
- import torch
4
- from gradio_client import Client, handle_file
5
- from colorama import Fore, Style
6
- from diffusers import AutoPipelineForImage2Image
7
- from PIL import Image
8
- from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
9
-
10
- joy_client = Client("fancyfeast/joy-caption-alpha-two")
11
- qwen_client = Client("Qwen/Qwen2.5-72B-Instruct")
12
-
13
- pipeline = AutoPipelineForImage2Image.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
14
- pipeline.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipeline)
15
-
16
- lora_ids = {
17
- # "Realism": "XLabs-AI/flux-RealismLora",
18
- "Cartoonism": "aleksa-codes/flux-ghibsky-illustration",
19
- }
20
-
21
- def load_lora(lora_name):
22
- print(f"Loading LoRA model: {lora_name}")
23
- global pipeline
24
- pipeline.unload_lora_weights()
25
- pipeline.load_lora_weights(lora_ids[lora_name])
26
- pipeline.enable_model_cpu_offload()
27
- print(f"{Fore.GREEN}LoRA model loaded{Style.RESET_ALL}")
28
-
29
- def describe_image(image_path):
30
- print(f"Describing image: {image_path}")
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
- lora_dropdown = gr.Dropdown(label="LoRA Model", choices=list(lora_ids.keys()), value=None)
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
- lora_dropdown.change(load_lora, inputs=lora_dropdown)
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