Spaces:
Sleeping
Sleeping
File size: 2,862 Bytes
945e112 475db54 e08f6b5 945e112 475db54 ad200c9 945e112 c87f7e4 5ea8c0b 945e112 1c4ff8c 357bfd3 589d259 ad200c9 945e112 f1af9e9 945e112 589d259 ad200c9 1c4ff8c 589d259 945e112 5ea8c0b 945e112 5ea8c0b 5e56955 5ea8c0b 7298228 5ea8c0b |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import gradio as gr
import requests
import qrcode
import io
from PIL import Image
import os
# Get the Hugging Face API token from the environment variable
hf_token = os.environ.get("hf_token")
# Function to generate a QR code with transparent fill color
def generate_qr_image(url, image_size):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=60, # Set a default box size
border=0.5, # Set a default border size
)
qr.add_data(url)
qr.make(fit=True)
qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGBA")
# Convert black color pixels to transparent
data = qr_img.getdata()
new_data = []
for item in data:
if item[:3] == (0, 0, 0): # Change black pixels to transparent
new_data.append((0, 0, 0, 0))
else:
new_data.append(item)
qr_img.putdata(new_data)
qr_img = qr_img.resize(image_size)
return qr_img
# Function to query the image from the Hugging Face API
def query_image(text):
API_URL = "https://api-inference.huggingface.co/models/goofyai/3d_render_style_xl"
headers = {"Authorization": f"Bearer {hf_token}"}
response = requests.post(API_URL, headers=headers, json={"inputs": text})
return response.content
# Gradio app function
def generate_image(url, text):
# Generate image from Hugging Face API
image_bytes = query_image(text)
api_image = Image.open(io.BytesIO(image_bytes))
# Generate QR code image
qr_image = generate_qr_image(url, api_image.size)
# Create a blank image with transparency
final_image = Image.new('RGBA', api_image.size, (255, 255, 255, 0))
# Position the QR code in the image
qr_position = ((final_image.width - qr_image.width) // 2, (final_image.height - qr_image.height) // 2)
final_image.paste(qr_image, qr_position, qr_image)
# Composite the API image and the final image with the QR code
final_image = Image.alpha_composite(api_image.convert("RGBA"), final_image)
return final_image
# Inputs and outputs for Gradio interface
inputs = [
gr.Textbox(lines=1, label="URL"),
gr.Textbox(lines=2, label="Prompt for Image"),
]
output = gr.Image(type="pil", label="Generated Image")
examples = [
["https://example.com/1", "A colorful sunset over the ocean"],
["https://example.com/2", "A cute puppy playing in the park"],
["https://example.com/3", "A green mountains landscape"]
]
# Gradio app interface
gr.Interface(
fn=generate_image,
inputs=inputs,
outputs=output,
title="QR Code Image Generator ✨",
description="Generate an image with a transparent QR code linked to the input URL and an image from the Dalle-3 API",
examples=examples,
theme="soft",
cache_examples=False # Disable example caching
).launch() |