Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 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
|
|
@@ -15,13 +14,11 @@ def infer(color_prompt, Phone_type_prompt, design_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,
|
| 25 |
"num_inference_steps": 30,
|
| 26 |
"scheduler": "DPMSolverMultistepScheduler"
|
| 27 |
},
|
|
@@ -30,11 +27,8 @@ def infer(color_prompt, Phone_type_prompt, design_prompt):
|
|
| 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)
|
|
@@ -43,10 +37,8 @@ def infer(color_prompt, Phone_type_prompt, design_prompt):
|
|
| 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 |
-
# Light and Dark Mode CSS
|
| 50 |
custom_css = """
|
| 51 |
body {
|
| 52 |
font-family: 'Poppins', sans-serif;
|
|
@@ -62,45 +54,87 @@ body {
|
|
| 62 |
background-color: #333;
|
| 63 |
color: #f8f9fa;
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
padding: 10px 20px;
|
| 68 |
-
border-radius: 5px;
|
| 69 |
-
cursor: pointer;
|
| 70 |
-
transition: 0.3s all;
|
| 71 |
}
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
color: #fff;
|
| 75 |
}
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
color: #fff;
|
| 79 |
}
|
| 80 |
-
textarea {
|
| 81 |
padding: 10px;
|
| 82 |
border-radius: 8px;
|
| 83 |
-
transition: 0.3s;
|
| 84 |
-
}
|
| 85 |
-
textarea.light-mode {
|
| 86 |
-
background-color: #fff;
|
| 87 |
-
color: #333;
|
| 88 |
border: 2px solid #ccc;
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
-
textarea.dark-mode {
|
| 91 |
background-color: #444;
|
| 92 |
color: #f8f9fa;
|
| 93 |
border: 2px solid #555;
|
| 94 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
.output-image {
|
| 96 |
max-width: 100%;
|
| 97 |
border-radius: 12px;
|
| 98 |
-
border: 2px solid #007bff;
|
| 99 |
margin-top: 20px;
|
| 100 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
"""
|
| 102 |
|
| 103 |
-
# JavaScript to Toggle Modes
|
| 104 |
custom_js = """
|
| 105 |
<script>
|
| 106 |
document.addEventListener('DOMContentLoaded', function () {
|
|
@@ -117,22 +151,22 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
| 117 |
</script>
|
| 118 |
"""
|
| 119 |
|
| 120 |
-
# Create the Gradio interface
|
| 121 |
with gr.Blocks(css=custom_css) as interface:
|
| 122 |
gr.HTML(custom_js)
|
| 123 |
gr.Markdown(
|
| 124 |
"""
|
| 125 |
# **AI Phone Cover Designer**
|
| 126 |
Create custom designs for your brand with AI. Specify color, style, and design details.
|
| 127 |
-
"""
|
|
|
|
| 128 |
)
|
| 129 |
-
with gr.Row():
|
| 130 |
-
with gr.Column():
|
| 131 |
color_prompt = gr.Textbox(label="Color", placeholder="E.g., Red", elem_id="component-1")
|
| 132 |
Back_cover_prompt = gr.Textbox(label="Mobile type", placeholder="E.g., iPhone, Samsung", elem_id="component-2")
|
| 133 |
design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Bold stripes with geometric patterns", elem_id="component-3")
|
| 134 |
generate_button = gr.Button("Generate Design")
|
| 135 |
-
with gr.Column():
|
| 136 |
output = gr.Image(label="Generated Design", elem_id="output-image")
|
| 137 |
|
| 138 |
generate_button.click(infer, inputs=[color_prompt, Back_cover_prompt, design_prompt], outputs=output)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
from tqdm import tqdm
|
|
|
|
| 14 |
|
| 15 |
print("Generating image with prompt:", full_prompt)
|
| 16 |
api_url = f"https://api-inference.huggingface.co/models/{repo}"
|
| 17 |
+
headers = {}
|
|
|
|
|
|
|
| 18 |
payload = {
|
| 19 |
"inputs": full_prompt,
|
| 20 |
"parameters": {
|
| 21 |
+
"negative_prompt": "(worst quality, low quality, lowres, oversaturated, grayscale, bad photo:1.4)",
|
| 22 |
"num_inference_steps": 30,
|
| 23 |
"scheduler": "DPMSolverMultistepScheduler"
|
| 24 |
},
|
|
|
|
| 27 |
error_count = 0
|
| 28 |
pbar = tqdm(total=None, desc="Loading model")
|
| 29 |
while True:
|
|
|
|
| 30 |
response = requests.post(api_url, headers=headers, json=payload)
|
|
|
|
| 31 |
if response.status_code == 200:
|
|
|
|
| 32 |
return Image.open(BytesIO(response.content))
|
| 33 |
elif response.status_code == 503:
|
| 34 |
time.sleep(1)
|
|
|
|
| 37 |
time.sleep(1)
|
| 38 |
error_count += 1
|
| 39 |
else:
|
|
|
|
| 40 |
raise Exception(f"API Error: {response.status_code}")
|
| 41 |
|
|
|
|
| 42 |
custom_css = """
|
| 43 |
body {
|
| 44 |
font-family: 'Poppins', sans-serif;
|
|
|
|
| 54 |
background-color: #333;
|
| 55 |
color: #f8f9fa;
|
| 56 |
}
|
| 57 |
+
.gr-markdown-container {
|
| 58 |
+
transition: color 0.3s;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
}
|
| 60 |
+
.light-mode .gr-markdown-container {
|
| 61 |
+
color: #333;
|
|
|
|
| 62 |
}
|
| 63 |
+
.dark-mode .gr-markdown-container {
|
| 64 |
+
color: #f8f9fa;
|
|
|
|
| 65 |
}
|
| 66 |
+
textarea, input {
|
| 67 |
padding: 10px;
|
| 68 |
border-radius: 8px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
border: 2px solid #ccc;
|
| 70 |
+
margin-bottom: 10px;
|
| 71 |
+
width: 100%;
|
| 72 |
}
|
| 73 |
+
textarea.dark-mode, input.dark-mode {
|
| 74 |
background-color: #444;
|
| 75 |
color: #f8f9fa;
|
| 76 |
border: 2px solid #555;
|
| 77 |
}
|
| 78 |
+
textarea.light-mode, input.light-mode {
|
| 79 |
+
background-color: #fff;
|
| 80 |
+
color: #333;
|
| 81 |
+
}
|
| 82 |
.output-image {
|
| 83 |
max-width: 100%;
|
| 84 |
border-radius: 12px;
|
|
|
|
| 85 |
margin-top: 20px;
|
| 86 |
}
|
| 87 |
+
|
| 88 |
+
/* Neon Button Styling */
|
| 89 |
+
button {
|
| 90 |
+
font-size: 1.2rem;
|
| 91 |
+
padding: 10px 20px;
|
| 92 |
+
border-radius: 5px;
|
| 93 |
+
cursor: pointer;
|
| 94 |
+
border: none;
|
| 95 |
+
color: white;
|
| 96 |
+
background: linear-gradient(90deg, red, orange, yellow, green, cyan, blue, violet);
|
| 97 |
+
background-size: 400%;
|
| 98 |
+
box-shadow: 0 0 10px rgba(255, 255, 255, 0.6),
|
| 99 |
+
0 0 20px rgba(255, 255, 255, 0.4),
|
| 100 |
+
0 0 40px rgba(255, 255, 255, 0.2);
|
| 101 |
+
animation: neon-button 2s infinite;
|
| 102 |
+
transition: transform 0.3s;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
button:hover {
|
| 106 |
+
transform: scale(1.1);
|
| 107 |
+
box-shadow: 0 0 20px rgba(255, 255, 255, 0.8),
|
| 108 |
+
0 0 40px rgba(255, 255, 255, 0.6),
|
| 109 |
+
0 0 60px rgba(255, 255, 255, 0.4);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
/* Neon Animation */
|
| 113 |
+
@keyframes neon-button {
|
| 114 |
+
0% { background-position: 0%; }
|
| 115 |
+
100% { background-position: 400%; }
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
@media screen and (max-width: 768px) {
|
| 119 |
+
.gradio-container {
|
| 120 |
+
padding: 10px;
|
| 121 |
+
}
|
| 122 |
+
textarea, input {
|
| 123 |
+
font-size: 0.9rem;
|
| 124 |
+
}
|
| 125 |
+
button {
|
| 126 |
+
font-size: 0.9rem;
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
@media screen and (min-width: 769px) {
|
| 130 |
+
.gradio-container {
|
| 131 |
+
padding: 20px;
|
| 132 |
+
max-width: 1200px;
|
| 133 |
+
margin: auto;
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
"""
|
| 137 |
|
|
|
|
| 138 |
custom_js = """
|
| 139 |
<script>
|
| 140 |
document.addEventListener('DOMContentLoaded', function () {
|
|
|
|
| 151 |
</script>
|
| 152 |
"""
|
| 153 |
|
|
|
|
| 154 |
with gr.Blocks(css=custom_css) as interface:
|
| 155 |
gr.HTML(custom_js)
|
| 156 |
gr.Markdown(
|
| 157 |
"""
|
| 158 |
# **AI Phone Cover Designer**
|
| 159 |
Create custom designs for your brand with AI. Specify color, style, and design details.
|
| 160 |
+
""",
|
| 161 |
+
elem_id="gr-markdown-container"
|
| 162 |
)
|
| 163 |
+
with gr.Row(elem_id="responsive-row"):
|
| 164 |
+
with gr.Column(scale=1, min_width=300):
|
| 165 |
color_prompt = gr.Textbox(label="Color", placeholder="E.g., Red", elem_id="component-1")
|
| 166 |
Back_cover_prompt = gr.Textbox(label="Mobile type", placeholder="E.g., iPhone, Samsung", elem_id="component-2")
|
| 167 |
design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Bold stripes with geometric patterns", elem_id="component-3")
|
| 168 |
generate_button = gr.Button("Generate Design")
|
| 169 |
+
with gr.Column(scale=1, min_width=300):
|
| 170 |
output = gr.Image(label="Generated Design", elem_id="output-image")
|
| 171 |
|
| 172 |
generate_button.click(infer, inputs=[color_prompt, Back_cover_prompt, design_prompt], outputs=output)
|