lynnhao commited on
Commit
fbad72d
·
verified ·
1 Parent(s): 002a82b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +380 -89
app.py CHANGED
@@ -1,106 +1,397 @@
1
- import gradio as gr
2
- from diffusers import DiffusionPipeline
 
 
3
  import torch
 
 
 
 
 
 
4
 
5
- model_name = "Qwen/Qwen-Image"
6
- aspect_ratios = {
7
- "1:1": (1328, 1328),
8
- "16:9": (1664, 928),
9
- "9:16": (928, 1664),
10
- "4:3": (1472, 1140),
11
- "3:4": (1140, 1472)
12
- }
13
- negative_prompt = " "
14
-
15
- if torch.cuda.is_available():
16
- torch_dtype = torch.bfloat16
17
- device = "cuda"
18
- else:
19
- torch_dtype = torch.float32
20
- device = "cpu"
21
-
22
- # Load the pipeline
23
- pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype)
24
- pipe = pipe.to(device)
25
-
26
- positive_magic = {
27
- "en": "Ultra HD, 4K, cinematic composition.",
28
- "zh": "超清,4K,电影级构图"
29
- }
30
-
31
- def generate_image(prompt):
32
- """Generate image from text prompt"""
33
- # Use the input prompt instead of hardcoded one
34
- if not prompt.strip():
35
- prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197".'''
36
-
37
- width, height = aspect_ratios["16:9"]
38
-
39
- try:
40
- image = pipe(
41
- prompt=prompt + " " + positive_magic["en"],
42
- negative_prompt=negative_prompt,
43
- width=width,
44
- height=height,
45
- num_inference_steps=50,
46
- true_cfg_scale=4.0,
47
- generator=torch.Generator(device=device).manual_seed(42) # Use dynamic device
48
- ).images[0]
49
-
50
- # Save the image
51
- image.save("example.png")
52
- return image
53
 
54
- except Exception as e:
55
- print(f"Error generating image: {e}")
56
  return None
57
 
58
- # Custom CSS for styling
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  custom_css = """
 
60
  .gradio-container {
61
- font-family: 'Arial', sans-serif;
 
 
 
 
62
  }
63
- .title {
 
 
 
 
 
 
 
 
 
64
  text-align: center;
65
- color: #2c3e50;
66
  }
67
- """
68
 
