anupa12 commited on
Commit
6d1088c
·
verified ·
1 Parent(s): 29162b4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -175
app.py CHANGED
@@ -1,185 +1,47 @@
 
1
  import gradio as gr
2
  import torch
3
- import spaces
4
- from diffusers import FluxInpaintPipeline
5
- from PIL import Image, ImageFile
6
 
7
- #ImageFile.LOAD_TRUNCATED_IMAGES = True
8
 
9
- # Initialize the pipeline
10
- pipe = FluxInpaintPipeline.from_pretrained(
11
- "black-forest-labs/FLUX.1-dev",
12
- torch_dtype=torch.bfloat16
13
- )
14
- pipe.to("cuda")
15
- pipe.load_lora_weights(
16
- "ali-vilab/In-Context-LoRA",
17
- weight_name="visual-identity-design.safetensors"
18
- )
19
 
20
- def square_center_crop(img, target_size=768):
21
- if img.mode in ('RGBA', 'P'):
22
- img = img.convert('RGB')
23
 
24
- width, height = img.size
25
- crop_size = min(width, height)
26
 
27
- left = (width - crop_size) // 2
28
- top = (height - crop_size) // 2
29
- right = left + crop_size
30
- bottom = top + crop_size
31
 
32
- img_cropped = img.crop((left, top, right, bottom))
33
- return img_cropped.resize((target_size, target_size), Image.Resampling.LANCZOS)
34
-
35
- def duplicate_horizontally(img):
36
- width, height = img.size
37
- if width != height:
38
- raise ValueError(f"Input image must be square, got {width}x{height}")
39
-
40
- new_image = Image.new('RGB', (width * 2, height))
41
- new_image.paste(img, (0, 0))
42
- new_image.paste(img, (width, 0))
43
- return new_image
44
-
45
-
46
- @spaces.GPU
47
- def generate(image, prompt_description, prompt_user, progress=gr.Progress(track_tqdm=True)):
48
- prompt_structure = "The two-panel image showcases the logo on the left and the application on the right, [LEFT] the left panel is showing "+prompt_description+" [RIGHT] this logo is applied to "
49
- prompt = prompt_structure + prompt_user
50
-
51
- mask = Image.open("mask_square.png")
52
- cropped_image = square_center_crop(image)
53
- logo_dupli = duplicate_horizontally(cropped_image)
54
-
55
- out = pipe(
56
- prompt=prompt,
57
- image=logo_dupli,
58
- mask_image=mask,
59
- guidance_scale=3.5,
60
- height=768,
61
- width=1536,
62
- num_inference_steps=28,
63
- max_sequence_length=256,
64
- strength=1
65
- ).images[0]
66
-
67
- width, height = out.size
68
- half_width = width // 2
69
- image_2 = out.crop((half_width, 0, width, height))
70
- return image_2, out
71
-
72
- with gr.Blocks() as demo:
73
- gr.Markdown("# Logo in Context")
74
- gr.Markdown("### [In-Context LoRA](https://huggingface.co/ali-vilab/In-Context-LoRA) + Image-to-Image + Inpainting, apply your logo to anything. diffusers implementation based on the [workflow by WizardWhitebeard/klinter](https://civitai.com/articles/8779)")
75
-
76
- with gr.Tab("Demo"):
77
- with gr.Row():
78
- with gr.Column():
79
- input_image = gr.Image(
80
- label="Upload Logo Image",
81
- type="pil",
82
- height=384
83
- )
84
- prompt_description = gr.Textbox(
85
- label="Describe your logo",
86
- placeholder="A Hugging Face emoji logo",
87
- )
88
- prompt_input = gr.Textbox(
89
- label="Where should the logo be applied?",
90
- placeholder="e.g., a coffee cup on a wooden table"
91
- )
92
- generate_btn = gr.Button("Generate Application", variant="primary")
93
-
94
- with gr.Column():
95
- output_image = gr.Image(label="Generated Application")
96
- output_side = gr.Image(label="Side by side")
97
-
98
- gr.Examples(
99
- examples=[
100
- ["huggingface.png", "A Hugging Face emoji logo", "An embroidered hat"],
101
- ["awesome.png", "An awesome face logo", "A tattoo on a leg"],
102
- ["dvd_logo.png", "A DVD logo", "a coconut, engraved logo on a green coconut"]
103
- ],
104
- inputs=[input_image, prompt_description, prompt_input],
105
- outputs=[output_image, output_side],
106
- fn=generate,
107
- cache_examples="lazy"
108
- )
109
-
110
- with gr.Row():
111
- gr.Markdown("""
112
- ### Instructions:
113
- 1. Upload a logo image (preferably square)
114
- 2. Describe where you'd like to see the logo applied
115
- 3. Click 'Generate Application' and wait for the result
116
-
117
- Note: The generation process might take a few moments.
118
- """)
119
-
120
- with gr.Tab("🧨 diffusers implementation"):
121
- gr.Markdown("The way this works is combining the [IC LoRA](https://github.com/ali-vilab/In-Context-LoRA) with image-to-image + inpainting. Where the image on the left (the logo) is uploaded by the user, and the image on the right is masked and applied on the product by the LoRA. Based on the [ComfyUI workflow by WizardWhitebeard/klinter](https://civitai.com/articles/8779). Below is a diffusers implementation of the idea")
122
-
123
- gr.Code(language="python", value="""# Support functions
124
- def square_center_crop(img, target_size=768):
125
- if img.mode in ('RGBA', 'P'):
126
- img = img.convert('RGB')
127
-
128
- width, height = img.size
129
- crop_size = min(width, height)
130
-
131
- left = (width - crop_size) // 2
132
- top = (height - crop_size) // 2
133
- right = left + crop_size
134
- bottom = top + crop_size
135
-
136
- img_cropped = img.crop((left, top, right, bottom))
137
- return img_cropped.resize((target_size, target_size), Image.Resampling.LANCZOS)
138
-
139
- def duplicate_horizontally(img):
140
- width, height = img.size
141
- if width != height:
142
- raise ValueError(f"Input image must be square, got {width}x{height}")
143
-
144
- new_image = Image.new('RGB', (width * 2, height))
145
- new_image.paste(img, (0, 0))
146
- new_image.paste(img, (width, 0))
147
- return new_image"""
148
- )
149
-
150
- gr.Code(language="python", value="""import torch
151
- from diffusers import FluxInpaintPipeline
152
- from PIL import Image
153
-
154
- pipe = FluxInpaintPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
155
- pipe.to("cuda")
156
- pipe.load_lora_weights("ali-vilab/In-Context-LoRA", weight_name="visual-identity-design.safetensors")
157
-
158
- mask = load_image("mask_square.png")
159
- image = load_image("the_logo.png")
160
- cropped_image = square_center_crop(image) #crop the image you upload to square
161
- logo_dupli = duplicate_horizontally(cropped_image) #duplicate it so the right side can be masked
162
-
163
- prompt_structure = "The two-panel image showcases the logo of a brand, [LEFT] the left panel is showing the logo [RIGHT] the right panel has this logo applied to "
164
- prompt = prompt_structure + "an coconut, engraved logo on a green coconut"
165
- out = pipe(
166
- prompt=prompt,
167
- image=logo_dupli,
168
- mask_image=mask,
169
- guidance_scale=6,
170
- height=768,
171
- width=1536,
172
- num_inference_steps=28,
173
- max_sequence_length=256,
174
- strength=1
175
- ).images[0]"""
176
- )
177
-
178
- # Set up the click event
179
- generate_btn.click(
180
- fn=generate,
181
- inputs=[input_image, prompt_description, prompt_input],
182
- outputs=[output_image, output_side]
183
  )
