File size: 17,080 Bytes
43f666f
 
 
 
 
 
 
 
 
bfa8f28
43f666f
bfa8f28
43f666f
bfa8f28
 
6367647
bfa8f28
6367647
 
bfa8f28
 
 
 
 
 
 
6367647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfa8f28
6367647
 
 
 
 
 
 
 
 
 
bfa8f28
 
 
6367647
 
bfa8f28
6367647
 
 
bfa8f28
 
 
 
 
 
 
6367647
bfa8f28
 
 
 
 
 
 
 
 
 
6367647
bfa8f28
 
 
 
 
6367647
 
 
 
 
bfa8f28
 
 
 
 
6367647
bfa8f28
43f666f
 
 
 
 
6367647
43f666f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6367647
43f666f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6367647
43f666f
6367647
43f666f
 
6367647
43f666f
 
6367647
 
 
 
bfa8f28
 
6367647
 
 
 
 
 
bfa8f28
 
eed20b1
bfa8f28
eed20b1
43f666f
6367647
eed20b1
 
 
 
 
 
 
 
 
 
 
 
6367647
eed20b1
6367647
43f666f
bfa8f28
43f666f
 
eed20b1
43f666f
 
eed20b1
6367647
43f666f
eed20b1
 
 
 
 
6367647
eed20b1
 
43f666f
 
eed20b1
 
 
 
 
6367647
eed20b1
 
 
 
 
 
 
 
 
 
43f666f
 
 
6367647
eed20b1
6367647
eed20b1
 
 
 
6367647
 
43f666f
6367647
43f666f
 
6367647
43f666f
 
 
 
6367647
43f666f
 
 
6367647
43f666f
 
 
 
bfa8f28
43f666f
 
 
6367647
43f666f
 
6367647
43f666f
6367647
43f666f
6367647
43f666f
 
6367647
43f666f
 
6367647
43f666f
 
 
6367647
 
43f666f
 
6367647
43f666f
 
 
6367647
43f666f
 
6367647
 
bfa8f28
43f666f
 
 
 
 
 
 
 
 
 
 
 
6367647
 
 
43f666f
 
 
6367647
 
43f666f
 
 
 
 
6367647
 
43f666f
6367647
 
 
 
 
 
 
43f666f
6367647
 
43f666f
 
6367647
43f666f
 
 
6367647
 
 
bfa8f28
 
6367647
bfa8f28
 
 
6367647
bfa8f28
6367647
 
43f666f
 
 
 
 
 
6367647
 
43f666f
6367647
43f666f
 
 
 
 
 
 
6367647
 
 
 
 
43f666f
 
 
 
 
 
 
6367647
 
43f666f
6367647
43f666f
 
 
bfa8f28
6367647
bfa8f28
6367647
bfa8f28
 
43f666f
 
6367647
43f666f
 
6367647
43f666f
 
6367647
43f666f
 
6367647
43f666f
 
 
6367647
43f666f
 
 
 
 
 
 
6367647
43f666f
 
6367647
43f666f
 
bfa8f28
6367647
bfa8f28
 
 
 
6367647
bfa8f28
 
43f666f
 
 
 
 
6367647
43f666f
6367647
 
 
 
 
 
 
 
43f666f
 
 
6367647
bfa8f28
6367647
43f666f
 
6367647
43f666f
 
6367647
 
 
 
 
 
 
 
 
 
43f666f
bfa8f28
6367647
43f666f
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import gradio as gr
import replicate
import os
from PIL import Image
import requests
from io import BytesIO
import time
import tempfile
import base64
import json

# Set up API keys from environment variables
os.environ['REPLICATE_API_TOKEN'] = os.getenv('REPLICATE_API_TOKEN')
FIREWORKS_API_KEY = os.getenv('FIREWORKS_API_KEY', '')

