simran681 commited on
Commit
778bd29
1 Parent(s): 8cda391

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -141
app.py CHANGED
@@ -1,148 +1,14 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
  import spaces
5
- from diffusers import DiffusionPipeline
6
  import torch
7
 
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
-
10
- if torch.cuda.is_available():
11
- torch.cuda.max_memory_allocated(device=device)
12
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
13
- pipe.enable_xformers_memory_efficient_attention()
14
- pipe = pipe.to(device)
15
- else:
16
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
17
- pipe = pipe.to(device)
18
-
19
- MAX_SEED = np.iinfo(np.int32).max
20
- MAX_IMAGE_SIZE = 1024
21
 
22
  @spaces.GPU
23
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
24
-
25
- if randomize_seed:
26
- seed = random.randint(0, MAX_SEED)
27
-
28
- generator = torch.Generator().manual_seed(seed)
29
-
30
- image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
- ).images[0]
39
-
40
- return image
41
-
42
- examples = [
43
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
44
- "An astronaut riding a green horse",
45
- "A delicious ceviche cheesecake slice",
46
- ]
47
-
48
- css="""
49
- #col-container {
50
- margin: 0 auto;
51
- max-width: 520px;
52
- }
53
- """
54
-
55
- if torch.cuda.is_available():
56
- power_device = "GPU"
57
- else:
58
- power_device = "CPU"
59
-
60
- with gr.Blocks(css=css) as demo:
61
-
62
- with gr.Column(elem_id="col-container"):
63
- gr.Markdown(f"""
64
- # Text-to-Image Gradio Template
65
- Currently running on {power_device}.
66
- """)
67
-
68
- with gr.Row():
69
-
70
- prompt = gr.Text(
71
- label="Prompt",
72
- show_label=False,
73
- max_lines=1,
74
- placeholder="Enter your prompt",
75
- container=False,
76
- )
77
-
78
- run_button = gr.Button("Run", scale=0)
79
-
80
- result = gr.Image(label="Result", show_label=False)
81
-
82
- with gr.Accordion("Advanced Settings", open=False):
83
-
84
- negative_prompt = gr.Text(
85
- label="Negative prompt",
86
- max_lines=1,
87
- placeholder="Enter a negative prompt",
88
- visible=False,
89
- )
90
-
91
- seed = gr.Slider(
92
- label="Seed",
93
- minimum=0,
94
- maximum=MAX_SEED,
95
- step=1,
96
- value=0,
97
- )
98
-
99
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
100
-
101
- with gr.Row():
102
-
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=512,
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=512,
117
- )
118
-
119
- with gr.Row():
120
-
121
- guidance_scale = gr.Slider(
122
- label="Guidance scale",
123
- minimum=0.0,
124
- maximum=10.0,
125
- step=0.1,
126
- value=0.0,
127
- )
128
-
129
- num_inference_steps = gr.Slider(
130
- label="Number of inference steps",
131
- minimum=1,
132
- maximum=12,
133
- step=1,
134
- value=2,
135
- )
136
-
137
- gr.Examples(
138
- examples = examples,
139
- inputs = [prompt]
140
- )
141
-
142
- run_button.click(
143
- fn = infer,
144
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
145
- outputs = [result]
146
- )
147
 
148
- demo.queue().launch()
 
 
1
  import gradio as gr
 
 
2
  import spaces
 
3
  import torch
4
 
5
+ zero = torch.Tensor([0]).cuda()
6
+ print(zero.device) # <-- 'cpu' 🤔
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  @spaces.GPU
9
+ def greet(n):
10
+ print(zero.device) # <-- 'cuda:0' 🤗
11
+ return f"Hello {zero + n} Tensor"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
14
+ demo.launch()