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