AlekseyCalvin commited on
Commit
9247cd4
·
verified ·
1 Parent(s): f3a733b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -1
app.py CHANGED
@@ -1,3 +1,128 @@
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- gr.load("models/AlekseyCalvin/RCA_Agitprop_Manufactory").launch()
 
1
+ import spaces # Import this first to avoid CUDA initialization issues
2
+ import os
3
  import gradio as gr
4
+ import numpy as np
5
+ import gradio as gr
6
+ import torch
7
+ import random
8
+ import time
9
+ from PIL import Image
10
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel
11
+ from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
12
+
13
+ dtype = torch.bfloat16
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+
16
+ # Use the 'waffles' environment variable as the access token
17
+ hf_token = os.getenv('waffles')
18
+
19
+ # Ensure the token is loaded correctly
20
+ if not hf_token:
21
+ raise ValueError("Hugging Face API token not found. Please set the 'waffles' environment variable.")
22
+
23
+
24
+ pipe = DiffusionPipeline.from_pretrained("models/AlekseyCalvin/RCA_Agitprop_Manufactory", torch_dtype=dtype, token=hf_token).to(device)
25
+
26
+ @spaces.GPU()
27
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
28
+ if randomize_seed:
29
+ seed = random.randint(0, MAX_SEED)
30
+ generator = torch.Generator().manual_seed(seed)
31
+ image = pipe(
32
+ prompt = prompt,
33
+ width = width,
34
+ height = height,
35
+ num_inference_steps = num_inference_steps,
36
+ generator = generator,
37
+ guidance_scale=0.0
38
+ ).images[0]
39
+ return image, seed
40
+
41
+
42
+ examples = [
43
+ "RCA style communist party poster with the words Ready for REVOLUTION? in large black consistent constructivist font alongside a red Soviet hammer and a red Soviet sickle over the background of planet earth, over the North American continent",
44
+ ]
45
+
46
+ css="""
47
+ #col-container {
48
+ margin: 0 auto;
49
+ max-width: 520px;
50
+ }
51
+ """
52
+
53
+ with gr.Blocks(css=css) as demo:
54
+
55
+ with gr.Column(elem_id="col-container"):
56
+ gr.Markdown(f"""# RCA Agitprop Manufactory""")
57
+
58
+ with gr.Row():
59
+
60
+ prompt = gr.Text(
61
+ label="Prompt",
62
+ show_label=False,
63
+ max_lines=1,
64
+ placeholder="Enter your prompt",
65
+ container=False,
66
+ )
67
+
68
+ run_button = gr.Button("Run", scale=0)
69
+
70
+ result = gr.Image(label="Result", show_label=False)
71
+
72
+ with gr.Accordion("Advanced Settings", open=False):
73
+
74
+ seed = gr.Slider(
75
+ label="Seed",
76
+ minimum=0,
77
+ maximum=MAX_SEED,
78
+ step=1,
79
+ value=0,
80
+ )
81
+
82
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
83
+
84
+ with gr.Row():
85
+
86
+ width = gr.Slider(
87
+ label="Width",
88
+ minimum=256,
89
+ maximum=MAX_IMAGE_SIZE,
90
+ step=32,
91
+ value=1024,
92
+ )
93
+
94
+ height = gr.Slider(
95
+ label="Height",
96
+ minimum=256,
97
+ maximum=MAX_IMAGE_SIZE,
98
+ step=32,
99
+ value=1024,
100
+ )
101
+
102
+ with gr.Row():
103
+
104
+
105
+ num_inference_steps = gr.Slider(
106
+ label="Number of inference steps",
107
+ minimum=1,
108
+ maximum=50,
109
+ step=1,
110
+ value=4,
111
+ )
112
+
113
+ gr.Examples(
114
+ examples = examples,
115
+ fn = infer,
116
+ inputs = [prompt],
117
+ outputs = [result, seed],
118
+ cache_examples="lazy"
119
+ )
120
+
121
+ gr.on(
122
+ triggers=[run_button.click, prompt.submit],
123
+ fn = infer,
124
+ inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
125
+ outputs = [result, seed]
126
+ )
127
 
128
+ demo.launch()