lazarusrolando commited on
Commit
15db5ed
·
verified ·
1 Parent(s): 6eb5c5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -124
app.py CHANGED
@@ -1,159 +1,132 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
-
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
  import torch
 
 
 
8
 
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "lazarus19/AuroraImageGen" # Replace to the model you would like to use
11
 
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
 
17
- pipe = DiffusionPipeline.from_pretrained(
18
- model_repo_id,
19
- torch_dtype=torch.float16
 
 
20
  )
21
 
22
- pipe.enable_model_cpu_offload()
23
- pipe = pipe.to(device)
24
-
25
- MAX_SEED = np.iinfo(np.int32).max
26
- MAX_IMAGE_SIZE = 1024
27
-
28
-
29
- # @spaces.GPU #[uncomment to use ZeroGPU]
30
- def infer(
31
  prompt,
32
- negative_prompt,
33
- seed,
34
- randomize_seed,
35
- width,
36
- height,
37
- guidance_scale,
38
- num_inference_steps,
39
- progress=gr.Progress(track_tqdm=True),
40
  ):
41
- if randomize_seed:
42
- seed = random.randint(0, MAX_SEED)
43
-
44
- generator = torch.Generator(device=device).manual_seed(seed)
45
 
46
- image = pipe(
47
- prompt=prompt,
48
- negative_prompt=negative_prompt,
49
- guidance_scale=guidance_scale,
50
- num_inference_steps=num_inference_steps,
51
- width=width,
52
- height=height,
53
- generator=generator,
54
- ).images[0]
55
 
56
- return image, seed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
 
58
 
59
  examples = [
60
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
61
- "An astronaut riding a green horse",
62
- "A delicious ceviche cheesecake slice",
63
  ]
64
 
65
  css = """
66
  #col-container {
67
  margin: 0 auto;
68
- max-width: 640px;
69
  }
70
  """
71
 
72
  with gr.Blocks(css=css) as demo:
73
  with gr.Column(elem_id="col-container"):
74
- gr.Markdown(" # Text-to-Image Gradio Template")
75
-
76
- with gr.Row():
77
- prompt = gr.Text(
78
- label="Prompt",
79
- show_label=False,
80
- max_lines=1,
81
- placeholder="Enter your prompt",
82
- container=False,
83
- )
84
 
85
- run_button = gr.Button("Run", scale=0, variant="primary")
86
 
87
- result = gr.Image(label="Result", show_label=False)
 
 
 
 
 
 
 
 
 
88
 
89
  with gr.Accordion("Advanced Settings", open=False):
90
- negative_prompt = gr.Text(
91
- label="Negative prompt",
92
- max_lines=1,
93
- placeholder="Enter a negative prompt",
94
- visible=False,
 
 
95
  )
96
 
97
- seed = gr.Slider(
98
- label="Seed",
99
- minimum=0,
100
- maximum=MAX_SEED,
101
- step=1,
102
- value=0,
103
  )
104
 
105
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
106
-
107
- with gr.Row():
108
- width = gr.Slider(
109
- label="Width",
110
- minimum=256,
111
- maximum=MAX_IMAGE_SIZE,
112
- step=32,
113
- value=512, # Replace with defaults that work for your model
114
- )
115
-
116
- height = gr.Slider(
117
- label="Height",
118
- minimum=256,
119
- maximum=MAX_IMAGE_SIZE,
120
- step=32,
121
- value=1024, # Replace with defaults that work for your model
122
- )
123
-
124
- with gr.Row():
125
- guidance_scale = gr.Slider(
126
- label="Guidance scale",
127
- minimum=0.0,
128
- maximum=10.0,
129
- step=0.1,
130
- value=0.0, # Replace with defaults that work for your model
131
- )
132
-
133
- num_inference_steps = gr.Slider(
134
- label="Number of inference steps",
135
- minimum=1,
136
- maximum=50,
137
- step=1,
138
- value=2, # Replace with defaults that work for your model
139
- )
140
-
141
- gr.Examples(examples=examples, inputs=[prompt])
142
- gr.on(
143
- triggers=[run_button.click, prompt.submit],
144
- fn=infer,
145
- inputs=[
146
- prompt,
147
- negative_prompt,
148
- seed,
149
- randomize_seed,
150
- width,
151
- height,
152
- guidance_scale,
153
- num_inference_steps,
154
- ],
155
- outputs=[result, seed],
156
- )
157
 
