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
|
|
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 |
+
"""
|
27 |
+
Generate images based on the given prompt and parameters.
|
28 |
+
Returns list of generated image paths.
|
29 |
+
"""
|
30 |
+
try:
|
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,
|
40 |
+
width=width,
|
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)
|
47 |
+
images.append(save_path)
|
48 |
+
|
49 |
+
return images
|
50 |
+
except Exception as e:
|
51 |
+
return str(e)
|
52 |
+
|
53 |
+
def create_interface():
|
54 |
+
"""Create and configure the Gradio interface"""
|
55 |
+
|
56 |
+
STYLES = [
|
57 |
+
"Realistic",
|
58 |
+
"Artistic",
|
59 |
+
"Abstract",
|
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 |
+
|
203 |
+
try:
|
204 |
+
images = generate_image(prompt, style, num_images, width, height)
|
205 |
+
if isinstance(images, str): # Error occurred
|
206 |
+
return None, images
|
207 |
+
return images, "Generation successful!"
|
208 |
+
except Exception as e:
|
209 |
+
return None, f"Error: {str(e)}"
|
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 |
+
""")
|
228 |
+
|
229 |
+
return interface
|
230 |
+
|
231 |
+
if __name__ == "__main__":
|
232 |
+
app = create_interface()
|
233 |
+
app.launch(
|
234 |
+
share=True,
|
235 |
+
enable_queue=True,
|
236 |
+
server_name="0.0.0.0",
|
237 |
+
server_port=7860
|
238 |
+
)
|