panelforge commited on
Commit
7819529
·
verified ·
1 Parent(s): 6d482fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -24
app.py CHANGED
@@ -20,12 +20,35 @@ pipe = pipe.to(device)
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  @spaces.GPU # [uncomment to use ZeroGPU]
24
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, tag_selection, progress=gr.Progress(track_tqdm=True)):
25
 
26
- # Combine selected tags with the user input prompt
27
- tags_text = ', '.join(tag_selection)
28
- final_prompt = f'score_9, score_8_up, score_7_up,source_anime, {tags_text}, {prompt}'
 
 
 
 
 
29
 
30
  if randomize_seed:
31
  seed = random.randint(0, MAX_SEED)
@@ -57,12 +80,6 @@ css = """
57
  }
58
  """
59
 
60
- # Define a list of example tags
61
- tag_options = [
62
- "fantasy", "sci-fi", "realistic", "cyberpunk", "noir", "surreal",
63
- "colorful", "detailed", "high resolution", "anime style"
64
- ]
65
-
66
  with gr.Blocks(css=css) as demo:
67
 
68
  with gr.Column(elem_id="col-container"):
@@ -70,23 +87,29 @@ with gr.Blocks(css=css) as demo:
70
  # Text-to-Image Gradio Template
71
  """)
72
 
73
- with gr.Row():
74
- prompt = gr.Text(
75
- label="Prompt",
76
- show_label=False,
77
- max_lines=1,
78
- placeholder="Enter your prompt",
79
- container=False,
80
- )
 
 
 
 
81
 
82
- run_button = gr.Button("Run", scale=0)
 
 
 
 
 
83
 
 
84
  result = gr.Image(label="Result", show_label=False)
85
 
86
- with gr.Row():
87
- # Checkbox group for selectable tags
88
- tag_selection = gr.CheckboxGroup(choices=tag_options, label="Select Tags")
89
-
90
  with gr.Accordion("Advanced Settings", open=False):
91
  negative_prompt = gr.Text(
92
  label="Negative prompt",
@@ -143,10 +166,20 @@ with gr.Blocks(css=css) as demo:
143
  examples=examples,
144
  inputs=[prompt]
145
  )
 
 
 
 
 
 
 
 
 
 
146
  gr.on(
147
  triggers=[run_button.click, prompt.submit],
148
  fn=infer,
149
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, tag_selection],
150
  outputs=[result, seed]
151
  )
152
 
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
+ # First set of tags
24
+ tag_options_1 = {
25
+ "Fantasy": "fantasy",
26
+ "Sci-Fi": "sci-fi",
27
+ "Realistic": "realistic",
28
+ "Cyberpunk": "cyberpunk",
29
+ "Noir": "noir",
30
+ }
31
+
32
+ # Second set of tags
33
+ tag_options_2 = {
34
+ "Vibrant Colors": "vibrant colors",
35
+ "Dark Theme": "dark theme",
36
+ "Minimalist": "minimalist",
37
+ "Photorealistic": "photorealistic",
38
+ "Abstract": "abstract",
39
+ }
40
+
41
  @spaces.GPU # [uncomment to use ZeroGPU]
42
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, tag_selection_1, tag_selection_2, use_tags, progress=gr.Progress(track_tqdm=True)):
43
 
44
+ # Determine prompt based on the tab selection
45
+ if use_tags:
46
+ selected_tags_1 = [tag_options_1[tag] for tag in tag_selection_1 if tag in tag_options_1]
47
+ selected_tags_2 = [tag_options_2[tag] for tag in tag_selection_2 if tag in tag_options_2]
48
+ tags_text = ', '.join(selected_tags_1 + selected_tags_2)
49
+ final_prompt = f'score_9, score_8_up, score_7_up, source_anime, {tags_text}'
50
+ else:
51
+ final_prompt = f'score_9, score_8_up, score_7_up, source_anime, {prompt}'
52
 
53
  if randomize_seed:
54
  seed = random.randint(0, MAX_SEED)
 
80
  }
81
  """
82
 
 
 
 
 
 
 
83
  with gr.Blocks(css=css) as demo:
84
 
85
  with gr.Column(elem_id="col-container"):
 
87
  # Text-to-Image Gradio Template
88
  """)
89
 
90
+ # Tabbed interface to select either Prompt or Tags
91
+ with gr.Tabs() as tabs:
92
+ # Prompt-only tab
93
+ with gr.TabItem("Prompt Input"):
94
+ prompt = gr.Text(
95
+ label="Prompt",
96
+ show_label=False,
97
+ max_lines=1,
98
+ placeholder="Enter your prompt",
99
+ container=False,
100
+ )
101
+ use_tags = gr.State(False) # This keeps track of whether tags are used
102
 
103
+ # Tag-based input tab
104
+ with gr.TabItem("Tag Selection"):
105
+ with gr.Row():
106
+ tag_selection_1 = gr.CheckboxGroup(choices=list(tag_options_1.keys()), label="Select Tags (Style)")
107
+ tag_selection_2 = gr.CheckboxGroup(choices=list(tag_options_2.keys()), label="Select Tags (Theme)")
108
+ use_tags = gr.State(True) # When this tab is selected, tags are used
109
 
110
+ run_button = gr.Button("Run", scale=0)
111
  result = gr.Image(label="Result", show_label=False)
112
 
 
 
 
 
113
  with gr.Accordion("Advanced Settings", open=False):
114
  negative_prompt = gr.Text(
115
  label="Negative prompt",
 
166
  examples=examples,
167
  inputs=[prompt]
168
  )
169
+
170
+ # Use `run_button.click` event to check which tab is selected
171
+ def check_tab(prompt, tag_selection_1, tag_selection_2, selected_tab):
172
+ if selected_tab == "Prompt Input":
173
+ return False # Use prompt only
174
+ return True # Use tags if on "Tag Selection"
175
+
176
+ # Set the selected tab
177
+ tabs.change(check_tab, inputs=[prompt, tag_selection_1, tag_selection_2, tabs], outputs=use_tags)
178
+
179
  gr.on(
180
  triggers=[run_button.click, prompt.submit],
181
  fn=infer,
182
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, tag_selection_1, tag_selection_2, use_tags],
183
  outputs=[result, seed]
184
  )
185