Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
def generate_image(prompt):
|
14 |
+
print("Generating image with prompt:", prompt)
|
15 |
+
api_url = f"https://api-inference.huggingface.co/models/{repo}"
|
16 |
+
#token = os.getenv("API_TOKEN") # Uncomment and use your Hugging Face API token
|
17 |
+
headers = {
|
18 |
+
#"Authorization": f"Bearer {token}"
|
19 |
+
}
|
20 |
+
full_prompt = f"{prompt} {trigger_word}"
|
21 |
+
payload = {
|
22 |
+
"inputs": full_prompt,
|
23 |
+
"parameters": {
|
24 |
+
"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)",
|
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 |
+
print("Sending request to API...")
|
34 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
35 |
+
print("API response status code:", response.status_code)
|
36 |
+
if response.status_code == 200:
|
37 |
+
print("Image generation successful!")
|
38 |
+
return Image.open(BytesIO(response.content)) # Changed to match the first code
|
39 |
+
elif response.status_code == 503:
|
40 |
+
time.sleep(1)
|
41 |
+
pbar.update(1)
|
42 |
+
elif response.status_code == 500 and error_count < 5:
|
43 |
+
time.sleep(1)
|
44 |
+
error_count += 1
|
45 |
+
else:
|
46 |
+
print("API Error:", response.status_code)
|
47 |
+
raise Exception(f"API Error: {response.status_code}")
|
48 |
+
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=generate_image,
|
51 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your prompt here..."),
|
52 |
+
outputs="image",
|
53 |
+
title="TShirt Design XL Image Generator",
|
54 |
+
description="Make designs for your clothes",
|
55 |
+
examples=[["Cute Panda"], ["Skull"]]
|
56 |
+
)
|
57 |
+
|
58 |
+
print("Launching Gradio interface...")
|
59 |
+
iface.launch()
|