File size: 5,396 Bytes
3519dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import os
from PIL import Image
from io import BytesIO
from tqdm import tqdm
import time

repo = "artificialguybr/TshirtDesignRedmond-V2"

def infer(color_prompt, Phone_type_prompt, design_prompt):
    prompt = (
        f"A single {color_prompt} colored {Phone_type_prompt} back cover featuring a bold {design_prompt} design on the front. The soft light and shadows, creating a striking contrast against the minimal background, evoking modern sophistication.")
    full_prompt = f"{prompt}"

    print("Generating image with prompt:", full_prompt)
    api_url = f"https://api-inference.huggingface.co/models/{repo}"
    headers = {
        # "Authorization": f"Bearer {token}"  # Uncomment and use your Hugging Face API token
    }
    payload = {
        "inputs": full_prompt,
        "parameters": {
            "negative_prompt": "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (watermark, signature, text font, username, error, logo, words, letters, digits, autograph, trademark, name:1.2), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate, (airbrushed, cartoon, anime, semi-realistic, cgi, render, blender, digital art, manga, amateur:1.3), (3D ,3D Game, 3D Game Scene, 3D Character:1.1), (bad hands, bad anatomy, bad body, bad face, bad teeth, bad arms, bad legs, deformities:1.3)",
            "num_inference_steps": 30,
            "scheduler": "DPMSolverMultistepScheduler"
        },
    }

    error_count = 0
    pbar = tqdm(total=None, desc="Loading model")
    while True:
        print("Sending request to API...")
        response = requests.post(api_url, headers=headers, json=payload)
        print("API response status code:", response.status_code)
        if response.status_code == 200:
            print("Image generation successful!")
            return Image.open(BytesIO(response.content))
        elif response.status_code == 503:
            time.sleep(1)
            pbar.update(1)
        elif response.status_code == 500 and error_count < 5:
            time.sleep(1)
            error_count += 1
        else:
            print("API Error:", response.status_code)
            raise Exception(f"API Error: {response.status_code}")

# Customized CSS and JS for Enhanced UI
custom_css = """
body {
    font-family: 'Poppins', sans-serif;
    background-color: #f8f9fa;
    margin: 0;
    padding: 0;
}

#component-1, #component-2, #component-3 {
    margin-bottom: 20px;
}

.gradio-container {
    width: 90%;
    max-width: 1200px;
    margin: auto;
    padding: 20px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    border-radius: 12px;
    background: white;
    position: relative;
}

button {
    font-size: 1.2rem;
    padding: 10px 20px;
    background-color: #007bff;
    border: none;
    color: white;
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s all;
    box-shadow: 0px 8px 15px rgba(0, 123, 255, 0.2);
}

button:hover {
    background-color: #0056b3;
    box-shadow: 0px 15px 20px rgba(0, 123, 255, 0.4);
    transform: translateY(-2px);
}

textarea {
    border: 2px solid #ccc;
    border-radius: 8px;
    padding: 10px;
    font-size: 1rem;
}

textarea:focus {
    border-color: #007bff;
}

.gr-input {
    padding: 10px;
    border: 2px solid #ccc;
    border-radius: 8px;
    transition: 0.3s;
}

.gr-input:focus {
    border-color: #007bff;
    outline: none;
}

.output-image {
    max-width: 100%;
    border-radius: 12px;
    border: 2px solid #007bff;
}

.flashy-btn {
    animation: flash 1.5s infinite;
}

@keyframes flash {
    0%, 100% {
        box-shadow: 0 0 10px #007bff, 0 0 40px #007bff, 0 0 80px #007bff;
    }
    50% {
        box-shadow: 0 0 20px #0056b3, 0 0 50px #0056b3, 0 0 100px #0056b3;
    }
}
"""

custom_js = """
<script>
document.addEventListener('DOMContentLoaded', function () {
    const button = document.querySelector('button');
    button.addEventListener('mouseenter', () => {
        button.classList.add('flashy-btn');
    });
    button.addEventListener('mouseleave', () => {
        button.classList.remove('flashy-btn');
    });
});
</script>
"""

# Create the Gradio interface
with gr.Blocks(css=custom_css) as interface:
    gr.HTML(custom_js)
    gr.Markdown(
        """
        # **AI Phone Cover Designer**
        Create custom designs for your brand with AI. Specify color, style, and design details.
        """
    )
    with gr.Row():
        with gr.Column():
            color_prompt = gr.Textbox(label="Color", placeholder="E.g., Red", elem_id="component-1")
            Back_cover_prompt = gr.Textbox(label="Mobile type", placeholder="E.g., iPhone, Samsung", elem_id="component-2")
            design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Bold stripes with geometric patterns", elem_id="component-3")
            generate_button = gr.Button("Generate Design")
        with gr.Column():
            output = gr.Image(label="Generated Design", elem_id="output-image")

    generate_button.click(infer, inputs=[color_prompt, Back_cover_prompt, design_prompt], outputs=output)

# Launch the app
interface.launch(debug=True)