158
  if __name__ == "__main__":
159
- demo.launch()
 
1
  import gradio as gr
 
 
 
 
 
2
  import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+
5
+ MODEL_ID = "lazarus19/AuroraImageGen"
6
 
7
+ # Device setup
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
10
 
11
+ # Load tokenizer
12
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
 
 
13
 
14
+ # Load model
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ MODEL_ID,
17
+ torch_dtype=torch_dtype,
18
+ device_map="auto"
19
  )
20
 
21
+ # Generate function
22
+ def generate(
 
 
 
 
 
 
 
23
  prompt,
24
+ max_new_tokens,
25
+ temperature,
26
+ top_p,
 
 
 
 
 
27
  ):
28
+ if not prompt.strip():
29
+ return "Please enter a prompt."
 
 
30
 
31
+ inputs = tokenizer(
32
+ prompt,
33
+ return_tensors="pt"
34
+ )
 
 
 
 
 
35
 
36
+ inputs = {k: v.to(model.device) for k, v in inputs.items()}
37
+
38
+ with torch.no_grad():
39
+ outputs = model.generate(
40
+ **inputs,
41
+ max_new_tokens=max_new_tokens,
42
+ temperature=temperature,
43
+ top_p=top_p,
44
+ do_sample=True,
45
+ pad_token_id=tokenizer.eos_token_id,
46
+ )
47
+
48
+ response = tokenizer.decode(
49
+ outputs[0],
50
+ skip_special_tokens=True
51
+ )
52
 
53
+ return response
54
 
55
  examples = [
56
+ "Write a short story about a robot explorer.",
57
+ "Explain quantum computing in simple terms.",
58
+ "Create a fantasy character profile.",
59
  ]
60
 
61
  css = """
62
  #col-container {
63
  margin: 0 auto;
64
+ max-width: 900px;
65
  }
66
  """
67
 
68
  with gr.Blocks(css=css) as demo:
69
  with gr.Column(elem_id="col-container"):
 
 
 
 
 
 
 
 
 
 
70
 
71
+ gr.Markdown("# AuroraImageGen Chat")
72
 
73
+ prompt = gr.Textbox(
74
+ label="Prompt",
75
+ lines=6,
76
+ placeholder="Enter your prompt..."
77
+ )
78
+
79
+ output = gr.Textbox(
80
+ label="Response",
81
+ lines=20
82
+ )
83
 
84
  with gr.Accordion("Advanced Settings", open=False):
85
+
86
+ max_new_tokens = gr.Slider(
87
+ minimum=32,
88
+ maximum=2048,
89
+ value=512,
90
+ step=32,
91
+ label="Max New Tokens"
92
  )
93
 
94
+ temperature = gr.Slider(
95
+ minimum=0.1,
96
+ maximum=2.0,
97
+ value=0.7,
98
+ step=0.1,
99
+ label="Temperature"
100
  )
101
 
102
+ top_p = gr.Slider(
103
+ minimum=0.1,
104
+ maximum=1.0,
105
+ value=0.9,
106
+ step=0.05,
107
+ label="Top-P"
108
+ )
109
+
110
+ run_button = gr.Button(
111
+ "Generate",
112
+ variant="primary"
113
+ )
114
+
115
+ gr.Examples(
116
+ examples=examples,
117
+ inputs=[prompt]
118
+ )
119
+
120
+ run_button.click(
121
+ fn=generate,
122
+ inputs=[
123
+ prompt,
124
+ max_new_tokens,
125
+ temperature,
126
+ top_p,
127
+ ],
128
+ outputs=output,
129
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  if __name__ == "__main__":
132
+ demo.launch()