Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
69 |
-
|
70 |
-
|
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 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
106 |
)
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
margin-bottom: 1rem;
|
161 |
-
border-radius: 0.25rem;
|
162 |
-
}
|
163 |
-
</style>
|
164 |
-
""")
|
165 |
|
166 |
-
#
|
167 |
-
|
168 |
-
|
169 |
-
|
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 |
-
|
190 |
-
|
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
|
214 |
outputs=[output_gallery, error_message]
|
215 |
)
|
216 |
|
|
|
217 |
gr.Markdown("""
|
218 |
## How to Use
|
219 |
-
1.
|
220 |
-
2.
|
221 |
-
3.
|
222 |
-
4.
|
223 |
-
5.
|
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,
|