Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import os
|
6 |
+
|
7 |
+
# API settings
|
8 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3-medium-diffusers"
|
9 |
+
headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
|
10 |
+
|
11 |
+
def query(payload):
|
12 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
13 |
+
return response.content
|
14 |
+
|
15 |
+
def generate_room_plan(space_type, prompt):
|
16 |
+
# Validate the prompt based on selected space type
|
17 |
+
valid_prompt = False
|
18 |
+
if space_type == "office" and "office" in prompt:
|
19 |
+
valid_prompt = True
|
20 |
+
elif space_type == "house" and "house" in prompt:
|
21 |
+
valid_prompt = True
|
22 |
+
elif space_type == "room" and "room" in prompt:
|
23 |
+
valid_prompt = True
|
24 |
+
|
25 |
+
if not valid_prompt:
|
26 |
+
return "Error: The prompt does not match the selected space type or is invalid."
|
27 |
+
|
28 |
+
image_bytes = query({"inputs": prompt})
|
29 |
+
|
30 |
+
# Convert the byte content to an image
|
31 |
+
image = Image.open(io.BytesIO(image_bytes))
|
32 |
+
|
33 |
+
return image
|
34 |
+
|
35 |
+
with gr.Blocks() as app:
|
36 |
+
gr.Markdown("# Space Plan Generator")
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
space_type = gr.Dropdown(
|
40 |
+
choices=["office", "house", "room"],
|
41 |
+
label="Select Space Type"
|
42 |
+
)
|
43 |
+
user_prompt = gr.Textbox(label="Enter the dimensions and other details")
|
44 |
+
|
45 |
+
output = gr.Image(label="Generated Space Plan")
|
46 |
+
error_message = gr.Markdown(label="")
|
47 |
+
|
48 |
+
def on_button_click(space_type, prompt):
|
49 |
+
result = generate_room_plan(space_type, prompt)
|
50 |
+
if isinstance(result, str): # If the result is an error message
|
51 |
+
error_message.update(result)
|
52 |
+
return None
|
53 |
+
else:
|
54 |
+
error_message.update("")
|
55 |
+
return result
|
56 |
+
|
57 |
+
gr.Button("Generate Space Plan").click(
|
58 |
+
on_button_click,
|
59 |
+
inputs=[space_type, user_prompt],
|
60 |
+
outputs=[output]
|
61 |
+
)
|
62 |
+
|
63 |
+
app.launch()
|