Skip to content

Commit 7af7283

Browse files
authored
Merge pull request #7 from Zhou-Shilin/main
feat!: Add in-program rendering
2 parents 61c014b + a6f2667 commit 7af7283

6 files changed

+89
-9
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ Get the key from [here](https://github.com/orgs/CubeGPT/discussions/1). You can
3333

3434
## Features
3535

36+
- [x] Generate structures
37+
- [x] Preview rendered schematic in-program
3638
- [x] Generate structures directly
3739
- [x] Export generated structures to `*.schem` files
3840
- [ ] Export generated structures to OOC commands
3941
- [ ] **Advanced Mode** (Use Stable Diffusion/DALL-E to generate the design image and let `gpt-4-vision` generate the struture base on it.)
40-
- [ ] Preview generated structure
4142
- [ ] Edit structures
4243

4344
### Other projects of CubeGPT Team
@@ -116,7 +117,7 @@ After the user enters a requirement, the program causes `gpt-4-preview` to gener
116117
]
117118
}
118119
```
119-
The program then parses this `json` response and generates a `*.schem` file for the user to import the structure into the game.
120+
The program then parses this `json` response. Then it uploads the image (headless) to cubical.xyz and downloads the rendered image from the site using `playwright`.
120121

121122
## Requirements
122123
You can use BukkitGPT on any device with [Python 3+](https://www.python.org/).

browser.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import re, time
2+
import config
3+
from playwright.sync_api import Playwright, sync_playwright, expect
4+
5+
6+
def run(playwright: Playwright) -> None:
7+
browser = playwright.chromium.launch(headless=True)
8+
context = browser.new_context()
9+
page = context.new_page()
10+
page.goto(config.RENDERING_URL)
11+
12+
time.sleep(10)
13+
14+
page.get_by_text("File").click()
15+
with page.expect_file_chooser() as fc_info:
16+
page.locator("[id=\"\\31 2\"]").click()
17+
file_chooser = fc_info.value
18+
file_chooser.set_files("temp/waiting_for_upload.schem")
19+
page.get_by_text("File", exact=True).click()
20+
page.locator("[id=\"\\32 3\"] > div:nth-child(3)").click()
21+
with page.expect_download() as download_info:
22+
page.locator("[id=\"\\31 8\"]").click()
23+
24+
download = download_info.value
25+
download.save_as("temp/screenshot.png")
26+
page.close()
27+
28+
context.close()
29+
browser.close()
30+
31+
if __name__ == "__main__":
32+
with sync_playwright() as playwright:
33+
run(playwright)

config.yaml

+21-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ VISION_BASE_URL: "https://api.openai.com/v1/chat/completions"
3030

3131
########## EDIT OPTIONAL ##########
3232

33+
# EXPERIMENTAL FEATURES #
34+
NAMING_MODEL: "gpt-3.5-turbo"
35+
RENDERING_URL: "https://beta.cubical.xyz/" # Don't change this unless you know what you are doing.
36+
37+
# DEVELOPER SETTINGS #
38+
DEBUG_MODE: True
39+
VERSION_NUMBER: "1.1.0" #NEVER EDIT THIS IF YOU DON'T KNOW WHAT ARE YOU DOING
40+
3341
# PROMPT SETTINGS #
3442
# If you don't know what it is, please don't touch it. Be sure to backup before editing.
3543

@@ -70,6 +78,18 @@ SYS_GEN: |
7078
USR_GEN: |
7179
%DESCRIPTION%
7280
81+
SYS_GEN_NAME: |
82+
Give a name to the building based on user's desciption. Do not use spaces.
83+
For example:
84+
Input "A small house with a red roof."
85+
Output "RedRoofHouse"
86+
87+
Input "A cafe with a modern design."
88+
Output "ModernCafe"
89+
90+
USR_GEN_NAME: |
91+
%DESCRIPTION%
92+
7393
## Advanced Mode ##
7494

7595
### Programme ###
@@ -127,8 +147,4 @@ SYS_GEN_ADV: |
127147
128148
USR_GEN_ADV: |
129149
%DESCRIPTION%
130-
The image is attached below.
131-
132-
# Developer Settings #
133-
DEBUG_MODE: True
134-
VERSION_NUMBER: "Alpha-1.0" #NEVER EDIT THIS IF YOU DON'T KNOW WHAT ARE YOU DOING
150+
The image is attached below.

temp/screenshot.png

168 KB
Loading

temp/waiting_for_upload.schem

444 Bytes
Binary file not shown.

ui.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import sys
2+
import uuid
3+
import os
4+
import shutil
5+
from playwright.sync_api import Playwright, sync_playwright
26
import tkinter as tk
37
import tkinter.messagebox as msgbox
48
import tkinter.simpledialog as simpledialog
59

610
from log_writer import logger
711
import core
812
import config
13+
import browser
914

1015
def get_schematic(description):
1116
"""
@@ -89,12 +94,35 @@ def generate_schematic():
8994

9095
schem.save("generated", name, version_tag)
9196

92-
msgbox.showinfo("Success", "Generated. Get your schem file in folder generated.")
97+
msgbox.showinfo("Success", f"Generated with file name \"{name}.schem\". Get your schem file in folder generated.")
9398

9499
generate_button.config(state=tk.NORMAL, text="Generate")
100+
render_button.pack()
101+
102+
def render_schematic():
103+
render_button.config(state=tk.DISABLED, text="Rendering...")
104+
105+
msgbox.showinfo("Info", "Rendering the schematic. It will take 15 seconds. DO NOT CLOSE THE PROGRAM.")
106+
107+
try:
108+
os.remove("temp/waiting_for_upload.schem")
109+
os.remove("temp/screenshot.png")
110+
except FileNotFoundError:
111+
pass
112+
113+
shutil.copy(f"generated/{name}.schem", "temp/waiting_for_upload.schem")
114+
with sync_playwright() as playwright:
115+
browser.run(playwright)
116+
117+
render_button.config(state=tk.NORMAL, text="Render")
118+
119+
image = tk.PhotoImage(file="temp/screenshot.png")
120+
image = image.subsample(4)
121+
rendered_image = tk.Label(window, image=image)
122+
rendered_image.pack()
95123

96124
def Application():
97-
global version_entry, name_entry, description_entry, generate_button
125+
global window, version_entry, description_entry, generate_button, render_button, rendered_image
98126

99127
window = tk.Tk()
100128
window.title("BuilderGPT")
@@ -122,6 +150,8 @@ def Application():
122150
generate_button = tk.Button(window, text="Generate", command=generate_schematic)
123151
generate_button.pack()
124152

153+
render_button = tk.Button(window, text="Render", command=render_schematic)
154+
125155
window.mainloop()
126156

127157
if __name__ == "__main__":

0 commit comments

Comments
 (0)