File size: 6,197 Bytes
eeb92e3 395262d eeb92e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import os
import gradio as gr
from google import genai
from google.genai import types
# Ensure 'static' directory exists
os.makedirs("static", exist_ok=True)
def save_binary_file(file_name, data):
with open(file_name, "wb") as f:
f.write(data)
def generate(api_key, occasion, sender_name, recipient_name, custom_message):
client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != ""
else os.environ.get("GEMINI_API_KEY")))
api_key = api_key.strip() if api_key and api_key.strip() != "" else os.environ.get("GEMINI_API_KEY")
client = genai.Client(api_key=api_key)
model = "gemini-2.0-flash-exp-image-generation"
# Predefined prompts for different occasions
if not custom_message:
if occasion == "Birthday":
prompt = (
f"Generate a design for a birthday card with beautiful floral decorations. "
f"The text should be large and say:\n\n"
f"Happy Birthday, {recipient_name}!\n\n"
f"Wishing you a day filled with joy, laughter, and all your favorite things.\n"
f"May this next year be your best one yet, bringing you exciting adventures and wonderful memories.\n"
f"Cheers to you!\n\n"
f"With love, {sender_name}."
)
elif occasion == "Anniversary":
prompt = (
f"Create an elegant anniversary card with golden details and a heartfelt message. "
f"The text should be large and say:\n\n"
f"Happy Anniversary, {recipient_name}!\n\n"
f"Celebrating another year of love, laughter, and cherished moments together.\n"
f"Wishing you endless happiness and many more beautiful years ahead.\n"
f"Cheers to love!\n\n"
f"With best wishes, {sender_name}."
)
elif occasion == "Christmas":
prompt = (
f"Design a festive Christmas card with snowy landscapes and warm holiday colors. "
f"The text should be large and say:\n\n"
f"Merry Christmas, {recipient_name}!\n\n"
f"May your holidays be filled with joy, love, and the magic of the season.\n"
f"Wishing you peace, happiness, and wonderful moments with your loved ones.\n"
f"Warmest wishes for a joyful Christmas!\n\n"
f"With love, {sender_name}."
)
elif occasion == "New Year":
prompt = (
f"Generate a vibrant New Year greeting card with fireworks and celebration themes. "
f"The text should be large and say:\n\n"
f"Happy New Year, {recipient_name}!\n\n"
f"Wishing you a year filled with success, happiness, and new opportunities.\n"
f"May each day bring you closer to your dreams and be filled with exciting possibilities.\n"
f"Cheers to a fantastic year ahead!\n\n"
f"Best wishes, {sender_name}."
)
else:
prompt = (
f"Create a heartfelt greeting card with a soft, pastel background. "
f"The text should be large and say:\n\n"
f"Thinking of You, {recipient_name}!\n\n"
f"Just a little note to remind you how special and amazing you are.\n"
f"Sending you warm thoughts, love, and positivity your way.\n"
f"Hope your day is as wonderful as you are!\n\n"
f"With warm wishes, {sender_name}."
)
else:
prompt = f"Generate a greeting card with a nice background and clear text that says:\n\nDear {recipient_name},\n\n{custom_message}\n\nFrom, {sender_name}."
contents = [
types.Content(
role="user",
parts=[types.Part.from_text(text=prompt)],
),
]
generate_content_config = types.GenerateContentConfig(
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192,
response_modalities=["text", "image"],
)
response = client.models.generate_content(
model=model,
contents=contents,
config=generate_content_config,
)
for candidate in response.candidates:
for part in candidate.content.parts:
if part.inline_data:
file_name = f"static/{str(occasion).lower()}_card.png"
save_binary_file(file_name, part.inline_data.data)
return file_name
return "β No image was generated. Try again with a different input."
# Gradio UI
with gr.Blocks(theme=gr.themes.Citrus()) as demo:
gr.Markdown("# π¨ AI Greeting Card Generator π¨")
# API Key Guide in a single row
with gr.Row():
gr.Markdown(
"### π Get Your Gemini API Key: \n"
"1οΈβ£ Go to [Google AI Studio](https://aistudio.google.com/apikey) and log in. \n"
"2οΈβ£ Generate your Gemini API Key. \n"
"3οΈβ£ Copy it and paste it β‘οΈ. \n",
elem_id="api_guide"
)
api_key_input = gr.Textbox(label="π Enter Your Gemini API Key", type="password")
with gr.Row():
with gr.Column():
occasion = gr.Dropdown(
label="π Choose an Occasion",
choices=["Birthday", "Christmas", "New Year", "Anniversary", "Others"],
value="Birthday"
)
sender_name = gr.Textbox(label="βοΈ Sender's Name")
recipient_name = gr.Textbox(label="π Recipient's Name")
custom_message = gr.Textbox(label="π Custom Message (Leave blank for auto-generated)")
generate_button = gr.Button("π Generate Greeting Card", variant="primary")
with gr.Column():
output_image = gr.Image(label="πΌοΈ Your Generated Card", elem_id="output_image",height=500, width=600)
generate_button.click(
fn=generate,
inputs=[api_key_input, occasion, sender_name, recipient_name, custom_message],
outputs=output_image
)
demo.launch()
|