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; } .avatar-container { text-align: center; margin-bottom: 20px; } .avatar-container img { border-radius: 50%; width: 150px; height: 150px; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.3); animation: bounce 2s infinite; } @keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } } """ custom_js = """ """ 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") 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)