LAYEK-143 commited on
Commit
7374f55
·
verified ·
1 Parent(s): 88f6c1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -154
app.py CHANGED
@@ -1,26 +1,6 @@
1
  import gradio as gr
2
  import os
3
  from datetime import datetime
4
- import json
5
- from http.cookies import SimpleCookie
6
-
7
- def load_user_consent():
8
- """Load user consent status from cookies"""
9
- try:
10
- with open('user_consent.json', 'r') as f:
11
- return json.load(f)
12
- except:
13
- return {}
14
-
15
- def save_user_consent(user_id, consent):
16
- """Save user consent status to cookies"""
17
- consents = load_user_consent()
18
- consents[user_id] = {
19
- 'nsfw_consent': consent,
20
- 'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
21
- }
22
- with open('user_consent.json', 'w') as f:
23
- json.dump(consents, f)
24
 
25
  def generate_image(prompt, style, num_images=1, width=512, height=512):
26
  """
@@ -31,9 +11,11 @@ def generate_image(prompt, style, num_images=1, width=512, height=512):
31
  model = gr.load("models/LAYEK-143/FLUX_V0")
32
  images = []
33
  for i in range(num_images):
 
34
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
35
  filename = f"generated_{timestamp}_{i}.png"
36
 
 
37
  result = model.predict(
38
  prompt=prompt,
39
  style=style,
@@ -41,6 +23,7 @@ def generate_image(prompt, style, num_images=1, width=512, height=512):
41
  height=height
42
  )
43
 
 
44
  os.makedirs("generated_images", exist_ok=True)
45
  save_path = os.path.join("generated_images", filename)
46
  result.save(save_path)
@@ -53,6 +36,7 @@ def generate_image(prompt, style, num_images=1, width=512, height=512):
53
  def create_interface():
54
  """Create and configure the Gradio interface"""
55
 
 
56
  STYLES = [
57
  "Realistic",
58
  "Artistic",
@@ -60,143 +44,69 @@ def create_interface():
60
  "Cartoon",
61
  "Sketch"
62
  ]
63
-
64
- # State variables for consent management
65
- has_consented = gr.State(False)
66
- user_id = gr.State(datetime.now().strftime("%Y%m%d%H%M%S")) # Simple user identification
67
 
68
- with gr.Blocks(title="Advanced Image Generator") as interface:
69
- # Content Warning Modal
70
- with gr.Box(visible=True) as warning_modal:
71
- gr.Markdown("""
72
- # ⚠️ Content Warning
73
-
74
- This image generation tool can potentially create sensitive or NSFW content.
75
- By proceeding, you acknowledge that:
76
-
77
- - You are 18 years or older
78
- - You understand that generated content may be inappropriate
79
- - You accept responsibility for the content you generate
80
- - You will not use this tool to create harmful or illegal content
81
-
82
- Do you accept these terms?
83
- """)
84
-
85
- with gr.Row():
86
- accept_btn = gr.Button("Accept", variant="primary")
87
- decline_btn = gr.Button("Decline", variant="secondary")
88
 
89
- # Main Interface (initially hidden)
90
- with gr.Box(visible=False) as main_interface:
91
- gr.Markdown("# 🎨 Advanced Image Generator")
92
-
93
- # NSFW Warning Banner
94
- with gr.Box(elem_classes="warning-banner"):
95
- gr.Markdown("""
96
- ⚠️ **NSFW Warning**: This tool can generate sensitive content.
97
- Please use responsibly and ensure compliance with all applicable laws and guidelines.
98
- """)
99
-
100
- with gr.Row():
101
- with gr.Column():
102
- prompt = gr.Textbox(
103
- label="Prompt",
104
- placeholder="Describe the image you want to generate...",
105
- lines=3
 
 
 
 
 
106
  )
107
 
108
- style = gr.Dropdown(
109
- choices=STYLES,
110
- label="Style",
111
- value="Realistic"
 
 
 
 
 
 
 
 
 
 
112
  )
113
-
114
- with gr.Row():
115
- num_images = gr.Slider(
116
- minimum=1,
117
- maximum=4,
118
- value=1,
119
- step=1,
120
- label="Number of Images"
121
- )
122
-
123
- with gr.Row():
124
- width = gr.Slider(
125
- minimum=256,
126
- maximum=1024,
127
- value=512,
128
- step=64,
129
- label="Width"
130
- )
131
- height = gr.Slider(
132
- minimum=256,
133
- maximum=1024,
134
- value=512,
135
- step=64,
136
- label="Height"
137
- )
138
-
139
- generate_btn = gr.Button("Generate Images", variant="primary")
140
 
141
- with gr.Column():
142
- output_gallery = gr.Gallery(
143
- label="Generated Images",
144
- show_label=True,
145
- elem_id="gallery"
146
- ).style(grid=2, height="auto")
147
-
148
- error_message = gr.Textbox(
149
- label="Status",
150
- visible=False
151
- )
152
 
153
- # Add CSS for styling
154
- gr.Markdown("""
155
- <style>
156
- .warning-banner {
157
- background-color: #fff3cd;
158
- border: 1px solid #ffeeba;
159
- padding: 1rem;
160
- margin-bottom: 1rem;
161
- border-radius: 0.25rem;
162
- }
163
- </style>
164
- """)
165
 
166
- # Handle consent button clicks
167
- def on_accept(user_id_val):
168
- save_user_consent(user_id_val, True)
169
- return {
170
- warning_modal: gr.update(visible=False),
171
- main_interface: gr.update(visible=True),
172
- has_consented: True
173
- }
174
-
175
- def on_decline(user_id_val):
176
- save_user_consent(user_id_val, False)
177
- return {
178
- warning_modal: gr.update(visible=True),
179
- main_interface: gr.update(visible=False),
180
- has_consented: False
181
- }
182
-
183
- accept_btn.click(
184
- fn=on_accept,
185
- inputs=[user_id],
186
- outputs=[warning_modal, main_interface, has_consented]
187
  )
188
 
189
- decline_btn.click(
190
- fn=on_decline,
191
- inputs=[user_id],
192
- outputs=[warning_modal, main_interface, has_consented]
193
- )
194
-
195
- # Handle image generation
196
- def on_generate(prompt, style, num_images, width, height, has_consented_val):
197
- if not has_consented_val:
198
- return None, "Please accept the content warning first."
199
-
200
  if not prompt.strip():
201
  return None, "Please enter a prompt."
202
 
@@ -210,18 +120,18 @@ def create_interface():
210
 
211
  generate_btn.click(
212
  fn=on_generate,
213
- inputs=[prompt, style, num_images, width, height, has_consented],
214
  outputs=[output_gallery, error_message]
215
  )
216
 
 
217
  gr.Markdown("""