def enhance_prompt_with_team(user_input):
    """
    Enhanced prompt generation using team collaboration approach:
    Director -> Engineer (with web search) -> Designer
    """
    if not FIREWORKS_API_KEY:
        return user_input
    
    try:
        url = "https://api.fireworks.ai/inference/v1/chat/completions"
        
        # Team collaboration system prompt
        system_prompt = """You are a product design team consisting of three roles working in sequence:

## TEAM COLLABORATION PROCESS:

### 1. DIRECTOR (Creative Vision Leader)
The Director analyzes the user's input and establishes:
- Core product category and target market
- Key functional requirements
- Design philosophy and brand positioning
- Initial design direction and constraints
- Questions for the engineering team

### 2. ENGINEER (Technical Feasibility Expert)
The Engineer responds to the Director's vision by:
- Researching current technologies and materials
- Identifying manufacturing possibilities
- Suggesting innovative technical features
- Evaluating feasibility of proposed concepts
- Providing data on similar successful products
- Recommending technical specifications

### 3. DESIGNER (Visual Design Specialist)
The Designer synthesizes all inputs to create the final prompt:
- Translating technical specs into visual language
- Adding aesthetic details and finishing touches
- Specifying materials, textures, and colors
- Defining the rendering style
- Creating the complete visual description

## OUTPUT FORMAT:
Structure the team discussion as follows:

**DIRECTOR'S VISION:**
[Director's analysis and initial direction]

**ENGINEER'S TECHNICAL ASSESSMENT:**
[Research findings and technical recommendations]

**DIRECTOR'S REFINEMENT:**
[Approval/modification of engineering inputs]

**DESIGNER'S FINAL PROMPT:**
[Complete product design prompt with all visual details]

Focus exclusively on PHYSICAL PRODUCT DESIGN - no services, apps, or abstract concepts.
Examples: furniture, electronics, vehicles, appliances, tools, wearables, etc."""
        
        # Create the collaborative dialogue
        user_prompt = f"""Create a product design for: {user_input}

Execute the full team collaboration process:
1. Director establishes the vision
2. Engineer researches and provides technical input (simulate web research findings)
3. Director refines based on engineering feedback
4. Designer creates the final detailed visual prompt

Remember: Focus ONLY on physical product design."""
        
        payload = {
            "model": "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507",
            "max_tokens": 800,
            "top_p": 0.9,
            "top_k": 40,
            "presence_penalty": 0.3,
            "frequency_penalty": 0.2,
            "temperature": 0.8,
            "messages": [
                {
                    "role": "system",
                    "content": system_prompt
                },
                {
                    "role": "user",
                    "content": user_prompt
                }
            ]
        }
        
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": f"Bearer {FIREWORKS_API_KEY}"
        }
        
        response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=15)
        
        if response.status_code == 200:
            result = response.json()
            enhanced = result.get('choices', [{}])[0].get('message', {}).get('content', '')
            if enhanced:
                # Extract only the designer's final prompt from the response
                if "DESIGNER'S FINAL PROMPT:" in enhanced:
                    parts = enhanced.split("DESIGNER'S FINAL PROMPT:")
                    if len(parts) > 1:
                        return parts[1].strip()
                return enhanced
        
        return user_input
        
    except Exception as e:
        print(f"Team collaboration error: {str(e)}")
        return user_input

def upload_image_to_hosting(image):
    """
    Upload image to multiple hosting services with fallback
    """
    # Method 1: Try imgbb.com
    try:
        buffered = BytesIO()
        image.save(buffered, format="PNG")
        buffered.seek(0)
        img_base64 = base64.b64encode(buffered.getvalue()).decode()
        
        response = requests.post(
            "https://api.imgbb.com/1/upload",
            data={
                'key': '6d207e02198a847aa98d0a2a901485a5',
                'image': img_base64,
            }
        )
        
        if response.status_code == 200:
            data = response.json()
            if data.get('success'):
                return data['data']['url']
    except:
        pass
    
    # Method 2: Try 0x0.st
    try:
        buffered = BytesIO()
        image.save(buffered, format="PNG")
        buffered.seek(0)
        
        files = {'file': ('image.png', buffered, 'image/png')}
        response = requests.post("https://0x0.st", files=files)
        
        if response.status_code == 200:
            return response.text.strip()
    except:
        pass
    
    # Method 3: Fallback to base64
    buffered = BytesIO()
    image.save(buffered, format="PNG")
    buffered.seek(0)
    img_base64 = base64.b64encode(buffered.getvalue()).decode()
    return f"data:image/png;base64,{img_base64}"