184
 
185
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import gradio as gr
3
  import torch
 
 
 
4
 
 
5
 
6
+ title = "????AI ChatBot"
7
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
8
+ examples = [["How are you?"]]
 
 
 
 
 
 
 
9
 
 
 
 
10
 
11
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
12
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
13
 
 
 
 
 
14
 
15
+ def predict(input, history=[]):
16
+ # tokenize the new input sentence
17
+ new_user_input_ids = tokenizer.encode(
18
+ input + tokenizer.eos_token, return_tensors="pt"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
 
21
+ # append the new user input tokens to the chat history
22
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
23
+
24
+ # generate a response
25
+ history = model.generate(
26
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
27
+ ).tolist()
28
+
29
+ # convert the tokens to text, and then split the responses into lines
30
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
31
+ # print('decoded_response-->>'+str(response))
32
+ response = [
33
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
34
+ ] # convert to tuples of list
35
+ # print('response-->>'+str(response))
36
+ return response, history
37
+
38
+
39
+ gr.Interface(
40
+ fn=predict,
41
+ title=title,
42
+ description=description,
43
+ examples=examples,
44
+ inputs=["text", "state"],
45
+ outputs=["chatbot", "state"],
46
+ theme="finlaymacklon/boxy_violet",
47
+ ).launch()