From 5de9e870d730ba5b53af95b27f1ed48b74d80ccc Mon Sep 17 00:00:00 2001 From: milochen0418 Date: Sat, 1 Jul 2023 11:57:19 +0800 Subject: [PATCH 1/2] Update dalle example with form and yield --- dalle/dalle/dalle.py | 32 ++++++++++++++------------------ dalle/requirements.txt | 2 +- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/dalle/dalle/dalle.py b/dalle/dalle/dalle.py index df84e4c6..8d5c92a4 100644 --- a/dalle/dalle/dalle.py +++ b/dalle/dalle/dalle.py @@ -7,39 +7,36 @@ class State(pc.State): """The app state.""" - - prompt = "" image_url = "" image_processing = False image_made = False - - def process_image(self): - """Set the image processing flag to true and indicate that the image has not been made yet.""" + def get_dalle_result(self, form_data: dict[str, str]): + prompt_text:str = form_data["prompt_text"] self.image_made = False self.image_processing = True - - def get_image(self): - """Get the image from the prompt.""" + yield try: - response = openai.Image.create(prompt=self.prompt, n=1, size="1024x1024") + response = openai.Image.create(prompt=prompt_text, n=1, size="1024x1024") self.image_url = response["data"][0]["url"] - # Set the image processing flag to false and indicate that the image has been made. self.image_processing = False self.image_made = True + yield except: self.image_processing = False - return pc.window_alert("Error with OpenAI Execution.") - + yield pc.window_alert("Error with OpenAI Execution.") def index(): return pc.center( pc.vstack( pc.heading("DALL-E", font_size="1.5em"), - pc.input(placeholder="Enter a prompt..", on_blur=State.set_prompt), - pc.button( - "Generate Image", - on_click=[State.process_image, State.get_image], - width="100%", + pc.form( + pc.input(id="prompt_text", placeholder="Enter a prompt.."), + pc.button( + "Generate Image", + type_="submit", + width="100%", + ), + on_submit=State.get_dalle_result, ), pc.divider(), pc.cond( @@ -64,7 +61,6 @@ def index(): background="radial-gradient(circle at 22% 11%,rgba(62, 180, 137,.20),hsla(0,0%,100%,0) 19%),radial-gradient(circle at 82% 25%,rgba(33,150,243,.18),hsla(0,0%,100%,0) 35%),radial-gradient(circle at 25% 61%,rgba(250, 128, 114, .28),hsla(0,0%,100%,0) 55%)", ) - # Add state and page to the app. app = pc.App(state=State) app.add_page(index, title="Pynecone:DALL-E") diff --git a/dalle/requirements.txt b/dalle/requirements.txt index 4f5c74bb..e8ec6176 100644 --- a/dalle/requirements.txt +++ b/dalle/requirements.txt @@ -1,2 +1,2 @@ -pynecone>=0.1.33 +pynecone>=0.1.34 openai From 0dd090ac35fedeb561269067bdc1e38dfe256a26 Mon Sep 17 00:00:00 2001 From: milochen0418 Date: Sat, 1 Jul 2023 15:09:40 +0800 Subject: [PATCH 2/2] Update dalle example for reflex 0.2.0 --- dalle/.gitignore | 4 ++-- dalle/dalle/dalle.py | 30 +++++++++++++++--------------- dalle/pcconfig.py | 9 --------- dalle/requirements.txt | 2 +- dalle/rxconfig.py | 10 ++++++++++ 5 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 dalle/pcconfig.py create mode 100644 dalle/rxconfig.py diff --git a/dalle/.gitignore b/dalle/.gitignore index 42649afd..bf56ff42 100644 --- a/dalle/.gitignore +++ b/dalle/.gitignore @@ -1,4 +1,4 @@ +*.py[cod] .web -pynecone.db __pycache__/ -*.py[cod] \ No newline at end of file +reflex.db \ No newline at end of file diff --git a/dalle/dalle/dalle.py b/dalle/dalle/dalle.py index 8d5c92a4..420adb7a 100644 --- a/dalle/dalle/dalle.py +++ b/dalle/dalle/dalle.py @@ -1,11 +1,11 @@ """Welcome to Pynecone! This file outlines the steps to create a basic app.""" -import pynecone as pc +import reflex as rx import openai openai.api_key = "YOUR_API_KEY" -class State(pc.State): +class State(rx.State): """The app state.""" image_url = "" image_processing = False @@ -23,28 +23,28 @@ def get_dalle_result(self, form_data: dict[str, str]): yield except: self.image_processing = False - yield pc.window_alert("Error with OpenAI Execution.") + yield rx.window_alert("Error with OpenAI Execution.") def index(): - return pc.center( - pc.vstack( - pc.heading("DALL-E", font_size="1.5em"), - pc.form( - pc.input(id="prompt_text", placeholder="Enter a prompt.."), - pc.button( + return rx.center( + rx.vstack( + rx.heading("DALL-E", font_size="1.5em"), + rx.form( + rx.input(id="prompt_text", placeholder="Enter a prompt.."), + rx.button( "Generate Image", type_="submit", width="100%", ), on_submit=State.get_dalle_result, ), - pc.divider(), - pc.cond( + rx.divider(), + rx.cond( State.image_processing, - pc.circular_progress(is_indeterminate=True), - pc.cond( + rx.circular_progress(is_indeterminate=True), + rx.cond( State.image_made, - pc.image( + rx.image( src=State.image_url, height="25em", width="25em", @@ -62,6 +62,6 @@ def index(): ) # Add state and page to the app. -app = pc.App(state=State) +app = rx.App(state=State) app.add_page(index, title="Pynecone:DALL-E") app.compile() diff --git a/dalle/pcconfig.py b/dalle/pcconfig.py deleted file mode 100644 index 176a04b5..00000000 --- a/dalle/pcconfig.py +++ /dev/null @@ -1,9 +0,0 @@ -import pynecone as pc - - -config = pc.Config( - app_name="dalle", - bun_path="$HOME/.bun/bin/bun", - db_url="sqlite:///pynecone.db", - env=pc.Env.DEV, -) diff --git a/dalle/requirements.txt b/dalle/requirements.txt index e8ec6176..743a150e 100644 --- a/dalle/requirements.txt +++ b/dalle/requirements.txt @@ -1,2 +1,2 @@ -pynecone>=0.1.34 +reflex>=0.2.0 openai diff --git a/dalle/rxconfig.py b/dalle/rxconfig.py new file mode 100644 index 00000000..88e3afe1 --- /dev/null +++ b/dalle/rxconfig.py @@ -0,0 +1,10 @@ +import reflex as rx + +class DalleConfig(rx.Config): + pass + +config = DalleConfig( + app_name="dalle", + db_url="sqlite:///reflex.db", + env=rx.Env.DEV, +)