def process_product_design(prompt, enhance_prompt_flag, image1, image2=None):
    """
    Process product design with team collaboration approach
    """
    if not os.getenv('REPLICATE_API_TOKEN'):
        return None, prompt, "Please set REPLICATE_API_TOKEN", ""
    
    try:
        # Store team discussion for display
        team_discussion = ""
        
        # Enhance prompt with team collaboration if requested
        final_prompt = prompt
        if enhance_prompt_flag:
            # This will contain the full team discussion
            full_response = enhance_prompt_with_team(prompt)
            final_prompt = full_response
            
            # Store the discussion for display
            team_discussion = f"🎬 **Director β†’ Engineer β†’ Designer Collaboration**\n\n{full_response}"
        
        # Prepare input for model
        model_input = {
            "prompt": final_prompt
        }
        
        # Add style reference images if provided
        if image1 or image2:
            image_urls = []
            
            if image1:
                url1 = upload_image_to_hosting(image1)
                image_urls.append(url1)
            
            if image2:
                url2 = upload_image_to_hosting(image2)
                image_urls.append(url2)
            
            model_input["image_input"] = image_urls
            status_msg = "🏭 Product design generated with style references!"
        else:
            status_msg = "🏭 Product design generated from concept!"
        
        # Run model
        output = replicate.run(
            "google/nano-banana",
            input=model_input
        )
        
        if output is None:
            return None, final_prompt, "No output received", team_discussion
        
        # Get the generated image
        try:
            if hasattr(output, 'read'):
                img_data = output.read()
                img = Image.open(BytesIO(img_data))
                return img, final_prompt, status_msg, team_discussion
        except:
            pass
        
        try:
            if hasattr(output, 'url'):
                output_url = output.url()
                response = requests.get(output_url, timeout=30)
                if response.status_code == 200:
                    img = Image.open(BytesIO(response.content))
                    return img, final_prompt, status_msg, team_discussion
        except:
            pass
        
        output_url = None
        if isinstance(output, str):
            output_url = output
        elif isinstance(output, list) and len(output) > 0:
            output_url = output[0]
        
        if output_url:
            response = requests.get(output_url, timeout=30)
            if response.status_code == 200:
                img = Image.open(BytesIO(response.content))
                return img, final_prompt, status_msg, team_discussion
        
        return None, final_prompt, "Could not process output", team_discussion
        
    except Exception as e:
        error_msg = str(e)
        if "image_input" in error_msg.lower():
            return None, prompt, "Note: Model may require at least one image.", ""
        return None, prompt, f"Error: {error_msg[:100]}", ""

# Enhanced CSS for Product Design focus
css = """
.gradio-container {
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    min-height: 100vh;
}
.header-container {
    background: linear-gradient(135deg, #434343 0%, #000000 100%);
    padding: 2.5rem;
    border-radius: 24px;
    margin-bottom: 2.5rem;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}
.logo-text {
    font-size: 3.5rem;
    font-weight: 900;
    color: white;
    text-align: center;
    margin: 0;
    letter-spacing: -2px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
.subtitle {
    color: #e0e0e0;
    text-align: center;
    font-size: 1.2rem;
    margin-top: 0.5rem;
    font-weight: 300;
}
.mode-indicator {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 0.8rem 1.5rem;
    margin-top: 1rem;
    text-align: center;
    font-weight: 600;
    color: #ffd700;
    border: 1px solid rgba(255, 215, 0, 0.3);
}
.main-content {
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(20px);
    border-radius: 24px;
    padding: 2.5rem;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
.gr-button-primary {
    background: linear-gradient(135deg, #434343 0%, #000000 100%) !important;
    border: 2px solid #ffd700 !important;
    color: white !important;
    font-weight: 700 !important;
    font-size: 1.1rem !important;
    padding: 1.2rem 2rem !important;
    border-radius: 14px !important;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
    text-transform: uppercase;
    letter-spacing: 1px;
    width: 100%;
    margin-top: 1rem !important;
}
.gr-button-primary:hover {
    transform: translateY(-3px) !important;
    box-shadow: 0 15px 40px rgba(255, 215, 0, 0.3) !important;
    background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%) !important;
    color: #000 !important;
}
.gr-input, .gr-textarea {
    background: #ffffff !important;
    border: 2px solid #d0d0d0 !important;
    border-radius: 12px !important;
    color: #2d3436 !important;
    font-size: 1rem !important;
    padding: 0.8rem 1rem !important;
}
.gr-input:focus, .gr-textarea:focus {
    border-color: #434343 !important;
    box-shadow: 0 0 0 4px rgba(67, 67, 67, 0.1) !important;
}
.team-discussion {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    border-radius: 12px;
    padding: 1.5rem;
    margin-top: 1rem;
    border-left: 4px solid #434343;
    font-family: 'Monaco', 'Courier New', monospace;
    font-size: 0.9rem;
    max-height: 400px;
    overflow-y: auto;
}
.info-box {
    background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%);
    border-radius: 12px;
    padding: 1rem;
    margin-bottom: 1rem;
    border-left: 4px solid #000;
    color: #000;
    font-weight: 500;
}
.enhanced-prompt-box {
    background: #f0f0f0;
    border-radius: 12px;
    padding: 1rem;
    margin-top: 1rem;
    border-left: 4px solid #ffd700;
}
footer {
    display: none !important;
}
"""

