Skip to content

Commit

Permalink
feat!: Add in-program rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-Shilin committed May 11, 2024
1 parent 893e6a4 commit bc9aa3b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ BuilderGPT is an open source, free, AI-powered Minecraft structure generator. It
## Features

- [x] Generate structures
- [x] Rendering schematic in-program
- [x] Export generated structures to `*.schem` files
- [ ] Export generated structures to OOC commands
- [ ] Edit structures
Expand Down Expand Up @@ -69,7 +70,7 @@ After the user enters a requirement, the program causes `gpt-4-preview` to gener
]
}
```
The program then parses this `json` response and generates a `*.schem` file for the user to import the structure into the game.
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`.

## Requirements
You can use BukkitGPT on any device with [Python 3+](https://www.python.org/).
Expand Down
33 changes: 33 additions & 0 deletions browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import re, time
import config
from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto(config.RENDERING_URL)

time.sleep(10)

page.get_by_text("File").click()
with page.expect_file_chooser() as fc_info:
page.locator("[id=\"\\31 2\"]").click()
file_chooser = fc_info.value
file_chooser.set_files("temp/waiting_for_upload.schem")
page.get_by_text("File", exact=True).click()
page.locator("[id=\"\\32 3\"] > div:nth-child(3)").click()
with page.expect_download() as download_info:
page.locator("[id=\"\\31 8\"]").click()

download = download_info.value
download.save_as("temp/screenshot.png")
page.close()

context.close()
browser.close()

if __name__ == "__main__":
with sync_playwright() as playwright:
run(playwright)
26 changes: 21 additions & 5 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ VISION_BASE_URL: "https://api.openai.com/v1/chat/completions"

########## EDIT OPTIONAL ##########

# EXPERIMENTAL FEATURES #
NAMING_MODEL: "gpt-3.5-turbo"
RENDERING_URL: "https://beta.cubical.xyz/" # Don't change this unless you know what you are doing.

# DEVELOPER SETTINGS #
DEBUG_MODE: True
VERSION_NUMBER: "1.1.0" #NEVER EDIT THIS IF YOU DON'T KNOW WHAT ARE YOU DOING

# PROMPT SETTINGS #
# If you don't know what it is, please don't touch it. Be sure to backup before editing.

Expand Down Expand Up @@ -70,6 +78,18 @@ SYS_GEN: |
USR_GEN: |
%DESCRIPTION%
SYS_GEN_NAME: |
Give a name to the building based on user's desciption. Do not use spaces.
For example:
Input "A small house with a red roof."
Output "RedRoofHouse"
Input "A cafe with a modern design."
Output "ModernCafe"
USR_GEN_NAME: |
%DESCRIPTION%
## Advanced Mode ##

### Programme ###
Expand Down Expand Up @@ -127,8 +147,4 @@ SYS_GEN_ADV: |
USR_GEN_ADV: |
%DESCRIPTION%
The image is attached below.
# Developer Settings #
DEBUG_MODE: True
VERSION_NUMBER: "Alpha-1.0" #NEVER EDIT THIS IF YOU DON'T KNOW WHAT ARE YOU DOING
The image is attached below.
Binary file added temp/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added temp/waiting_for_upload.schem
Binary file not shown.
34 changes: 32 additions & 2 deletions ui.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import sys
import uuid
import os
import shutil
from playwright.sync_api import Playwright, sync_playwright
import tkinter as tk
import tkinter.messagebox as msgbox
import tkinter.simpledialog as simpledialog

from log_writer import logger
import core
import config
import browser

def get_schematic(description):
"""
Expand Down Expand Up @@ -89,12 +94,35 @@ def generate_schematic():

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

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

generate_button.config(state=tk.NORMAL, text="Generate")
render_button.pack()

def render_schematic():
render_button.config(state=tk.DISABLED, text="Rendering...")

msgbox.showinfo("Info", "Rendering the schematic. It will take 15 seconds. DO NOT CLOSE THE PROGRAM.")

try:
os.remove("temp/waiting_for_upload.schem")
os.remove("temp/screenshot.png")
except FileNotFoundError:
pass

shutil.copy(f"generated/{name}.schem", "temp/waiting_for_upload.schem")
with sync_playwright() as playwright:
browser.run(playwright)

render_button.config(state=tk.NORMAL, text="Render")

image = tk.PhotoImage(file="temp/screenshot.png")
image = image.subsample(4)
rendered_image = tk.Label(window, image=image)
rendered_image.pack()

def Application():
global version_entry, name_entry, description_entry, generate_button
global window, version_entry, description_entry, generate_button, render_button, rendered_image

window = tk.Tk()
window.title("BuilderGPT")
Expand Down Expand Up @@ -122,6 +150,8 @@ def Application():
generate_button = tk.Button(window, text="Generate", command=generate_schematic)
generate_button.pack()

render_button = tk.Button(window, text="Render", command=render_schematic)

window.mainloop()

if __name__ == "__main__":
Expand Down

0 comments on commit bc9aa3b

Please sign in to comment.