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typing import Optional, List
|
| 6 |
+
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import streamlit as st
|
| 9 |
+
|
| 10 |
+
from providers.openai_provider import OpenAIImageProvider
|
| 11 |
+
from utils.prompt_templates import STYLE_PRESETS, compose_prompt
|
| 12 |
+
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class GenOptions:
|
| 17 |
+
style: str
|
| 18 |
+
room_type: str
|
| 19 |
+
elements: str
|
| 20 |
+
materials: str
|
| 21 |
+
palette: str
|
| 22 |
+
lighting: str
|
| 23 |
+
camera: str
|
| 24 |
+
size: str
|
| 25 |
+
variations: int
|
| 26 |
+
|
| 27 |
+
def init_state():
|
| 28 |
+
if "gallery" not in st.session_state:
|
| 29 |
+
st.session_state.gallery = []
|
| 30 |
+
|
| 31 |
+
def decode_b64_to_bytes(b64: str) -> bytes:
|
| 32 |
+
return base64.b64decode(b64)
|
| 33 |
+
|
| 34 |
+
def main():
|
| 35 |
+
st.set_page_config(page_title="RoomGen — Text → Furnished Room", page_icon="🛋️", layout="wide")
|
| 36 |
+
init_state()
|
| 37 |
+
|
| 38 |
+
st.title("🛋️ RoomGen")
|
| 39 |
+
st.caption("Type a description → get a photorealistic, fully furnished room image.")
|
| 40 |
+
|
| 41 |
+
# Sidebar: provider + settings
|
| 42 |
+
with st.sidebar:
|
| 43 |
+
st.subheader("⚙️ Settings")
|
| 44 |
+
provider_name = st.selectbox("Image Provider", ["Together (FLUX)","OpenAI (recommended)"], index=0)
|
| 45 |
+
size = st.selectbox("Image Size", ["1024x1024", "768x768", "512x512"], index=0)
|
| 46 |
+
variations = st.slider("Number of Variations", 1, 2, 1)
|
| 47 |
+
st.markdown("---")
|
| 48 |
+
st.caption("Set your API key in `.env` or in this box (overrides env).")
|
| 49 |
+
openai_key_override = st.text_input("OPENAI_API_KEY", type="password")
|
| 50 |
+
|
| 51 |
+
# Prompt composer
|
| 52 |
+
st.subheader("📝 Describe your room")
|
| 53 |
+
col1, col2 = st.columns([1,1])
|
| 54 |
+
with col1:
|
| 55 |
+
style = st.selectbox("Style preset", list(STYLE_PRESETS.keys()), index=0)
|
| 56 |
+
room_type = st.selectbox(
|
| 57 |
+
"Room type",
|
| 58 |
+
["living room", "bedroom", "kitchen", "home office", "dining room", "bathroom", "kids room"],
|
| 59 |
+
index=0
|
| 60 |
+
)
|
| 61 |
+
elements = st.text_input("Key elements", "sofa, coffee table, area rug, wall art, floor lamp")
|
| 62 |
+
materials = st.text_input("Materials", "light oak wood, linen upholstery, matte black metal")
|
| 63 |
+
with col2:
|
| 64 |
+
palette = st.text_input("Color palette", "soft neutrals, sage green accents")
|
| 65 |
+
lighting = st.text_input("Lighting", "soft ambient, warm temperature, window light")
|
| 66 |
+
camera = st.text_input("Camera / framing", "35mm lens, eye-level, centered composition")
|
| 67 |
+
|
| 68 |
+
# Free-form addition / override
|
| 69 |
+
extra_desc = st.text_area("Extra description (optional)", "", help="Add anything special (e.g., bay windows, bookshelves, plants).")
|
| 70 |
+
|
| 71 |
+
# Compose final prompt preview
|
| 72 |
+
final_prompt = compose_prompt(
|
| 73 |
+
style=style,
|
| 74 |
+
room_type=room_type,
|
| 75 |
+
elements=elements,
|
| 76 |
+
materials=materials,
|
| 77 |
+
palette=palette,
|
| 78 |
+
lighting=lighting,
|
| 79 |
+
camera=camera,
|
| 80 |
+
extra=extra_desc
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
with st.expander("🔍 Final prompt (what the model sees)"):
|
| 84 |
+
st.code(final_prompt)
|
| 85 |
+
|
| 86 |
+
# Generate
|
| 87 |
+
do_gen = st.button("✨ Generate", type="primary")
|
| 88 |
+
|
| 89 |
+
if do_gen:
|
| 90 |
+
try:
|
| 91 |
+
if provider_name.startswith("Together"):
|
| 92 |
+
key = os.getenv("TOGETHER_API_KEY", "")
|
| 93 |
+
if not key:
|
| 94 |
+
st.error("Missing TOGETHER_API_KEY. Add it to .env or your environment.")
|
| 95 |
+
st.stop()
|
| 96 |
+
from providers.together_provider import TogetherImageProvider
|
| 97 |
+
provider = TogetherImageProvider(api_key=key)
|
| 98 |
+
elif provider_name.startswith("OpenAI"):
|
| 99 |
+
key = openai_key_override or os.getenv("OPENAI_API_KEY", "")
|
| 100 |
+
if not key:
|
| 101 |
+
st.error("Missing OPENAI_API_KEY.")
|
| 102 |
+
st.stop()
|
| 103 |
+
provider = OpenAIImageProvider(api_key=key)
|
| 104 |
+
else:
|
| 105 |
+
st.error("Unknown provider selected.")
|
| 106 |
+
st.stop()
|
| 107 |
+
|
| 108 |
+
with st.spinner("Creating your room…"):
|
| 109 |
+
images_b64: List[str] = provider.generate_image(
|
| 110 |
+
prompt=final_prompt,
|
| 111 |
+
size=size,
|
| 112 |
+
n=variations
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
cols = st.columns(variations)
|
| 116 |
+
for i, img_b64 in enumerate(images_b64):
|
| 117 |
+
img_bytes = decode_b64_to_bytes(img_b64)
|
| 118 |
+
cols[i].image(img_bytes, caption=f"Result {i+1} — {size}")
|
| 119 |
+
# Save to gallery
|
| 120 |
+
st.session_state.gallery.insert(0, {"bytes": img_bytes, "size": size, "style": style, "room_type": room_type, "prompt": final_prompt})
|
| 121 |
+
|
| 122 |
+
# Download button
|
| 123 |
+
cols[i].download_button(
|
| 124 |
+
"Download PNG",
|
| 125 |
+
data=img_bytes,
|
| 126 |
+
file_name=f"roomgen_{style}_{room_type}_{i+1}.png",
|
| 127 |
+
mime="image/png",
|
| 128 |
+
use_container_width=True
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
except Exception as e:
|
| 132 |
+
st.error(f"Generation failed: {e}")
|
| 133 |
+
|
| 134 |
+
# Gallery
|
| 135 |
+
if st.session_state.gallery:
|
| 136 |
+
st.subheader("🖼️ Recent Generations")
|
| 137 |
+
grid_cols = st.columns(3)
|
| 138 |
+
for idx, item in enumerate(st.session_state.gallery[:6]):
|
| 139 |
+
col = grid_cols[idx % 3]
|
| 140 |
+
col.image(item["bytes"], caption=f'{item["style"]} {item["room_type"]}')
|
| 141 |
+
with col.expander("Prompt"):
|
| 142 |
+
col.code(item["prompt"])
|
| 143 |
+
|
| 144 |
+
st.markdown("---")
|
| 145 |
+
st.caption("Tip: start simple. You can add windows, wall colors, ceiling lights, rugs, etc., in the extra box.")
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
+
main()
|