File size: 2,680 Bytes
c6428de db21836 94a7a4b db21836 94a7a4b db21836 c6428de db21836 c6428de db21836 c6428de db21836 c6428de db21836 c6428de db21836 c6428de db21836 c6428de 732c6ea c6428de 69d2a66 c6428de 94a7a4b c6428de db21836 c6428de 9ebfd06 434bd85 c6428de db21836 |
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 |
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"
def infer(color_prompt, dress_type_prompt, design_prompt, text):
# Improved prompt for higher accuracy
prompt = (
f"A high-quality digital image of a {color_prompt} {dress_type_prompt}, "
f"featuring a {design_prompt} printed in sharp detail. The fabric has realistic texture, "
f"smooth folds, and accurate lighting. The design is perfectly aligned, with natural shadows "
f"and highlights, creating a photorealistic look."
)
print("Generating image with prompt:", prompt)
api_url = f"https://api-inference.huggingface.co/models/{repo}"
headers = {} # If API token needed, add here
payload = {
"inputs": prompt,
"parameters": {
# Optimized negative prompt
"negative_prompt": "low quality, artifacts, distorted, blurry, overexposed, underexposed, unrealistic texture, poor lighting, misaligned print, plastic-like fabric, grainy, washed-out colors, 3D render, cartoon, digital art, watermark, bad anatomy, malformed, cluttered design",
"num_inference_steps": 30,
"scheduler": "EulerAncestralDiscreteScheduler" # Faster & more accurate scheduler
},
}
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}")
# Gradio Interface
iface = gr.Interface(
fn=infer,
inputs=[
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)"),
],
outputs="image",
title="Make your Brand",
description="Generation of clothes",
examples=[["Red", "T-shirt", "Simple design", "Stylish Text"]]
)
print("Launching Gradio interface...")
iface.launch() |