Spaces:
Runtime error
Runtime error
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline
|
| 3 |
+
from diffusers.utils import load_image
|
| 4 |
+
from PIL import Image as im
|
| 5 |
+
import requests
|
| 6 |
+
import io
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
|
| 10 |
+
headers = {"Authorization": "Bearer HF_TOKEN"}
|
| 11 |
+
|
| 12 |
+
model_id = "instruction-tuning-sd/cartoonizer"
|
| 13 |
+
pipeline = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
| 14 |
+
model_id, torch_dtype=torch.float16, use_auth_token=True
|
| 15 |
+
).to("cuda")
|
| 16 |
+
|
| 17 |
+
def query(payload):
|
| 18 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 19 |
+
return response.content
|
| 20 |
+
|
| 21 |
+
def cartoonizer(input_img,bg_prompt):
|
| 22 |
+
if input_img is not None:
|
| 23 |
+
data = im.fromarray(input_img)
|
| 24 |
+
data = data.resize((300,300))
|
| 25 |
+
org_image = load_image(data)
|
| 26 |
+
cart_image = pipeline("Cartoonize the following image", image=org_image).images[0]
|
| 27 |
+
if len(bg_prompt) !=0:
|
| 28 |
+
image_bytes = query({
|
| 29 |
+
"inputs": bg_prompt,
|
| 30 |
+
})
|
| 31 |
+
else:
|
| 32 |
+
image_bytes = query({
|
| 33 |
+
"inputs": "orange background image",
|
| 34 |
+
})
|
| 35 |
+
bg_image = im.open(io.BytesIO(image_bytes))
|
| 36 |
+
|
| 37 |
+
return [cart_image,bg_image]
|
| 38 |
+
else:
|
| 39 |
+
gr.Warning("Please upload an Input Image!")
|
| 40 |
+
return [input_img,input_img]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(theme = gr.themes.Citrus()) as cart:
|
| 44 |
+
gr.HTML("""<h1 align="center">Cartoonize your Image with best backgrounds!</h1>""")
|
| 45 |
+
with gr.Tab("Cartoonize"):
|
| 46 |
+
with gr.Row():
|
| 47 |
+
image_input = gr.Image()
|
| 48 |
+
image_output = gr.Image()
|
| 49 |
+
text_img_output = gr.Image()
|
| 50 |
+
|
| 51 |
+
txt_label = gr.Label("Enter your photo frame description:")
|
| 52 |
+
txt_input = gr.Textbox()
|
| 53 |
+
image_btn = gr.Button("Convert")
|
| 54 |
+
|
| 55 |
+
image_btn.click(cartoonizer,inputs = [image_input,txt_input],outputs=[image_output,text_img_output])
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
cart.launch()
|