69
- # Create Gradio interface
70
- with gr.Blocks(css=custom_css, title="🎨 Artist Ideation - Image Generation") as block:
71
- gr.Markdown("# 🎨 Artist Ideation - Image Generation")
72
- gr.Markdown("Enter a text prompt to generate an image using the Qwen diffusion model.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- with gr.Row():
75
- with gr.Column():
76
- prompt_input = gr.Textbox(
77
- label="Enter your prompt",
78
- placeholder="Describe the image you want to generate...",
79
- lines=3
80
- )
81
- generate_btn = gr.Button("Generate Image", variant="primary")
82
-
83
- with gr.Column():
84
- output_image = gr.Image(label="Generated Image")
85
 
86
- # Connect the function to the interface
87
- generate_btn.click(
88
- fn=generate_image,
89
- inputs=prompt_input,
90
- outputs=output_image
91
- )
 
 
 
 
 
 
 
92
 
93
- # Add example prompts
94
- gr.Examples(
95
- examples=[
96
- ["A serene landscape with mountains and a lake at sunset"],
97
- ["A futuristic city with flying cars and neon lights"],
98
- ["A cozy coffee shop with warm lighting and comfortable seating"],
99
- ["A magical forest with glowing mushrooms and fairy lights"]
100
- ],
101
- inputs=prompt_input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  )
103
 
104
- # Launch the interface
105
  if __name__ == "__main__":
106
- block.queue().launch(share=True)
 
1
+ import os
2
+ import random
3
+ import sys
4
+ from typing import Sequence, Mapping, Any, Union
5
  import torch
6
+ import gradio as gr
7
+ from PIL import Image
8
+ from huggingface_hub import hf_hub_download
9
+ import base64
10
+ from io import BytesIO
11
+ from openai import OpenAI
12
 
13
+ def init_gpt_api():
14
+ return OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
15
+
16
+ def base64_to_pil(base64_str):
17
+ image_data = base64.b64decode(base64_str)
18
+ return Image.open(BytesIO(image_data))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ def customize_image(prompt, image):
21
+ if image is None or prompt.strip() == "":
22
  return None
23
 
24
+ prompt = f"Convert this image into a {prompt} drawing"
25
+ client = init_gpt_api()
26
+ # image_base64 = encode_image_from_pil(image)
27
+
28
+ result = client.images.edit(
29
+ model="gpt-image-1",
30
+ image=[open(image, 'rb')],
31
+ prompt=prompt,
32
+ )
33
+ if result.data:
34
+ image_base64 = result.data[0].b64_json
35
+ return base64_to_pil(image_base64)
36
+ else:
37
+ return None
38
+
39
+
40
+ # Create Gradio interface
41
+
42
  custom_css = """
43
+ /* Global styling */
44
  .gradio-container {
45
+ max-width: 1200px !important;
46
+ margin: 0 auto !important;
47
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
48
+ min-height: 100vh;
49
+ padding: 20px;
50
  }
51
+
52
+ /* Header styling */
53
+ .main-header {
54
+ background: rgba(255, 255, 255, 0.95);
55
+ backdrop-filter: blur(10px);
56
+ border-radius: 20px;
57
+ padding: 40px;
58
+ margin-bottom: 30px;
59
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
60
+ border: 1px solid rgba(255, 255, 255, 0.2);
61
  text-align: center;
 
62
  }
 
63
 
64
+ .main-header h1 {
65
+ background: linear-gradient(45deg, #667eea, #764ba2);
66
+ -webkit-background-clip: text;
67
+ -webkit-text-fill-color: transparent;
68
+ background-clip: text;
69
+ font-size: 3rem !important;
70
+ font-weight: 700 !important;
71
+ margin-bottom: 15px !important;
72
+ text-align: center !important;
73
+ }
74
+
75
+ .main-header h3 {
76
+ color: #6b7280 !important;
77
+ text-align: center !important;
78
+ font-weight: 400 !important;
79
+ font-size: 1.3rem !important;
80
+ line-height: 1.6 !important;
81
+ }
82
+
83
+ /* Step containers */
84
+ .step-container {
85
+ background: rgba(255, 255, 255, 0.9);
86
+ backdrop-filter: blur(10px);
87
+ border-radius: 16px;
88
+ padding: 30px;
89
+ margin-bottom: 25px;
90
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
91
+ border: 1px solid rgba(255, 255, 255, 0.3);
92
+ transition: transform 0.2s ease, box-shadow 0.2s ease;
93
+ }
94
+
95
+ .step-container:hover {
96
+ transform: translateY(-2px);
97
+ box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
98
+ }
99
+
100
+ /* Step headers */
101
+ .step-header {
102
+ display: flex;
103
+ align-items: center;
104
+ margin-bottom: 25px;
105
+ padding-bottom: 20px;
106
+ border-bottom: 3px solid #f3f4f6;
107
+ }
108
+
109
+ .step-number {
110
+ background: linear-gradient(45deg, #667eea, #764ba2);
111
+ color: white;
112
+ width: 45px;
113
+ height: 45px;
114
+ border-radius: 50%;
115
+ display: flex;
116
+ align-items: center;
117
+ justify-content: center;
118
+ font-weight: bold;
119
+ font-size: 1.3rem;
120
+ margin-right: 20px;
121
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
122
+ }
123
+
124
+ .step-title {
125
+ font-size: 1.5rem;
126
+ font-weight: 600;
127
+ color: #374151;
128
+ margin: 0;
129
+ }
130
+
131
+ /* Enhanced button styling */
132
+ .primary-button {
133
+ background: linear-gradient(45deg, #667eea, #764ba2) !important;
134
+ border: none !important;
135
+ border-radius: 12px !important;
136
+ padding: 15px 35px !important;
137
+ font-weight: 600 !important;
138
+ font-size: 1.1rem !important;
139
+ color: white !important;
140
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3) !important;
141
+ transition: all 0.3s ease !important;
142
+ margin: 15px 5px !important;
143
+ min-height: 50px !important;
144
+ width: 100% !important;
145
+ }
146
+
147
+ .primary-button:hover {
148
+ transform: translateY(-2px) !important;
149
+ box-shadow: 0 6px 25px rgba(102, 126, 234, 0.4) !important;
150
+ }
151
+
152
+ .secondary-button {
153
+ background: linear-gradient(45deg, #10b981, #059669) !important;
154
+ border: none !important;
155
+ border-radius: 12px !important;
156
+ padding: 15px 35px !important;
157
+ font-weight: 600 !important;
158
+ font-size: 1.1rem !important;
159
+ color: white !important;
160
+ box-shadow: 0 4px 15px rgba(16, 185, 129, 0.3) !important;
161
+ transition: all 0.3s ease !important;
162
+ margin: 15px 5px !important;
163
+ min-height: 50px !important;
164
+ width: 100% !important;
165
+ }
166
+
167
+ .secondary-button:hover {
168
+ transform: translateY(-2px) !important;
169
+ box-shadow: 0 6px 25px rgba(16, 185, 129, 0.4) !important;
170
+ }
171
+
172
+ /* Enhanced input styling */
173
+ .gr-textbox input, .gr-dropdown, .gr-slider {
174
+ border: 2px solid #e5e7eb !important;
175
+ border-radius: 10px !important;
176
+ padding: 12px 16px !important;
177
+ font-size: 1rem !important;
178
+ transition: all 0.3s ease !important;
179
+ }
180
+
181
+ .gr-textbox input:focus, .gr-dropdown:focus {
182
+ border-color: #667eea !important;
183
+ box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
184
+ outline: none !important;
185
+ }
186
+
187
+ /* Upload area enhancement */
188
+ .upload-area {
189
+ border: 3px dashed #667eea;
190
+ border-radius: 15px;
191
+ padding: 10px;
192
+ text-align: center;
193
+ background: linear-gradient(135deg, rgba(102, 126, 234, 0.05), rgba(118, 75, 162, 0.05));
194
+ transition: all 0.3s ease;
195
+ min-height: 150px;
196
+ display: flex;
197
+ align-items: center;
198
+ justify-content: center;
199
+ }
200
+
201
+ .upload-area:hover {
202
+ border-color: #764ba2;
203
+ background: linear-gradient(135deg, rgba(102, 126, 234, 0.1), rgba(118, 75, 162, 0.1));
204
+ transform: translateY(-2px);
205
+ }
206
+
207
+ /* Image preview styling */
208
+ .image-preview {
209
+ border: 2px solid #e2e8f0;
210
+ border-radius: 12px;
211
+ overflow: hidden;
212
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
213
+ background: #f8fafc;
214
+ }
215
+
216
+ /* Gallery styling */
217
+ .enhanced-gallery {
218
+ border: 2px solid #e2e8f0;
219
+ border-radius: 12px;
220
+ padding: 15px;
221
+ background: #f8fafc;
222
+ min-height: 300px;
223
+ }
224
+
225
+ /* Control groups */
226
+ .control-group {
227
+ background: #f8fafc;
228
+ border: 2px solid #e2e8f0;
229
+ border-radius: 12px;
230
+ padding: 20px;
231
+ margin: 15px 0;
232
+ }
233
+
234
+ .control-group h4 {
235
+ color: #374151;
236
+ font-weight: 600;
237
+ margin-bottom: 15px;
238
+ text-align: center;
239
+ }
240
+
241
+ /* Info boxes */
242
+ .info-box {
243
+ background: linear-gradient(45deg, #dbeafe, #bfdbfe);
244
+ border: 2px solid #60a5fa;
245
+ border-radius: 10px;
246
+ padding: 15px 20px;
247
+ margin: 15px 0;
248
+ color: #1e40af;
249
+ font-weight: 500;
250
+ }
251
+
252
+ .warning-box {
253
+ background: linear-gradient(45deg, #fef3c7, #fde68a);
254
+ border: 2px solid #f59e0b;
255
+ border-radius: 10px;
256
+ padding: 15px 20px;
257
+ margin: 15px 0;
258
+ color: #92400e;
259
+ font-weight: 500;
260
+ }
261
+
262
+ .success-box {
263
+ background: linear-gradient(45deg, #d1fae5, #a7f3d0);
264
+ border: 2px solid #10b981;
265
+ border-radius: 10px;
266
+ padding: 15px 20px;
267
+ margin: 15px 0;
268
+ color: #065f46;
269
+ font-weight: 500;
270
+ }
271
+
272
+ /* Section dividers */
273
+ .section-divider {
274
+ height: 3px;
275
+ background: linear-gradient(45deg, #667eea, #764ba2);
276
+ border-radius: 2px;
277
+ margin: 30px 0;
278
+ opacity: 0.7;
279
+ }
280
+
281
+ /* Examples section */
282
+ .examples-section {
283
+ background: rgba(255, 255, 255, 0.9);
284
+ backdrop-filter: blur(10px);
285
+ border-radius: 16px;
286
+ padding: 30px;
287
+ margin-top: 30px;
288
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
289
+ }
290
+ /* Responsive design */
291
+ @media (max-width: 768px) {
292
+ .gradio-container {
293
+ padding: 15px;
294
+ }
295
 
296
+ .main-header {
297
+ padding: 25px;
298
+ }
 
 
 
 
 
 
 
 
299
 
300
+ .main-header h1 {
301
+ font-size: 2.5rem !important;
302
+ }
303
+
304
+ .step-container {
305
+ padding: 20px;
306
+ }
307
+
308
+ .step-number {
309
+ width: 35px;
310
+ height: 35px;
311
+ font-size: 1.1rem;
312
+ }
313
 
314
+ .step-title {
315
+ font-size: 1.3rem;
316
+ }
317
+ }
318
+
319
+ /* Hide default gradio styling conflicts */
320
+ .gr-box {
321
+ margin-bottom: 0px !important;
322
+ border: none !important;
323
+ }
324
+
325
+ .gr-panel {
326
+ border: none !important;
327
+ background: transparent !important;
328
+ }
329
+
330
+ .gr-form {
331
+ background: transparent !important;
332
+ }
333
+ """
334
+ examples = [
335
+ ["", "mona.png", "receita-tacos.webp", 15, 0.6],
336
+ ["a woman looking at a house catching fire on the background", "disaster_girl.png", "abaporu.jpg", 15, 0.15],
337
+ ["istanbul aerial, dramatic photography", "natasha.png", "istambul.jpg", 15, 0.5],
338
+ ]
339
+ output_image = gr.Image(label="Generated Image")
340
+ multi_image = gr.Image(label="Generated Image")
341
+ # customized_image = gr.Image(label="Customized Image")
342
+ medium_images = gr.Gallery(label="Images", columns=[2], rows=[2], elem_id="gallery", format="png")
343
+
344
+
345
+ with gr.Blocks(css=custom_css, title="🎨 Artist Ideation - Advanced Style Transfer") as app:
346
+ # Enhanced Header
347
+ gr.HTML("""
348
+ <div class="main-header">
349
+ <h1>🎨 Artist Ideation</h1>
350
+ <h3>Advanced Image Medium Transfer </h3>
351
+ </div>
352
+ """)
353
+
354
+ with gr.Row(elem_classes="step-container"):
355
+ with gr.Column():
356
+ gr.HTML("""
357
+ <div class="step-header">
358
+ <h2 class="step-title">Artistic Medium Transformation</h2>
359
+ </div>
360
+ """)
361
+ with gr.Row():
362
+ with gr.Column(scale=1):
363
+ structure_image = gr.Image(label="Structure Image", type="filepath")
364
+ custom_medium = gr.Textbox(
365
+ label="🎨 Artistic Medium",
366
+ placeholder="Enter your desired medium (e.g., watercolor, charcoal sketch, digital art)...",
367
+ lines=2,
368
+ )
369
+ gr.HTML("""
370
+ <div class="success-box">
371
+ 💡 <strong>Pro Tips:</strong> Try "watercolor portrait", "pencil sketch", "oil painting masterpiece",
372
+ or "digital art concept" for best results!
373
+ </div>
374
+ """)
375
+ customize_btn = gr.Button(
376
+ "🖌️ Transform Medium",
377
+ variant="primary",
378
+ elem_classes="secondary-button",
379
+ size="lg"
380
+ )
381
+
382
+ with gr.Column(scale=1):
383
+ customized_image = gr.Image(
384
+ label="🎭 Artistic Transformation",
385
+ elem_classes="image-preview",
386
+ height=400
387
+ )
388
+ # customized_image.render()
389
+
390
+ customize_btn.click(
391
+ fn=customize_image,
392
+ inputs=[custom_medium, structure_image],
393
+ outputs=[customized_image]
394
  )
395
 
 
396
  if __name__ == "__main__":
397
+ app.launch(share=True)