Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dalle to templates #45

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dalle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.db
*.py[cod]
.web
__pycache__/
reflex.db
Binary file added dalle/assets/favicon.ico
Binary file not shown.
Empty file added dalle/dalle/__init__.py
Empty file.
96 changes: 96 additions & 0 deletions dalle/dalle/dalle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Welcome to Reflex! This file outlines the steps to create a basic app."""

import reflex as rx

import os

import openai

_client = None


def get_openai_client():
global _client
if _client is None:
_client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

return _client


class State(rx.State):
"""The app state."""

image_url = ""
image_processing = False
image_made = False

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
# Yield here so the image_processing take effects and the circular progress is shown.
yield
try:
response = get_openai_client().images.generate(
prompt=prompt_text, n=1, size="1024x1024"
)
self.image_url = response.data[0].url
self.image_processing = False
self.image_made = True
yield
except Exception as ex:
self.image_processing = False
yield rx.window_alert(f"Error with OpenAI Execution. {ex}")


def index():
return rx.center(
rx.vstack(
rx.heading("DALL-E", font_size="1.5em"),
rx.form(
rx.vstack(
rx.input(
id="prompt_text",
placeholder="Enter a prompt..",
size="3",
),
rx.button(
"Generate Image",
type="submit",
size="3",
),
align="stretch",
spacing="2",
),
width="100%",
on_submit=State.get_dalle_result,
),
rx.divider(),
rx.cond(
State.image_processing,
rx.spinner(),
rx.cond(
State.image_made,
rx.image(
src=State.image_url,
),
),
),
width="25em",
bg="white",
padding="2em",
align="center",
),
width="100%",
height="100vh",
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 = rx.App(
theme=rx.theme(
appearance="light", has_background=True, radius="medium", accent_color="mint"
),
)
app.add_page(index, title="Reflex:DALL-E")
2 changes: 2 additions & 0 deletions dalle/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reflex>=0.4.0
openai>=1
3 changes: 3 additions & 0 deletions dalle/rxconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import reflex as rx

config = rx.Config(app_name="dalle")