helms commited on
Commit
a1eb9b9
·
1 Parent(s): ba6312c

test gallery clearing

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -7,19 +7,16 @@ import os
7
  import warnings
8
  from dotenv import load_dotenv
9
 
10
-
11
  load_dotenv()
12
  SD_KEY = os.getenv("SD_KEY")
13
  HF_KEY = os.getenv("HF_KEY")
14
  USERNAME = os.getenv("USERNAME")
15
  PASS = os.getenv("PASS")
16
- CD_KEY = os.getenv("CD_KEY")
17
-
18
 
 
19
  hf_writer_gen = gr.HuggingFaceDatasetSaver(HF_KEY, "helms/master-thesis-generated-images-1", private=True, separate_dirs=False)
20
 
21
-
22
-
23
  def infer(prompt):
24
  stability_api = client.StabilityInference(
25
  key=SD_KEY, # AaPI Key reference.
@@ -44,19 +41,21 @@ def infer(prompt):
44
  # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers.
45
  # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m)
46
  )
47
- infer.results = []
48
  for resp in answers:
49
  for artifact in resp.artifacts:
50
  if artifact.finish_reason == generation.FILTER:
51
  warnings.warn(
52
- "Your request activated the API's safety filters and could not be processed."
53
  "Please modify the prompt and try again.")
54
  if artifact.type == generation.ARTIFACT_IMAGE:
55
  img = Image.open(io.BytesIO(artifact.binary))
 
56
  return img
57
 
58
 
59
  with gr.Blocks(title="Master Thesis Image Generator") as demo:
 
60
  with gr.Row():
61
  gr.HTML("""
62
  <div style="text-align: justify; margin: 0 auto;">
@@ -81,14 +80,15 @@ with gr.Blocks(title="Master Thesis Image Generator") as demo:
81
  with gr.Row(equal_height=True):
82
  with gr.Column(scale=1):
83
  user = gr.Textbox(placeholder="Please enter your name.", label="Name")
84
- inp = gr.Textbox(placeholder="Enter your prompt here.", lines=3, label="Prompt")
85
- btn_gen = gr.Button("Generate Images", variant="primary", size="lg", visible=True)
86
  with gr.Box():
87
- gr.HTML("""<div style="text-align: center; margin: 0 auto; padding-bottom: 10px"><p style="font-size:10pt">Generated Images</p></div>""")
88
- store = gr.Gallery(visible=True, show_share_button=False, allow_preview=False, columns=1)
 
 
89
  with gr.Column(scale=3):
90
- out = gr.Image(interactive=False, visible=True, container=True, width=1024,
91
- show_share_button=False)
92
  with gr.Row():
93
  with gr.Accordion("See Examples:", open=False):
94
  examples = [
@@ -98,19 +98,28 @@ with gr.Blocks(title="Master Thesis Image Generator") as demo:
98
  ["Beautiful waterfall in a lush jungle, with sunlight shining through the trees"]
99
  ]
100
  ex = gr.Examples(examples=examples, inputs=inp)
 
101
  def make_visible():
102
  return gr.update(visible=True)
 
103
  def make_invisible():
104
  return gr.update(visible=False)
 
105
  def make_inactive():
106
  return gr.update(interactive=False)
 
107
  def make_active():
108
  return gr.update(interactive=True)
109
 
110
  def add_to_gallery(img):
111
- gall = store.value
112
  gall.insert(0, img)
113
  return gall
 
 
 
 
 
114
  def select_img(evt: gr.SelectData):
115
  sel_img = store.value[evt.index]
116
  return sel_img
@@ -133,12 +142,6 @@ with gr.Blocks(title="Master Thesis Image Generator") as demo:
133
 
134
  store.select(fn=select_img, outputs=out)
135
 
 
136
 
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
- demo.launch(show_api=False, auth=(USERNAME, PASS))
 
7
  import warnings
8
  from dotenv import load_dotenv
9
 
10
+ # load secrets
11
  load_dotenv()
12
  SD_KEY = os.getenv("SD_KEY")
13
  HF_KEY = os.getenv("HF_KEY")
14
  USERNAME = os.getenv("USERNAME")
15
  PASS = os.getenv("PASS")
 
 
16
 
17
+ # set up dataset writer
18
  hf_writer_gen = gr.HuggingFaceDatasetSaver(HF_KEY, "helms/master-thesis-generated-images-1", private=True, separate_dirs=False)
19
 
 
 
20
  def infer(prompt):
21
  stability_api = client.StabilityInference(
22
  key=SD_KEY, # AaPI Key reference.
 
41
  # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers.
42
  # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m)
43
  )
44
+ #infer.results = []
45
  for resp in answers:
46
  for artifact in resp.artifacts:
47
  if artifact.finish_reason == generation.FILTER:
48
  warnings.warn(
49
+ "Your request activated the APIs safety filters and could not be processed."
50
  "Please modify the prompt and try again.")
51
  if artifact.type == generation.ARTIFACT_IMAGE:
52
  img = Image.open(io.BytesIO(artifact.binary))
53
+ #infer.results.insert(0, img)
54
  return img
55
 
56
 
57
  with gr.Blocks(title="Master Thesis Image Generator") as demo:
58
+ storage = gr.State(value=[])
59
  with gr.Row():
60
  gr.HTML("""
61
  <div style="text-align: justify; margin: 0 auto;">
 
80
  with gr.Row(equal_height=True):
81
  with gr.Column(scale=1):
82
  user = gr.Textbox(placeholder="Please enter your name.", label="Name")
83
+ inp = gr.Textbox(placeholder="Enter your prompt here.", lines=5, label="Prompt")
84
+ btn_gen = gr.Button("Generate Image", variant="primary", size="lg", visible=True)
85
  with gr.Box():
86
+ gr.HTML(
87
+ """<div style="text-align: center; margin: 0 auto; padding-bottom: 10px"><p style="font-size:10pt">Generated Images</p></div>""")
88
+ store = gr.Gallery(value=storage.value, visible=True, show_share_button=False, allow_preview=False, columns=1)
89
+ gr.ClearButton(store)
90
  with gr.Column(scale=3):
91
+ out = gr.Image(interactive=False, visible=True, container=True, width=1024, show_share_button=False)
 
92
  with gr.Row():
93
  with gr.Accordion("See Examples:", open=False):
94
  examples = [
 
98
  ["Beautiful waterfall in a lush jungle, with sunlight shining through the trees"]
99
  ]
100
  ex = gr.Examples(examples=examples, inputs=inp)
101
+
102
  def make_visible():
103
  return gr.update(visible=True)
104
+
105
  def make_invisible():
106
  return gr.update(visible=False)
107
+
108
  def make_inactive():
109
  return gr.update(interactive=False)
110
+
111
  def make_active():
112
  return gr.update(interactive=True)
113
 
114
  def add_to_gallery(img):
115
+ gall = storage.value
116
  gall.insert(0, img)
117
  return gall
118
+
119
+ def clear_gallery():
120
+ gall = []
121
+ return gall, gall
122
+
123
  def select_img(evt: gr.SelectData):
124
  sel_img = store.value[evt.index]
125
  return sel_img
 
142
 
143
  store.select(fn=select_img, outputs=out)
144
 
145
+ demo.load(fn=clear_gallery, outputs=[store, storage], api_name=False)
146
 
147
+ demo.launch(share=True, show_api=False, auth=(USERNAME, PASS))