K00B404 commited on
Commit
2385a9b
·
verified ·
1 Parent(s): 48fa5cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -96
app.py CHANGED
@@ -1,146 +1,144 @@
1
  import gradio as gr
 
2
  import numpy as np
3
  import random
4
- from diffusers import DiffusionPipeline
5
  import torch
 
 
6
 
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
8
 
 
9
  if torch.cuda.is_available():
10
  torch.cuda.max_memory_allocated(device=device)
11
  pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
  pipe.enable_xformers_memory_efficient_attention()
13
- pipe = pipe.to(device)
14
- else:
15
  pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
- pipe = pipe.to(device)
17
 
18
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
 
 
20
 
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
 
 
 
 
 
 
 
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  if randomize_seed:
24
  seed = random.randint(0, MAX_SEED)
25
-
26
  generator = torch.Generator().manual_seed(seed)
27
 
28
  image = pipe(
29
- prompt = prompt,
30
- negative_prompt = negative_prompt,
31
- guidance_scale = guidance_scale,
32
- num_inference_steps = num_inference_steps,
33
- width = width,
34
- height = height,
35
- generator = generator
36
- ).images[0]
37
 
38
  return image
39
 
40
- examples = [
41
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
- "An astronaut riding a green horse",
43
- "A delicious ceviche cheesecake slice",
44
- ]
45
 
46
- css="""
47
  #col-container {
48
  margin: 0 auto;
49
  max-width: 520px;
50
  }
51
  """
52
 
53
- if torch.cuda.is_available():
54
- power_device = "GPU"
55
- else:
56
- power_device = "CPU"
57
 
58
  with gr.Blocks(css=css) as demo:
59
-
60
  with gr.Column(elem_id="col-container"):
61
  gr.Markdown(f"""
62
  # Text-to-Image Gradio Template
63
  Currently running on {power_device}.
64
  """)
65
-
66
  with gr.Row():
67
-
68
- prompt = gr.Text(
69
- label="Prompt",
70
- show_label=False,
71
- max_lines=1,
72
- placeholder="Enter your prompt",
73
- container=False,
74
- )
75
-
76
- run_button = gr.Button("Run", scale=0)
77
 
78
  result = gr.Image(label="Result", show_label=False)
79
 
80
  with gr.Accordion("Advanced Settings", open=False):
81
-
82
- negative_prompt = gr.Text(
83
- label="Negative prompt",
84
- max_lines=1,
85
- placeholder="Enter a negative prompt",
86
- visible=False,
87
- )
88
-
89
- seed = gr.Slider(
90
- label="Seed",
91
- minimum=0,
92
- maximum=MAX_SEED,
93
- step=1,
94
- value=0,
95
- )
96
-
97
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
 
99
  with gr.Row():
100
-
101
- width = gr.Slider(
102
- label="Width",
103
- minimum=256,
104
- maximum=MAX_IMAGE_SIZE,
105
- step=32,
106
- value=512,
107
- )
108
-
109
- height = gr.Slider(
110
- label="Height",
111
- minimum=256,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=32,
114
- value=512,
115
- )
116
 
117
  with gr.Row():
118
-
119
- guidance_scale = gr.Slider(
120
- label="Guidance scale",
121
- minimum=0.0,
122
- maximum=10.0,
123
- step=0.1,
124
- value=0.0,
125
- )
126
-
127
- num_inference_steps = gr.Slider(
128
- label="Number of inference steps",
129
- minimum=1,
130
- maximum=12,
131
- step=1,
132
- value=2,
133
- )
134
-
135
- gr.Examples(
136
- examples = examples,
137
- inputs = [prompt]
138
  )
139
 
140
- run_button.click(
141
- fn = infer,
142
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
- outputs = [result]
144
- )
 
 
 
 
 
 
 
 
 
145
 
146
- demo.queue().launch()
 
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
  import numpy as np
4
  import random
 
5
  import torch
6
+ from transformers import pipeline
7
+ from diffusers import DiffusionPipeline
8
 
9
+ # Initialize the device for running the diffusion model
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ MAX_SEED = np.iinfo(np.int32).max
12
+ MAX_IMAGE_SIZE = 1024
13
 
14
+ # Set up the diffusion pipeline
15
  if torch.cuda.is_available():
16
  torch.cuda.max_memory_allocated(device=device)
17
  pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
18
  pipe.enable_xformers_memory_efficient_attention()
19
+ else:
 
20
  pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
21
+ pipe = pipe.to(device)
22
 
23
+ class ImagePromptGenerator:
24
+ def __init__(self, model_name="gpt2"):
25
+ # Initialize the text generation pipeline
26
+ self.generator = pipeline("text-generation", model=model_name, use_auth_token=True)
27
 
