Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
from tqdm import tqdm
|
6 |
+
import time
|
7 |
+
import os
|
8 |
+
|
9 |
+
repo = "artificialguybr/TshirtDesignRedmond-V2"
|
10 |
+
|
11 |
+
# Generate design based on prompts
|
12 |
+
def infer(color_prompt, phone_type_prompt, design_prompt):
|
13 |
+
prompt = (
|
14 |
+
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."
|
15 |
+
)
|
16 |
+
full_prompt = f"{prompt}"
|
17 |
+
|
18 |
+
print("Generating image with prompt:", full_prompt)
|
19 |
+
api_url = f"https://api-inference.huggingface.co/models/{repo}"
|
20 |
+
headers = {}
|
21 |
+
payload = {
|
22 |
+
"inputs": full_prompt,
|
23 |
+
"parameters": {
|
24 |
+
"negative_prompt": "(worst quality, low quality, lowres, oversaturated, grayscale, bad photo:1.4)",
|
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 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
34 |
+
if response.status_code == 200:
|
35 |
+
speech_text = f"Your design is generated with the color '{color_prompt}', mobile type '{phone_type_prompt}', and design '{design_prompt}'."
|
36 |
+
return Image.open(BytesIO(response.content)), speech_text
|
37 |
+
elif response.status_code == 503:
|
38 |
+
time.sleep(1)
|
39 |
+
pbar.update(1)
|
40 |
+
elif response.status_code == 500 and error_count < 5:
|
41 |
+
time.sleep(1)
|
42 |
+
error_count += 1
|
43 |
+
else:
|
44 |
+
raise Exception(f"API Error: {response.status_code}")
|
45 |
+
|
46 |
+
# Function to save the design
|
47 |
+
def save_design(image):
|
48 |
+
file_path = "saved_design.png"
|
49 |
+
image.save(file_path)
|
50 |
+
return f"Design saved as {file_path}!"
|
51 |
+
|
52 |
+
# Custom CSS for animations
|
53 |
+
custom_css = """
|
54 |
+
body {
|
55 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
56 |
+
margin: 0;
|
57 |
+
padding: 0;
|
58 |
+
overflow: hidden;
|
59 |
+
}
|
60 |
+
body::before {
|
61 |
+
content: "";
|
62 |
+
position: fixed;
|
63 |
+
top: 0;
|
64 |
+
left: 0;
|
65 |
+
width: 100%;
|
66 |
+
height: 100%;
|
67 |
+
background: linear-gradient(-45deg, #ff9a9e, #fad0c4, #fbc2eb, #8fd3f4);
|
68 |
+
background-size: 400% 400%;
|
69 |
+
z-index: -1;
|
70 |
+
animation: gradientShift 15s ease infinite;
|
71 |
+
}
|
72 |
+
@keyframes gradientShift {
|
73 |
+
0% { background-position: 0% 50%; }
|
74 |
+
50% { background-position: 100% 50%; }
|
75 |
+
100% { background-position: 0% 50%; }
|
76 |
+
}
|
77 |
+
.carousel {
|
78 |
+
display: flex;
|
79 |
+
overflow-x: auto;
|
80 |
+
scroll-behavior: smooth;
|
81 |
+
}
|
82 |
+
.carousel .image-container {
|
83 |
+
display: inline-block;
|
84 |
+
text-align: center;
|
85 |
+
margin: 0 10px;
|
86 |
+
}
|
87 |
+
.carousel img {
|
88 |
+
max-height: 200px;
|
89 |
+
border-radius: 10px;
|
90 |
+
transition: transform 0.3s;
|
91 |
+
}
|
92 |
+
.carousel img:hover {
|
93 |
+
transform: scale(1.1);
|
94 |
+
}
|
95 |
+
.carousel .caption {
|
96 |
+
margin-top: 5px;
|
97 |
+
font-size: 14px;
|
98 |
+
color: #333;
|
99 |
+
}
|
100 |
+
"""
|
101 |
+
|
102 |
+
# JavaScript for text-to-speech
|
103 |
+
custom_js = """
|
104 |
+
<script>
|
105 |
+
document.addEventListener('DOMContentLoaded', function () {
|
106 |
+
function speak(text) {
|
107 |
+
const synth = window.speechSynthesis;
|
108 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
109 |
+
synth.speak(utterance);
|
110 |
+
}
|
111 |
+
document.addEventListener('gradio_event:output_update', (event) => {
|
112 |
+
const outputText = event.detail?.text || '';
|
113 |
+
if (outputText) {
|
114 |
+
speak(outputText);
|
115 |
+
}
|
116 |
+
});
|
117 |
+
});
|
118 |
+
</script>
|
119 |
+
"""
|
120 |
+
|
121 |
+
with gr.Blocks(css=custom_css) as interface:
|
122 |
+
gr.HTML(custom_js)
|
123 |
+
gr.Markdown("# **AI Phone Cover Designer**")
|
124 |
+
gr.Markdown("Create custom phone covers with AI. Save your designs for future use.")
|
125 |
+
|
126 |
+
with gr.Tabs():
|
127 |
+
with gr.Tab("Home"):
|
128 |
+
gr.Markdown("Welcome to the **AI Phone Cover Designer**! Use the 'Design' tab to start creating custom designs.")
|
129 |
+
|
130 |
+
|
131 |
+
with gr.Tab("Design"):
|
132 |
+
with gr.Row():
|
133 |
+
with gr.Column(scale=1):
|
134 |
+
color_prompt = gr.Textbox(label="Color", placeholder="E.g., Red")
|
135 |
+
phone_type_prompt = gr.Textbox(label="Mobile type", placeholder="E.g., iPhone, Samsung")
|
136 |
+
design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Bold stripes with geometric patterns")
|
137 |
+
chatbot = gr.Chatbot()
|
138 |
+
generate_button = gr.Button("Generate Design")
|
139 |
+
save_button = gr.Button("Save Design")
|
140 |
+
with gr.Column(scale=1):
|
141 |
+
output_image = gr.Image(label="Generated Design")
|
142 |
+
output_message = gr.Textbox(label="AI Assistant Message", interactive=False)
|
143 |
+
|
144 |
+
generate_button.click(
|
145 |
+
infer,
|
146 |
+
inputs=[color_prompt, phone_type_prompt, design_prompt],
|
147 |
+
outputs=[output_image, output_message],
|
148 |
+
)
|
149 |
+
save_button.click(
|
150 |
+
save_design,
|
151 |
+
inputs=[output_image],
|
152 |
+
outputs=output_message,
|
153 |
+
)
|
154 |
+
|
155 |
+
with gr.Tab("About"):
|
156 |
+
gr.Markdown("""
|
157 |
+
## About AI Phone Cover Maker
|
158 |
+
The **AI Phone Cover Maker** is a cutting-edge tool designed to help users create personalized phone cover designs quickly and easily.
|
159 |
+
Powered by AI, it uses advanced image generation techniques to craft unique, high-quality designs for any mobile device.
|
160 |
+
|
161 |
+
### Features:
|
162 |
+
- Create custom designs using simple prompts.
|
163 |
+
- Generate designs for various phone models.
|
164 |
+
- Save your designs for future use.
|
165 |
+
|
166 |
+
Start designing today and bring your creative ideas to life!
|
167 |
+
""")
|
168 |
+
|
169 |
+
interface.launch(debug=True)
|