Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
import spaces
|
4 |
+
|
5 |
+
def find_cuda():
|
6 |
+
# Check if CUDA_HOME or CUDA_PATH environment variables are set
|
7 |
+
cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH')
|
8 |
+
|
9 |
+
if cuda_home and os.path.exists(cuda_home):
|
10 |
+
return cuda_home
|
11 |
+
|
12 |
+
# Search for the nvcc executable in the system's PATH
|
13 |
+
nvcc_path = shutil.which('nvcc')
|
14 |
+
|
15 |
+
if nvcc_path:
|
16 |
+
# Remove the 'bin/nvcc' part to get the CUDA installation path
|
17 |
+
cuda_path = os.path.dirname(os.path.dirname(nvcc_path))
|
18 |
+
return cuda_path
|
19 |
+
|
20 |
+
return None
|
21 |
+
|
22 |
+
cuda_path = find_cuda()
|
23 |
+
|
24 |
+
if cuda_path:
|
25 |
+
print(f"CUDA installation found at: {cuda_path}")
|
26 |
+
else:
|
27 |
+
print("CUDA installation not found")
|
28 |
+
|
29 |
+
# check if cuda is available
|
30 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
31 |
+
|
32 |
+
# load the pipeline/model
|
33 |
+
pipeline = DiffusionPipeline.from_pretrained("jadechoghari/mar", trust_remote_code=True, custom_pipeline="jadechoghari/mar")
|
34 |
+
|
35 |
+
# function that generates images
|
36 |
+
@spaces.GPU
|
37 |
+
def generate_image(seed, num_ar_steps, class_labels, cfg_scale, cfg_schedule):
|
38 |
+
generated_image = pipeline(
|
39 |
+
model_type="mar_huge", # using mar_huge
|
40 |
+
seed=seed,
|
41 |
+
num_ar_steps=num_ar_steps,
|
42 |
+
class_labels=[int(label.strip()) for label in class_labels.split(',')],
|
43 |
+
cfg_scale=cfg_scale,
|
44 |
+
cfg_schedule=cfg_schedule,
|
45 |
+
output_dir="./images"
|
46 |
+
)
|
47 |
+
return generated_image
|
48 |
+
|
49 |
+
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
gr.Markdown("""
|
52 |
+
# MAR Image Generation Demo 🚀
|
53 |
+
|
54 |
+
Welcome to the demo for **MAR** (Masked Autoregressive Model), a novel approach to image generation that eliminates the need for vector quantization. MAR uses a diffusion process to generate images in a continuous-valued space, resulting in faster, more efficient, and higher-quality outputs.
|
55 |
+
|
56 |
+
Simply adjust the parameters below to create your custom images in real-time.
|
57 |
+
|
58 |
+
Make sure to provide valid **ImageNet class labels** to see the translation of text to image. For a complete list of ImageNet classes, check out [this reference](https://deeplearning.cms.waikato.ac.nz/user-guide/class-maps/IMAGENET/).
|
59 |
+
|
60 |
+
For more details, visit the [GitHub repository](https://github.com/LTH14/mar).
|
61 |
+
""")
|
62 |
+
|
63 |
+
seed = gr.Number(value=42, label="Seed")
|
64 |
+
num_ar_steps = gr.Slider(minimum=1, maximum=256, value=64, label="Number of AR Steps")
|
65 |
+
class_labels = gr.Textbox(value="207, 360, 388", label="Class Labels (comma-separated ImageNet labels)")
|
66 |
+
cfg_scale = gr.Slider(minimum=1, maximum=10, value=4, label="CFG Scale")
|
67 |
+
cfg_schedule = gr.Dropdown(choices=["constant", "linear"], label="CFG Schedule", value="constant")
|
68 |
+
|
69 |
+
image_output = gr.Image(label="Generated Image")
|
70 |
+
|
71 |
+
generate_button = gr.Button("Generate Image")
|
72 |
+
|
73 |
+
# we link the button to the function and display the output
|
74 |
+
generate_button.click(generate_image, inputs=[seed, num_ar_steps, class_labels, cfg_scale, cfg_schedule], outputs=image_output)
|
75 |
+
|
76 |
+
demo.launch()
|