File size: 1,901 Bytes
51fcaf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import gradio as gr
from PIL import Image
import io
import os

# API settings
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3-medium-diffusers"
headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.content

def generate_room_plan(space_type, prompt):
    # Validate the prompt based on selected space type
    valid_prompt = False
    if space_type == "office" and "office" in prompt:
        valid_prompt = True
    elif space_type == "house" and "house" in prompt:
        valid_prompt = True
    elif space_type == "room" and "room" in prompt:
        valid_prompt = True
    
    if not valid_prompt:
        return "Error: The prompt does not match the selected space type or is invalid."

    image_bytes = query({"inputs": prompt})
    
    # Convert the byte content to an image
    image = Image.open(io.BytesIO(image_bytes))
    
    return image

with gr.Blocks() as app:
    gr.Markdown("# Space Plan Generator")

    with gr.Row():
        space_type = gr.Dropdown(
            choices=["office", "house", "room"],
            label="Select Space Type"
        )
        user_prompt = gr.Textbox(label="Enter the dimensions and other details")

    output = gr.Image(label="Generated Space Plan")
    error_message = gr.Markdown(label="")

    def on_button_click(space_type, prompt):
        result = generate_room_plan(space_type, prompt)
        if isinstance(result, str):  # If the result is an error message
            error_message.update(result)
            return None
        else:
            error_message.update("")
            return result

    gr.Button("Generate Space Plan").click(
        on_button_click,
        inputs=[space_type, user_prompt],
        outputs=[output]
    )

app.launch()