|
import gradio as gr |
|
import requests |
|
import os |
|
from PIL import Image |
|
from io import BytesIO |
|
from tqdm import tqdm |
|
import time |
|
|
|
|
|
repo = "artificialguybr/TshirtDesignRedmond-V2" |
|
trigger_word = "T shirt design, TshirtDesignAF, " |
|
|
|
|
|
def generate_image(a, color_prompt, dress_type_prompt, design_prompt, text, shadows): |
|
prompt_parts = [ |
|
a, |
|
color_prompt, |
|
dress_type_prompt, |
|
design_prompt, |
|
"hangs effortlessly on a plain grey wall, its simplicity transformed by bold", |
|
] |
|
|
|
|
|
if text: |
|
typography = "contemporary typography reading" |
|
contrast = "The contrast between the text and the calm background creates a striking visual" |
|
prompt_parts.extend([typography, text, contrast]) |
|
|
|
prompt_parts.append(shadows) |
|
|
|
|
|
prompt = " ".join([part for part in prompt_parts if part]) |
|
|
|
print("Generating image with prompt:", prompt) |
|
api_url = f"https://api-inference.huggingface.co/models/{repo}" |
|
|
|
headers = { |
|
|
|
} |
|
full_prompt = f"{prompt} {trigger_word}" |
|
payload = { |
|
"inputs": full_prompt, |
|
"parameters": { |
|
"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)", |
|
"num_inference_steps": 30, |
|
"scheduler": "DPMSolverMultistepScheduler" |
|
}, |
|
} |
|
|
|
error_count = 0 |
|
pbar = tqdm(total=None, desc="Loading model") |
|
while True: |
|
print("Sending request to API...") |
|
response = requests.post(api_url, headers=headers, json=payload) |
|
print("API response status code:", response.status_code) |
|
if response.status_code == 200: |
|
print("Image generation successful!") |
|
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: |
|
print("API Error:", response.status_code) |
|
raise Exception(f"API Error: {response.status_code}") |
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_image, |
|
inputs=[ |
|
gr.Textbox(visible=False, placeholder="Hidden Part 1"), |
|
gr.Textbox(lines=1, placeholder="Color Prompt"), |
|
gr.Textbox(lines=1, placeholder="Dress Type Prompt"), |
|
gr.Textbox(lines=2, placeholder="Design Prompt"), |
|
gr.Textbox(lines=1, placeholder="Text (Optional)"), |
|
gr.Textbox(visible=False, placeholder="Hidden Part 5"), |
|
gr.Textbox(visible=False, placeholder="Hidden Part 6"), |
|
gr.Textbox(visible=False, placeholder="Hidden Part 8"), |
|
gr.Textbox(visible=False, placeholder="Hidden Part 9"), |
|
], |
|
outputs="image", |
|
title="Clothe Designs to use in our img2img model", |
|
description="Make designs for your clothes", |
|
examples=[["a part", "Red", "T-shirt", "Simple design", "Stylish Text"]] |
|
) |
|
|
|
print("Launching Gradio interface...") |
|
iface.launch() |
|
|