File size: 6,454 Bytes
3519dec 9244761 3519dec 3397572 3519dec 3397572 3519dec 8df44fc 3519dec 8df44fc 3519dec 8df44fc 3519dec 3397572 3519dec 3397572 3519dec 3397572 3519dec 3397572 3519dec 8df44fc 3397572 8df44fc 3397572 8df44fc 3519dec 3397572 3519dec 8df44fc 3519dec 3397572 69c49d3 3397572 3519dec 8df44fc 69c49d3 3519dec 3397572 3519dec 3397572 3519dec 69c49d3 3397572 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 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 |
import gradio as gr
import requests
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 vertical {color_prompt} colored {Phone_type_prompt} back cover featuring a bold {design_prompt} design on the front, hanging on the plain wall. 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 = {}
payload = {
"inputs": full_prompt,
"parameters": {
"negative_prompt": "(worst quality, low quality, lowres, oversaturated, grayscale, bad photo:1.4)",
"num_inference_steps": 30,
"scheduler": "DPMSolverMultistepScheduler"
},
}
error_count = 0
pbar = tqdm(total=None, desc="Loading model")
while True:
response = requests.post(api_url, headers=headers, json=payload)
if response.status_code == 200:
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:
raise Exception(f"API Error: {response.status_code}")
custom_css = """
body {
font-family: 'Poppins', sans-serif;
margin: 0;
padding: 0;
transition: background-color 0.3s, color 0.3s;
}
.light-mode {
background-color: #f8f9fa;
color: #333;
}
.dark-mode {
background-color: #333;
color: #f8f9fa;
}
.gr-markdown-container {
transition: color 0.3s;
}
.light-mode .gr-markdown-container {
color: #333;
}
.dark-mode .gr-markdown-container {
color: #f8f9fa;
}
textarea, input {
padding: 10px;
border-radius: 8px;
border: 2px solid #ccc;
margin-bottom: 10px;
width: 100%;
}
textarea.dark-mode, input.dark-mode {
background-color: #444;
color: #f8f9fa;
border: 2px solid #555;
}
textarea.light-mode, input.light-mode {
background-color: #fff;
color: #333;
}
.output-image {
max-width: 100%;
border-radius: 12px;
margin-top: 20px;
}
/* Neon Button Styling */
button {
font-size: 1.2rem;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
border: none;
color: white;
background: linear-gradient(90deg, red, orange, yellow, green, cyan, blue, violet);
background-size: 400%;
box-shadow: 0 0 10px rgba(255, 255, 255, 0.6),
0 0 20px rgba(255, 255, 255, 0.4),
0 0 40px rgba(255, 255, 255, 0.2);
animation: neon-button 2s infinite;
transition: transform 0.3s;
}
button:hover {
transform: scale(1.1);
box-shadow: 0 0 20px rgba(255, 255, 255, 0.8),
0 0 40px rgba(255, 255, 255, 0.6),
0 0 60px rgba(255, 255, 255, 0.4);
}
/* Neon Animation */
@keyframes neon-button {
0% { background-position: 0%; }
100% { background-position: 400%; }
}
/* Loading Spinner */
#loading-spinner {
display: none;
margin: 20px auto;
border: 6px solid #f3f3f3;
border-top: 6px solid #3498db;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@media screen and (max-width: 768px) {
.gradio-container {
padding: 10px;
}
textarea, input {
font-size: 0.9rem;
}
button {
font-size: 0.9rem;
}
}
@media screen and (min-width: 769px) {
.gradio-container {
padding: 20px;
max-width: 1200px;
margin: auto;
}
}
"""
custom_js = """
<script>
document.addEventListener('DOMContentLoaded', function () {
const toggleButton = document.createElement('button');
toggleButton.textContent = 'Toggle Light/Dark Mode';
toggleButton.style.marginBottom = '20px';
toggleButton.onclick = () => {
document.body.classList.toggle('dark-mode');
document.body.classList.toggle('light-mode');
};
document.body.prepend(toggleButton);
document.body.classList.add('light-mode'); // Default to light mode
// Dynamic Welcome Message
const welcomeMessage = document.createElement('h2');
const currentHour = new Date().getHours();
let greeting = "Welcome!";
if (currentHour < 12) greeting = "Good Morning!";
else if (currentHour < 18) greeting = "Good Afternoon!";
else greeting = "Good Evening!";
welcomeMessage.textContent = greeting + " Customize your phone cover!";
welcomeMessage.style.textAlign = "center";
document.body.prepend(welcomeMessage);
// Add spinner toggle on button click
const generateButton = document.querySelector('button:contains("Generate Design")');
const spinner = document.getElementById('loading-spinner');
generateButton.onclick = () => {
spinner.style.display = 'block';
setTimeout(() => spinner.style.display = 'none', 5000); // Mock loading
};
});
</script>
"""
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.
""",
elem_id="gr-markdown-container"
)
with gr.Row(elem_id="responsive-row"):
with gr.Column(scale=1, min_width=300):
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")
spinner = gr.HTML('<div id="loading-spinner"></div>')
with gr.Column(scale=1, min_width=300):
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)
|