|
|
|
|
|
import streamlit as st |
|
from utils.helper_utilities import generate_flux_image, add_custom_message |
|
from utils.configuration import fonts, holiday_scene_prompts, example_holiday_messages |
|
from dotenv import load_dotenv |
|
import os |
|
|
|
|
|
st.set_page_config(page_title="FLUX Image Generation Tool", page_icon="π") |
|
|
|
|
|
st.title("π FLUX-tastic Holiday Postcard Generator π¨") |
|
st.markdown( |
|
"""Welcome to the FLUX Holiday Postcard Generator! |
|
|
|
- π
π¨ It's time to make your holiday greetings pop with a personalized, AI-generated postcard! |
|
- β¨ No more boring, store-bought cards! Instead, flex your creative muscles with the FLUX models, adding your unique touch. |
|
- Customize a dazzling holiday scene with your own message and share your festive vibes! ππ |
|
|
|
### How it works: |
|
|
|
1. Choose a holiday-themed prompt or write your own π |
|
2. Select a FLUX model to bring your vision to life β¨ |
|
3. Customize with a snazzy holiday message and choose from a variety of fonts ποΈ |
|
4. Design your text's background and color to perfectly match the vibe π¨ |
|
5. Generate your masterpiece and share the joy with friends and family! π¬ |
|
|
|
Get ready to deck the halls with creativity! πβ¨ |
|
|
|
""" |
|
) |
|
|
|
st.divider() |
|
st.subheader("Load Fireworks API Key") |
|
|
|
|
|
|
|
dotenv_path = os.path.join(os.path.dirname(__file__), '..', 'env', '.env') |
|
os.makedirs(os.path.dirname(dotenv_path), exist_ok=True) |
|
|
|
|
|
if not os.path.exists(dotenv_path): |
|
with open(dotenv_path, "w") as f: |
|
st.success(f"Created {dotenv_path}") |
|
|
|
|
|
load_dotenv(dotenv_path, override=True) |
|
|
|
|
|
fireworks_api_key = os.getenv("FIREWORKS_API_KEY") |
|
|
|
|
|
if not fireworks_api_key or fireworks_api_key.strip() == "": |
|
fireworks_api_key = st.text_input("Enter Fireworks API Key", type="password") |
|
|
|
|
|
if fireworks_api_key and st.checkbox("Save API key for future use"): |
|
with open(dotenv_path, "a") as f: |
|
f.write(f"FIREWORKS_API_KEY={fireworks_api_key}\n") |
|
st.success("API key saved to .env file.") |
|
else: |
|
st.success(f"API key loaded successfully: partial preview {fireworks_api_key[:5]}") |
|
|
|
|
|
|
|
st.divider() |
|
st.subheader("1οΈβ£ Step 1: Pick Your Holiday Theme or Create Your Own πβ¨") |
|
st.markdown(""" |
|
Get into the festive spirit by choosing from a range of holiday-inspired prompts. Feeling extra creative? Enter your own prompt and let the holiday magic begin! π
β¨ |
|
""") |
|
|
|
selected_prompt = st.selectbox("Choose a holiday-themed prompt or enter your own", options=["Custom"] + holiday_scene_prompts) |
|
custom_prompt = st.text_input("Enter your custom prompt") if selected_prompt == "Custom" else "" |
|
prompt = custom_prompt if selected_prompt == "Custom" else selected_prompt |
|
|
|
|
|
st.divider() |
|
st.subheader("2οΈβ£ Step 2: Select Your FLUX Model π") |
|
st.markdown(""" |
|
Choose from two FLUX models: whether youβre aiming for lightning speed or extra detail, weβve got you covered! π₯β¨ |
|
""") |
|
|
|
model_choice = st.selectbox("Select the model:", ["flux-1-schnell-fp8", "flux-1-dev-fp8"]) |
|
|
|
|
|
st.divider() |
|
st.subheader("2οΈβ£A: Customize Your Model Parameters βοΈ") |
|
st.markdown(""" |
|
Now that you've selected your model, fine-tune the parameters to adjust how the FLUX model generates your holiday card. |
|
""") |
|
|
|
|
|
guidance_scale = st.slider("Guidance Scale", min_value=0.0, max_value=20.0, value=7.5, step=0.1) |
|
num_inference_steps = st.slider("Number of Inference Steps", min_value=1, max_value=100, value=50, step=1) |
|
seed = st.slider("Random Seed", min_value=0, max_value=1000, value=42) |
|
|
|
|
|
|
|
st.divider() |
|
st.subheader("3οΈβ£ Step 3: Craft the Perfect Message π") |
|
st.markdown(""" |
|
Whatβs a holiday card without a heartfelt (or hilarious) message? Choose from example holiday greetings or write your own to make the card so you! ποΈπ |
|
""") |
|
|
|
selected_message = st.selectbox("Choose an example message or write your own:", options=["Custom"] + example_holiday_messages) |
|
message = st.text_input("Enter a holiday message to add:", value=selected_message if selected_message != "Custom" else "") |
|
|
|
|
|
st.divider() |
|
st.subheader("4οΈβ£ Step 4: Style Your Message with Flair β¨") |
|
st.markdown(""" |
|
From fancy fonts to colorful backgrounds, youβre in control. Pick your favorite font, adjust the size, and add a splash of color to make your message truly shine. ππ¨ |
|
""") |
|
|
|
font_choice = st.selectbox("Select a font:", list(fonts.keys())) |
|
font_size = st.slider("Select font size:", 1, 300, 40) |
|
max_chars = st.slider("Max characters per line:", 10, 100, 40) |
|
|
|
|
|
bg_color = st.color_picker("Pick a background color for the text box:", "#FFFFFF") |
|
font_color = st.color_picker("Pick a font color:", "#000000") |
|
|
|
|
|
alpha = st.slider("Select transparency level for the background (0: fully transparent, 255: fully opaque)", 0, 255, 220) |
|
|
|
|
|
position_vertical = st.radio("Select message vertical position:", ["Top", "Center", "Bottom"]) |
|
position_horizontal = st.radio("Select message horizontal position:", ["Left", "Center", "Right"]) |
|
|
|
st.divider() |
|
st.subheader("5οΈβ£ Step 5: Preview and Share the Holiday Cheer! π
π¬") |
|
st.markdown(""" |
|
Click "Generate Image" and watch the magic happen! Your holiday card is just moments away from spreading joy to everyone on your list. ππβ¨ |
|
""") |
|
|
|
|
|
if not fireworks_api_key or fireworks_api_key.strip() == "": |
|
st.warning("Enter a valid Fireworks API key to enable image generation.") |
|
generate_button = st.button("Generate Image", disabled=True) |
|
else: |
|
generate_button = st.button("Generate Image") |
|
|
|
|
|
if generate_button: |
|
st.markdown(""" |
|
π You're one click away from holiday magic! π Hit that Generate Image button and let FLUX create your personalized postcardβready for sharing! π¬ |
|
""") |
|
if not prompt.strip(): |
|
st.error("Please provide a prompt.") |
|
else: |
|
try: |
|
with st.spinner("Generating image..."): |
|
|
|
steps = 30 if model_choice == "flux-1-dev-fp8" else 4 |
|
|
|
|
|
generated_image = generate_flux_image( |
|
model_path=model_choice, |
|
api_key=fireworks_api_key, |
|
prompt=prompt, |
|
steps=steps, |
|
guidance_scale=guidance_scale, |
|
seed=seed |
|
) |
|
|
|
|
|
font_path = fonts[font_choice] |
|
|
|
|
|
image_with_message = add_custom_message( |
|
generated_image.copy(), message, font_path, font_size, |
|
position_vertical, position_horizontal, max_chars, bg_color, font_color, alpha |
|
) |
|
|
|
|
|
st.image(image_with_message, caption=f"Generated using {model_choice} with custom message", use_column_width=True) |
|
|
|
|
|
st.write(f"Message preview (vertical position: {position_vertical}, horizontal position: {position_horizontal}, font: {font_choice}, size: {font_size}, max chars: {max_chars}, bg color: {bg_color}, font color: {font_color}, transparency: {alpha})") |
|
|
|
except Exception as e: |
|
st.error(f"An error occurred: {e}") |
|
|
|
|
|
st.divider() |
|
st.markdown( |
|
""" |
|
Thank you for using the Holiday Card Generator powered by **Fireworks**! π |
|
Share your creations with the world and spread the holiday cheer! |
|
Happy Holidays from the **Fireworks Team**. π₯ |
|
""" |
|
) |