iitolstykh commited on
Commit
1199a4a
·
1 Parent(s): 5fe3ae7

Add application file

Browse files
Files changed (3) hide show
  1. .gitignore +4 -0
  2. app.py +133 -0
  3. requirements.txt +8 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio_cached_examples/
2
+ flagged/
3
+ .idea/
4
+ .DS_Store
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shlex
3
+ import subprocess
4
+
5
+ if os.getenv('SYSTEM', "") == 'spaces' and not os.getenv('USE_PRIVATE_PACKAGE', False):
6
+ GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
7
+ GITHUB_USER = os.getenv('GITHUB_USER')
8
+ git_repo = f"https://{GITHUB_TOKEN}@github.com/{GITHUB_USER}/VIBE.git"
9
+ subprocess.call(shlex.split(f'pip install git+{git_repo}'))
10
+
11
+ from functools import partial
12
+ from gradio.components import Image, Textbox
13
+ import gradio as gr
14
+
15
+ from PIL import Image
16
+ from huggingface_hub import snapshot_download
17
+ import os
18
+ import random
19
+ import torch
20
+ import numpy as np
21
+ import pathlib
22
+
23
+ from vibe.editor import ImageEditor
24
+
25
+ MAX_SEED = np.iinfo(np.int32).max
26
+
27
+
28
+ def load_pipeline():
29
+ HF_TOKEN = os.getenv('HF_TOKEN')
30
+ model_path = snapshot_download(
31
+ repo_id="iitolstykh/VIBE-Image-Edit",
32
+ repo_type="model",
33
+ token=HF_TOKEN,
34
+ )
35
+
36
+ # Load model
37
+ editor_pipeline = ImageEditor(
38
+ checkpoint_path=model_path,
39
+ image_guidance_scale=1.2,
40
+ guidance_scale=4.5,
41
+ num_inference_steps=20,
42
+ device="cuda:0",
43
+ )
44
+
45
+ return editor_pipeline
46
+
47
+ def set_env(seed=0):
48
+ torch.manual_seed(seed)
49
+ torch.set_grad_enabled(False)
50
+
51
+
52
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
53
+ if randomize_seed:
54
+ seed = random.randint(0, MAX_SEED)
55
+ return seed
56
+
57
+
58
+ @torch.inference_mode()
59
+ def generate_img(
60
+ pipeline: ImageEditor,
61
+ np_image: np.ndarray, # rgb image
62
+ edit_prompt: str,
63
+ sample_steps,
64
+ scale,
65
+ image_guidance_scale,
66
+ seed=0,
67
+ randomize_seed=False,
68
+ ):
69
+ # color_converted_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
70
+
71
+ pil_image = Image.fromarray(np_image)
72
+
73
+ seed = int(randomize_seed_fn(seed, randomize_seed))
74
+ set_env(seed)
75
+
76
+ edited_image = pipeline.generate_edited_image(
77
+ instruction=edit_prompt,
78
+ conditioning_image=pil_image,
79
+ num_images_per_prompt=1,
80
+ num_inference_steps=sample_steps,
81
+ guidance_scale=scale,
82
+ image_guidance_scale=image_guidance_scale,
83
+ )[0]
84
+ return edited_image
85
+
86
+
87
+ if __name__ == "__main__":
88
+
89
+ DESCRIPTION = f"""DEMO for VIBE-Image-Edit model."""
90
+
91
+ image_dir = pathlib.Path('images')
92
+ examples = [[path.as_posix(), "let this case swim in the river", 20, 4.5, 1.2, 42] for path in sorted(image_dir.glob('*.png'))]
93
+
94
+ editor_pipeline = load_pipeline()
95
+ generate_fn = partial(
96
+ generate_img,
97
+ editor_pipeline=editor_pipeline
98
+ )
99
+
100
+ demo = gr.Interface(
101
+ fn=generate_fn,
102
+ inputs=[
103
+ gr.Image(label="Input", type="numpy"),
104
+ Textbox(label="Prompt", placeholder="Please enter your prompt. \n"),
105
+ gr.Slider(label="Sample Steps", minimum=1, maximum=100, value=20, step=1),
106
+ gr.Slider(
107
+ label="Guidance Scale", minimum=0.1, maximum=30.0, value=4.5, step=0.1
108
+ ),
109
+ gr.Slider(
110
+ label="Image Guidance Scale",
111
+ minimum=0.1,
112
+ maximum=30.0,
113
+ value=1.2,
114
+ step=0.1,
115
+ ),
116
+ gr.Slider(
117
+ label="Seed",
118
+ minimum=0,
119
+ maximum=MAX_SEED,
120
+ step=1,
121
+ value=42,
122
+ ),
123
+ gr.Checkbox(label="Randomize seed", value=False),
124
+ ],
125
+ outputs=[
126
+ Image(type="numpy", label="Img"),
127
+ ],
128
+ title="",
129
+ description=DESCRIPTION,
130
+ examples=examples,
131
+ )
132
+
133
+ demo.queue(max_size=15).launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ transformers==4.57.1
2
+ torchvision==0.21.0
3
+ torch==2.6.0
4
+ diffusers==0.33.1
5
+ loguru==0.7.3
6
+ gradio==6.2.0
7
+ huggingface_hub==0.34.1
8
+ # git+https://github.com/ai-forever/VIBE