Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
from tqdm import tqdm
|
7 |
+
import time
|
8 |
+
|
9 |
+
repo = "artificialguybr/TshirtDesignRedmond-V2"
|
10 |
+
|
11 |
+
def infer(color_prompt, Phone_type_prompt, design_prompt):
|
12 |
+
prompt = (
|
13 |
+
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.")
|
14 |
+
full_prompt = f"{prompt}"
|
15 |
+
|
16 |
+
print("Generating image with prompt:", full_prompt)
|
17 |
+
api_url = f"https://api-inference.huggingface.co/models/{repo}"
|
18 |
+
headers = {
|
19 |
+
# "Authorization": f"Bearer {token}" # Uncomment and use your Hugging Face API token
|
20 |
+
}
|
21 |
+
payload = {
|
22 |
+
"inputs": full_prompt,
|
23 |
+
"parameters": {
|
24 |
+
"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)",
|
25 |
+
"num_inference_steps": 30,
|
26 |
+
"scheduler": "DPMSolverMultistepScheduler"
|
27 |
+
},
|
28 |
+
}
|
29 |
+
|
30 |
+
error_count = 0
|
31 |
+
pbar = tqdm(total=None, desc="Loading model")
|
32 |
+
while True:
|
33 |
+
print("Sending request to API...")
|
34 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
35 |
+
print("API response status code:", response.status_code)
|
36 |
+
if response.status_code == 200:
|
37 |
+
print("Image generation successful!")
|
38 |
+
return Image.open(BytesIO(response.content))
|
39 |
+
elif response.status_code == 503:
|
40 |
+
time.sleep(1)
|
41 |
+
pbar.update(1)
|
42 |
+
elif response.status_code == 500 and error_count < 5:
|
43 |
+
time.sleep(1)
|
44 |
+
error_count += 1
|
45 |
+
else:
|
46 |
+
print("API Error:", response.status_code)
|
47 |
+
raise Exception(f"API Error: {response.status_code}")
|
48 |
+
|
49 |
+
# Customized CSS and JS for Enhanced UI
|
50 |
+
custom_css = """
|
51 |
+
body {
|
52 |
+
font-family: 'Poppins', sans-serif;
|
53 |
+
background-color: #f8f9fa;
|
54 |
+
margin: 0;
|
55 |
+
padding: 0;
|
56 |
+
}
|
57 |
+
|
58 |
+
#component-1, #component-2, #component-3 {
|
59 |
+
margin-bottom: 20px;
|
60 |
+
}
|
61 |
+
|
62 |
+
.gradio-container {
|
63 |
+
width: 90%;
|
64 |
+
max-width: 1200px;
|
65 |
+
margin: auto;
|
66 |
+
padding: 20px;
|
67 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
68 |
+
border-radius: 12px;
|
69 |
+
background: white;
|
70 |
+
position: relative;
|
71 |
+
}
|
72 |
+
|
73 |
+
button {
|
74 |
+
font-size: 1.2rem;
|
75 |
+
padding: 10px 20px;
|
76 |
+
background-color: #007bff;
|
77 |
+
border: none;
|
78 |
+
color: white;
|
79 |
+
border-radius: 5px;
|
80 |
+
cursor: pointer;
|
81 |
+
transition: 0.3s all;
|
82 |
+
box-shadow: 0px 8px 15px rgba(0, 123, 255, 0.2);
|
83 |
+
}
|
84 |
+
|
85 |
+
button:hover {
|
86 |
+
background-color: #0056b3;
|
87 |
+
box-shadow: 0px 15px 20px rgba(0, 123, 255, 0.4);
|
88 |
+
transform: translateY(-2px);
|
89 |
+
}
|
90 |
+
|
91 |
+
textarea {
|
92 |
+
border: 2px solid #ccc;
|
93 |
+
border-radius: 8px;
|
94 |
+
padding: 10px;
|
95 |
+
font-size: 1rem;
|
96 |
+
}
|
97 |
+
|
98 |
+
textarea:focus {
|
99 |
+
border-color: #007bff;
|
100 |
+
}
|
101 |
+
|
102 |
+
.gr-input {
|
103 |
+
padding: 10px;
|
104 |
+
border: 2px solid #ccc;
|
105 |
+
border-radius: 8px;
|
106 |
+
transition: 0.3s;
|
107 |
+
}
|
108 |
+
|
109 |
+
.gr-input:focus {
|
110 |
+
border-color: #007bff;
|
111 |
+
outline: none;
|
112 |
+
}
|
113 |
+
|
114 |
+
.output-image {
|
115 |
+
max-width: 100%;
|
116 |
+
border-radius: 12px;
|
117 |
+
border: 2px solid #007bff;
|
118 |
+
}
|
119 |
+
|
120 |
+
.flashy-btn {
|
121 |
+
animation: flash 1.5s infinite;
|
122 |
+
}
|
123 |
+
|
124 |
+
@keyframes flash {
|
125 |
+
0%, 100% {
|
126 |
+
box-shadow: 0 0 10px #007bff, 0 0 40px #007bff, 0 0 80px #007bff;
|
127 |
+
}
|
128 |
+
50% {
|
129 |
+
box-shadow: 0 0 20px #0056b3, 0 0 50px #0056b3, 0 0 100px #0056b3;
|
130 |
+
}
|
131 |
+
}
|
132 |
+
"""
|
133 |
+
|
134 |
+
custom_js = """
|
135 |
+
<script>
|
136 |
+
document.addEventListener('DOMContentLoaded', function () {
|
137 |
+
const button = document.querySelector('button');
|
138 |
+
button.addEventListener('mouseenter', () => {
|
139 |
+
button.classList.add('flashy-btn');
|
140 |
+
});
|
141 |
+
button.addEventListener('mouseleave', () => {
|
142 |
+
button.classList.remove('flashy-btn');
|
143 |
+
});
|
144 |
+
});
|
145 |
+
</script>
|
146 |
+
"""
|
147 |
+
|
148 |
+
# Create the Gradio interface
|
149 |
+
with gr.Blocks(css=custom_css) as interface:
|
150 |
+
gr.HTML(custom_js)
|
151 |
+
gr.Markdown(
|
152 |
+
"""
|
153 |
+
# **AI Phone Cover Designer**
|
154 |
+
Create custom designs for your brand with AI. Specify color, style, and design details.
|
155 |
+
"""
|
156 |
+
)
|
157 |
+
with gr.Row():
|
158 |
+
with gr.Column():
|
159 |
+
color_prompt = gr.Textbox(label="Color", placeholder="E.g., Red", elem_id="component-1")
|
160 |
+
Back_cover_prompt = gr.Textbox(label="Mobile type", placeholder="E.g., iPhone, Samsung", elem_id="component-2")
|
161 |
+
design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Bold stripes with geometric patterns", elem_id="component-3")
|
162 |
+
generate_button = gr.Button("Generate Design")
|
163 |
+
with gr.Column():
|
164 |
+
output = gr.Image(label="Generated Design", elem_id="output-image")
|
165 |
+
|
166 |
+
generate_button.click(infer, inputs=[color_prompt, Back_cover_prompt, design_prompt], outputs=output)
|
167 |
+
|
168 |
+
# Launch the app
|
169 |
+
interface.launch(debug=True)
|