218
  ## How to Use
219
- 1. Accept the content warning to proceed
220
- 2. Enter a detailed description of the image you want to generate
221
- 3. Select a style from the dropdown menu
222
- 4. Adjust the number of images and dimensions as needed
223
- 5. Click 'Generate Images' and wait for the results
224
- 6. Generated images will be saved in the 'generated_images' folder
225
 
226
  Note: Generation time may vary depending on the complexity of your prompt.
227
  """)
@@ -229,6 +139,7 @@ def create_interface():
229
  return interface
230
 
231
  if __name__ == "__main__":
 
232
  app = create_interface()
233
  app.launch(
234
  share=True,
 
1
  import gradio as gr
2
  import os
3
  from datetime import datetime
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def generate_image(prompt, style, num_images=1, width=512, height=512):
6
  """
 
11
  model = gr.load("models/LAYEK-143/FLUX_V0")
12
  images = []
13
  for i in range(num_images):
14
+ # Generate unique filename with timestamp
15
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
16
  filename = f"generated_{timestamp}_{i}.png"
17
 
18
+ # Generate the image
19
  result = model.predict(
20
  prompt=prompt,
21
  style=style,
 
23
  height=height
24
  )
25
 
26
+ # Save the image
27
  os.makedirs("generated_images", exist_ok=True)
28
  save_path = os.path.join("generated_images", filename)
29
  result.save(save_path)
 
36
  def create_interface():
37
  """Create and configure the Gradio interface"""
38
 
39
+ # Define available style options
40
  STYLES = [
41
  "Realistic",
42
  "Artistic",
 
44
  "Cartoon",
45
  "Sketch"
46
  ]
 
 
 
 
47
 
48
+ # Create the interface
49
+ with gr.Blocks(title="Image Generator") as interface:
50
+ gr.Markdown("# 🎨 Image Generator")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ with gr.Row():
53
+ with gr.Column():
54
+ # Input components
55
+ prompt = gr.Textbox(
56
+ label="Prompt",
57
+ placeholder="Describe the image you want to generate...",
58
+ lines=3
59
+ )
60
+
61
+ style = gr.Dropdown(
62
+ choices=STYLES,
63
+ label="Style",
64
+ value="Realistic"
65
+ )
66
+
67
+ with gr.Row():
68
+ num_images = gr.Slider(
69
+ minimum=1,
70
+ maximum=4,
71
+ value=1,
72
+ step=1,
73
+ label="Number of Images"
74
  )
75
 
76
+ with gr.Row():
77
+ width = gr.Slider(
78
+ minimum=256,
79
+ maximum=1024,
80
+ value=512,
81
+ step=64,
82
+ label="Width"
83
+ )
84
+ height = gr.Slider(
85
+ minimum=256,
86
+ maximum=1024,
87
+ value=512,
88
+ step=64,
89
+ label="Height"
90
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ generate_btn = gr.Button("Generate Images", variant="primary")
 
 
 
 
 
 
 
 
 
 
93
 
94
+ with gr.Column():
95
+ # Output gallery
96
+ output_gallery = gr.Gallery(
97
+ label="Generated Images",
98
+ show_label=True,
99
+ elem_id="gallery"
100
+ ).style(grid=2, height="auto")
 
 
 
 
 
101
 
102
+ # Error message display
103
+ error_message = gr.Textbox(
104
+ label="Status",
105
+ visible=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  )
107
 
108
+ # Handle generation
109
+ def on_generate(prompt, style, num_images, width, height):
 
 
 
 
 
 
 
 
 
110
  if not prompt.strip():
111
  return None, "Please enter a prompt."
112
 
 
120
 
121
  generate_btn.click(
122
  fn=on_generate,
123
+ inputs=[prompt, style, num_images, width, height],
124
  outputs=[output_gallery, error_message]
125
  )
126
 
127
+ # Add usage instructions
128
  gr.Markdown("""
129
  ## How to Use
130
+ 1. Enter a detailed description of the image you want to generate
131
+ 2. Select a style from the dropdown menu
132
+ 3. Adjust the number of images and dimensions as needed
133
+ 4. Click 'Generate Images' and wait for the results
134
+ 5. Generated images will be saved in the 'generated_images' folder
 
135
 
136
  Note: Generation time may vary depending on the complexity of your prompt.
137
  """)
 
139
  return interface
140
 
141
  if __name__ == "__main__":
142
+ # Create and launch the interface
143
  app = create_interface()
144
  app.launch(
145
  share=True,