Spaces:
Paused
Paused
Commit
·
8124057
1
Parent(s):
659532d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
import base64
|
6 |
+
|
7 |
+
# Hugging Face ControlNet API (Canny version)
|
8 |
+
HF_API = "https://api-inference.huggingface.co/models/lllyasviel/controlnet-sdxl-1.0-canny"
|
9 |
+
API_KEY = os.getenv("HF_API_KEY") # Secure: fetch from secret
|
10 |
+
|
11 |
+
headers = {
|
12 |
+
"Authorization": f"Bearer {API_KEY}"
|
13 |
+
}
|
14 |
+
|
15 |
+
def generate_image(prompt, image):
|
16 |
+
buffered = BytesIO()
|
17 |
+
image.save(buffered, format="JPEG")
|
18 |
+
img_bytes = buffered.getvalue()
|
19 |
+
|
20 |
+
payload = {
|
21 |
+
"inputs": {
|
22 |
+
"prompt": prompt,
|
23 |
+
"image": base64.b64encode(img_bytes).decode("utf-8"),
|
24 |
+
"negative_prompt": "blurry, deformed, cropped"
|
25 |
+
},
|
26 |
+
"options": {"wait_for_model": True}
|
27 |
+
}
|
28 |
+
|
29 |
+
response = requests.post(HF_API, headers=headers, json=payload)
|
30 |
+
|
31 |
+
if response.status_code == 200:
|
32 |
+
img_out = Image.open(BytesIO(response.content))
|
33 |
+
return img_out
|
34 |
+
else:
|
35 |
+
return f"Error: {response.status_code} - {response.text}"
|
36 |
+
|
37 |
+
# Gradio UI
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
gr.Markdown("# 🧠 NewCrux AI Demo: Product → Lifestyle Image")
|
40 |
+
with gr.Row():
|
41 |
+
input_image = gr.Image(type="pil", label="Upload Product Image")
|
42 |
+
prompt_text = gr.Textbox(label="Enter Prompt", placeholder="e.g., A runner on a beach wearing this shoe")
|
43 |
+
output_image = gr.Image(label="Generated Lifestyle Image")
|
44 |
+
generate_btn = gr.Button("Generate Image")
|
45 |
+
|
46 |
+
generate_btn.click(fn=generate_image, inputs=[prompt_text, input_image], outputs=output_image)
|
47 |
+
|
48 |
+
demo.launch()
|