with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
    with gr.Column(elem_classes="header-container"):
        gr.HTML("""
            <h1 class="logo-text">🏭 PRODUCT DESIGN STUDIO</h1>
            <p class="subtitle">AI-Powered Industrial & Product Design System</p>
            <div class="mode-indicator">
                πŸ‘₯ Team Collaboration: Director β†’ Engineer β†’ Designer
            </div>
        """)

    with gr.Column(elem_classes="main-content"):
        # Info box
        gr.HTML("""
            <div class="info-box">
                <strong>🎯 Professional Product Design Process:</strong><br>
                β€’ <b>Director:</b> Establishes design vision and requirements<br>
                β€’ <b>Engineer:</b> Researches technology and feasibility<br>
                β€’ <b>Designer:</b> Creates detailed visual specifications<br>
                β€’ Focus: Physical products only (electronics, furniture, vehicles, tools, etc.)
            </div>
        """)
        
        with gr.Row(equal_height=True):
            # Left Column - Inputs
            with gr.Column(scale=1):
                prompt = gr.Textbox(
                    label="Product Concept",
                    placeholder="Examples: 'ergonomic office chair', 'smart home speaker', 'electric bicycle', 'modular kitchen appliance'...",
                    lines=3,
                    value="professional mechanical keyboard",
                    elem_classes="prompt-input"
                )
                
                enhance_prompt_checkbox = gr.Checkbox(
                    label="πŸ‘₯ Enable Team Collaboration (Director + Engineer + Designer)",
                    value=True,
                    info="Activates multi-role design process with technical research"
                )
                
                with gr.Row(equal_height=True):
                    image1 = gr.Image(
                        label="Style/Material Reference 1",
                        type="pil",
                        height=200,
                        elem_classes="image-container"
                    )
                    image2 = gr.Image(
                        label="Style/Material Reference 2",
                        type="pil",
                        height=200,
                        elem_classes="image-container"
                    )
                
                generate_btn = gr.Button(
                    "Generate Product Design 🏭",
                    variant="primary",
                    size="lg"
                )
            
            # Right Column - Output
            with gr.Column(scale=1):
                output_image = gr.Image(
                    label="Product Design Visualization",
                    type="pil",
                    height=420,
                    elem_classes="image-container"
                )
                
                enhanced_prompt_display = gr.Textbox(
                    label="Final Design Specifications",
                    interactive=False,
                    lines=4,
                    elem_classes="enhanced-prompt-box",
                    visible=True,
                    value="Design specifications will appear here..."
                )
                
                status = gr.Textbox(
                    label="Status",
                    interactive=False,
                    lines=1,
                    elem_classes="status-text",
                    value="Ready to design products..."
                )
        
        # Team Discussion Display
        with gr.Row():
            team_discussion_display = gr.Markdown(
                label="Team Collaboration Process",
                value="",
                elem_classes="team-discussion"
            )
    
    # Event handler
    generate_btn.click(
        fn=process_product_design,
        inputs=[prompt, enhance_prompt_checkbox, image1, image2],
        outputs=[output_image, enhanced_prompt_display, status, team_discussion_display]
    )
    
    # Product Design Examples
    gr.Examples(
        examples=[
            ["ergonomic gaming mouse", True, None, None],
            ["portable coffee maker", True, None, None],
            ["modular storage system", True, None, None],
            ["smart bicycle helmet", True, None, None],
            ["industrial power tool", True, None, None],
            ["luxury watch design", True, None, None],
            ["compact home gym equipment", True, None, None],
            ["sustainable packaging solution", True, None, None],
            ["professional camera lens", True, None, None],
            ["electric vehicle charging station", True, None, None],
        ],
        inputs=[prompt, enhance_prompt_checkbox, image1, image2],
        label="Product Design Examples (Team will collaborate on these concepts)"
    )

# Launch
if __name__ == "__main__":
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860
    )