Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
# Defining the repository information and the trigger word
|
| 10 |
+
repo = "artificialguybr/TshirtDesignRedmond-V2"
|
| 11 |
+
trigger_word = "T shirt design, TshirtDesignAF, "
|
| 12 |
+
|
| 13 |
+
# Function to generate image based on the prompt
|
| 14 |
+
def generate_image(a, color_prompt, dress_type_prompt, design_prompt, typography, text, contrast, shadows):
|
| 15 |
+
prompt_parts = [a, color_prompt, dress_type_prompt, design_prompt]
|
| 16 |
+
|
| 17 |
+
# Optional parts
|
| 18 |
+
if text:
|
| 19 |
+
prompt_parts.extend([typography, text, contrast])
|
| 20 |
+
|
| 21 |
+
prompt_parts.append(shadows)
|
| 22 |
+
|
| 23 |
+
# Combine all parts into a full prompt
|
| 24 |
+
prompt = " ".join([part for part in prompt_parts if part])
|
| 25 |
+
|
| 26 |
+
print("Generating image with prompt:", prompt)
|
| 27 |
+
api_url = f"https://api-inference.huggingface.co/models/{repo}"
|
| 28 |
+
#token = os.getenv("API_TOKEN") # Uncomment and use your Hugging Face API token
|
| 29 |
+
headers = {
|
| 30 |
+
#"Authorization": f"Bearer {token}"
|
| 31 |
+
}
|
| 32 |
+
full_prompt = f"{prompt} {trigger_word}"
|
| 33 |
+
payload = {
|
| 34 |
+
"inputs": full_prompt,
|
| 35 |
+
"parameters": {
|
| 36 |
+
"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)",
|
| 37 |
+
"num_inference_steps": 30,
|
| 38 |
+
"scheduler": "DPMSolverMultistepScheduler"
|
| 39 |
+
},
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
error_count = 0
|
| 43 |
+
pbar = tqdm(total=None, desc="Loading model")
|
| 44 |
+
while True:
|
| 45 |
+
print("Sending request to API...")
|
| 46 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
| 47 |
+
print("API response status code:", response.status_code)
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
print("Image generation successful!")
|
| 50 |
+
return Image.open(BytesIO(response.content)) # Changed to match the first code
|
| 51 |
+
elif response.status_code == 503:
|
| 52 |
+
time.sleep(1)
|
| 53 |
+
pbar.update(1)
|
| 54 |
+
elif response.status_code == 500 and error_count < 5:
|
| 55 |
+
time.sleep(1)
|
| 56 |
+
error_count += 1
|
| 57 |
+
else:
|
| 58 |
+
print("API Error:", response.status_code)
|
| 59 |
+
raise Exception(f"API Error: {response.status_code}")
|
| 60 |
+
|
| 61 |
+
# Gradio Interface
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=generate_image,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Textbox(visible=False, placeholder="Hidden Part 1"), # a (hidden)
|
| 66 |
+
gr.Textbox(lines=1, placeholder="Color Prompt"), # color_prompt
|
| 67 |
+
gr.Textbox(lines=1, placeholder="Dress Type Prompt"), # dress_type_prompt
|
| 68 |
+
gr.Textbox(lines=2, placeholder="Design Prompt"), # design_prompt
|
| 69 |
+
gr.Textbox(visible=False, placeholder="Hidden Part 5"), # Typography (hidden)
|
| 70 |
+
gr.Textbox(lines=1, optional=True, placeholder="Text"), # text
|
| 71 |
+
gr.Textbox(visible=False, placeholder="Hidden Part 6"), # Contrast (hidden)
|
| 72 |
+
gr.Textbox(visible=False, placeholder="Hidden Part 9"), # Shadows (hidden)
|
| 73 |
+
],
|
| 74 |
+
outputs="image",
|
| 75 |
+
title="Clothe Designs to use in our img2img model",
|
| 76 |
+
description="Make designs for your clothes",
|
| 77 |
+
examples=[["a part", "Red", "T-shirt", "Simple design", "Bold Typography", "Stylish Text", "High Contrast", "Soft Shadows"]]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
print("Launching Gradio interface...")
|
| 81 |
+
iface.launch()
|