28
+ def generate_short_prompts(self, theme, num_prompts=5):
29
+ # Generate short prompts based on the theme
30
+ prompts = self.generator(f"{theme} concept", max_length=50, num_return_sequences=num_prompts)
31
+ short_prompts = [prompt['generated_text'].strip() for prompt in prompts]
32
+ return short_prompts
33
+
34
+ def enhance_prompt(self, short_prompt):
35
+ # Enhance the short prompt into a more detailed long prompt
36
+ long_prompt = self.generator(f"Elaborate: {short_prompt}", max_length=100, num_return_sequences=1)
37
+ return long_prompt[0]['generated_text'].strip()
38
 
39
+ def generate_prompts_csv(self, theme):
40
+ # Generate short prompts and enhance them
41
+ short_prompts = self.generate_short_prompts(theme)
42
+ long_prompts = [self.enhance_prompt(sp) for sp in short_prompts]
43
+ # Create a DataFrame
44
+ df = pd.DataFrame({"short": short_prompts, "long": long_prompts})
45
+ return df.to_csv(index=False)
46
+
47
+ def generate_and_save_prompts(theme):
48
+ generator = ImagePromptGenerator()
49
+ csv_content = generator.generate_prompts_csv(theme)
50
+ return csv_content
51
+
52
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
53
  if randomize_seed:
54
  seed = random.randint(0, MAX_SEED)
 
55
  generator = torch.Generator().manual_seed(seed)
56
 
57
  image = pipe(
58
+ prompt=prompt,
59
+ negative_prompt=negative_prompt,
60
+ guidance_scale=guidance_scale,
61
+ num_inference_steps=num_inference_steps,
62
+ width=width,
63
+ height=height,
64
+ generator=generator
65
+ ).images[0]
66
 
67
  return image
68
 
69
+ def gradio_interface(theme):
70
+ # Generate image prompts based on theme
71
+ csv_content = generate_and_save_prompts(theme)
72
+ return gr.File(content=csv_content, file_name=f"{theme}_image_prompts.csv")
 
73
 
74
+ css = """
75
  #col-container {
76
  margin: 0 auto;
77
  max-width: 520px;
78
  }
79
  """
80
 
81
+ # Determine the computational power available
82
+ power_device = "GPU" if torch.cuda.is_available() else "CPU"
 
 
83
 
84
  with gr.Blocks(css=css) as demo:
 
85
  with gr.Column(elem_id="col-container"):
86
  gr.Markdown(f"""
87
  # Text-to-Image Gradio Template
88
  Currently running on {power_device}.
89
  """)
90
+
91
  with gr.Row():
92
+ theme = gr.Textbox(label="Theme for Image Generation", placeholder="Enter a theme to generate prompts")
93
+ prompt = gr.Textbox(label="Prompt for Image Generation", placeholder="Enter your prompt here or select from generated prompts", show_label=False)
94
+ generate_prompts_button = gr.Button("Generate Prompts")
95
+
96
+ with gr.Row():
97
+ run_button = gr.Button("Run")
 
 
 
 
98
 
99
  result = gr.Image(label="Result", show_label=False)
100
 
101
  with gr.Accordion("Advanced Settings", open=False):
102
+ negative_prompt = gr.Textbox(label="Negative prompt", placeholder="Enter a negative prompt")
103
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
105
 
106
  with gr.Row():
107
+ width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
108
+ height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  with gr.Row():
111
+ guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=7.5)
112
+ num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=250, step=1, value=50)
113
+
114
+ generate_prompts_button.click(
115
+ fn=gradio_interface,
116
+ inputs=[theme],
117
+ outputs=[gr.File(label="Download Generated Prompts CSV")]
118
+ )
119
+
120
+ run_button.click(
121
+ fn=infer,
122
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
123
+ outputs=[result]
 
 
 
 
 
 
 
124
  )
125
 
126
+ demo.launch()
127
+ '''
128
+ Explanation:
129
+
130
+ Class ImagePromptGenerator: This class now includes methods to generate short prompts, enhance them, and output a CSV.
131
+
132
+ generate_and_save_prompts Function: This function generates a CSV of prompts based on the theme.
133
+
134
+ infer Function: This function generates an image based on the provided parameters using the diffusion model.
135
+
136
+ Gradio Interface: The interface now includes:
137
+ A textbox to input the theme for generating prompts.
138
+ A button to generate prompts based on the theme.
139
+ The original image generation interface with advanced settings.
140
 
141
+ Button Actions:
142
+ Generate Prompts Button: Generates a list of prompts as a downloadable CSV file.
143
+ Run Button: Generates an image based on the provided prompt and settings.
144
+ '''