Spaces:
Sleeping
Sleeping
Kvikontent
commited on
Commit
•
945e112
1
Parent(s):
e627280
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import qrcode
|
4 |
+
import io
|
5 |
+
from PIL import Image
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Get the Hugging Face API token from the environment variable
|
9 |
+
hf_token = os.environ.get("hf_token")
|
10 |
+
|
11 |
+
# Function to generate a QR code from the input URL
|
12 |
+
def generate_qr_code(url):
|
13 |
+
qr = qrcode.QRCode(
|
14 |
+
version=1,
|
15 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
16 |
+
box_size=10,
|
17 |
+
border=4,
|
18 |
+
)
|
19 |
+
qr.add_data(url)
|
20 |
+
qr.make(fit=True)
|
21 |
+
|
22 |
+
qr_img = qr.make_image(fill_color="black", back_color="white")
|
23 |
+
return qr_img
|
24 |
+
|
25 |
+
# Function to query the image from the Hugging Face API
|
26 |
+
def query_image(text):
|
27 |
+
API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/realistic-vision-v51"
|
28 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
29 |
+
|
30 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": text})
|
31 |
+
return response.content
|
32 |
+
|
33 |
+
# Gradio app function
|
34 |
+
def generate_image(url, text):
|
35 |
+
# Generate QR code
|
36 |
+
qr_img = generate_qr_code(url)
|
37 |
+
|
38 |
+
# Generate image from Hugging Face API
|
39 |
+
image_bytes = query_image(text)
|
40 |
+
api_image = Image.open(io.BytesIO(image_bytes))
|
41 |
+
|
42 |
+
# Create a blank image to combine the API image and QR code
|
43 |
+
final_image = Image.new('RGB', (600, 600))
|
44 |
+
final_image.paste(api_image, (0, 0))
|
45 |
+
final_image.paste(qr_img, (400, 400))
|
46 |
+
|
47 |
+
return final_image
|
48 |
+
|
49 |
+
# Inputs and outputs for Gradio interface
|
50 |
+
inputs = [
|
51 |
+
gr.Textbox(lines=1, label="URL"),
|
52 |
+
gr.Textbox(lines=2, label="Prompt for Image"),
|
53 |
+
]
|
54 |
+
output = gr.Image(type="pil", label="Generated Image")
|
55 |
+
|
56 |
+
# Gradio app interface
|
57 |
+
gr.Interface(fn=generate_image, inputs=inputs, outputs=output, title="QR Code Image Generator", description="Generate an image with a QR code linked to the input URL and an image from the Hugging Face API").launch(theme="soft")
|