Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
# Mapping styles to instructions
|
| 8 |
+
STYLE_INSTRUCTIONS = {
|
| 9 |
+
"General": (
|
| 10 |
+
"Describe this design as a single, cohesive, and concise prompt suitable for Flux 1.1 Pro. "
|
| 11 |
+
"Focus on key elements such as text, symbols, layout, and overall style."
|
| 12 |
+
),
|
| 13 |
+
"Realistic": (
|
| 14 |
+
"Describe this design with a focus on photorealistic details, including textures, lighting, and depth, "
|
| 15 |
+
"ensuring accuracy in representation. Avoid illustrations or cartoon-like interpretations."
|
| 16 |
+
),
|
| 17 |
+
"Kawaii/Cartoon": (
|
| 18 |
+
"Describe this design with an emphasis on cute, rounded shapes, playful expressions, and vibrant, fun aesthetics "
|
| 19 |
+
"typical of the kawaii/cartoon style. Focus on light-hearted and visually charming elements."
|
| 20 |
+
),
|
| 21 |
+
"Vector": (
|
| 22 |
+
"Describe this design as a single, cohesive, and concise prompt suitable for Flux 1.1 Pro. "
|
| 23 |
+
"Focus on key elements such as text, symbols, layout, and overall style. "
|
| 24 |
+
"Specify that the design should be created in a clean, professional vector and illustration style, "
|
| 25 |
+
"emphasizing sharp lines, geometric shapes, and smooth gradients where applicable. "
|
| 26 |
+
"The entire design must be in black and white, using only shades of gray for depth and contrast. "
|
| 27 |
+
"Avoid any use of color unless it is essential to the design's purpose. "
|
| 28 |
+
"Avoid cartoonish or overly stylized elements unless they align with the design's purpose. "
|
| 29 |
+
"Use clear and direct language to convey the design's theme, humor, or purpose, if applicable. "
|
| 30 |
+
"Ensure the description is compact, well-structured, and optimized for creating a graphic. "
|
| 31 |
+
"Include only essential details and avoid unnecessary elaboration."
|
| 32 |
+
),
|
| 33 |
+
"Silhouette": (
|
| 34 |
+
"Describe this design using high-contrast black and white silhouettes, focusing on bold shapes and clear outlines "
|
| 35 |
+
"to convey the subject effectively. Eliminate interior details unless crucial to recognition."
|
| 36 |
+
),
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# Function to check API key validity
|
| 40 |
+
def check_api(api_key):
|
| 41 |
+
try:
|
| 42 |
+
genai.configure(api_key=api_key)
|
| 43 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 44 |
+
response = model.generate_content("Hello, can you respond?")
|
| 45 |
+
return "β
API Key is valid!" if response.text else "β οΈ API responded but empty."
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"β Error: {str(e)}"
|
| 48 |
+
|
| 49 |
+
# Function to generate a prompt using Gemini
|
| 50 |
+
def generate_prompt(image, api_key, style):
|
| 51 |
+
if not api_key:
|
| 52 |
+
return "β Error: Please enter your Google Gemini API key."
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
genai.configure(api_key=api_key)
|
| 56 |
+
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 57 |
+
|
| 58 |
+
# Convert image to base64
|
| 59 |
+
image_bytes = io.BytesIO()
|
| 60 |
+
image.save(image_bytes, format=image.format if image.format else "PNG")
|
| 61 |
+
image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
|
| 62 |
+
|
| 63 |
+
instruction = STYLE_INSTRUCTIONS.get(style, STYLE_INSTRUCTIONS["General"])
|
| 64 |
+
|
| 65 |
+
response = model.generate_content([
|
| 66 |
+
instruction,
|
| 67 |
+
{"mime_type": "image/png", "data": image_base64}
|
| 68 |
+
])
|
| 69 |
+
|
| 70 |
+
return response.text if response.text else "β οΈ No response generated."
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"β Error generating prompt: {str(e)}"
|
| 73 |
+
|
| 74 |
+
# Gradio Interface
|
| 75 |
+
def main():
|
| 76 |
+
with gr.Blocks() as app:
|
| 77 |
+
gr.Markdown("## π¨ Flux Prompt Generator with Gemini 1.5 Pro")
|
| 78 |
+
|
| 79 |
+
api_key = gr.Textbox(label="π Google Gemini API Key", type="password", placeholder="Paste your Gemini API key here")
|
| 80 |
+
check_button = gr.Button("β
Check API Key")
|
| 81 |
+
check_output = gr.Textbox(label="API Key Status")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column():
|
| 85 |
+
image_input = gr.Image(label="π· Upload Design Image", type="pil", width=300)
|
| 86 |
+
style_dropdown = gr.Dropdown(label="π¨ Style", choices=list(STYLE_INSTRUCTIONS.keys()), value="General")
|
| 87 |
+
|
| 88 |
+
generate_button = gr.Button("π§ Generate Prompt")
|
| 89 |
+
prompt_output = gr.Textbox(label="π Generated Flux Prompt", lines=6)
|
| 90 |
+
copy_button = gr.Button("π Copy to Clipboard")
|
| 91 |
+
|
| 92 |
+
def copy_prompt(prompt):
|
| 93 |
+
return gr.update(value=prompt, interactive=True)
|
| 94 |
+
|
| 95 |
+
check_button.click(check_api, inputs=api_key, outputs=check_output)
|
| 96 |
+
generate_button.click(generate_prompt, inputs=[image_input, api_key, style_dropdown], outputs=prompt_output)
|
| 97 |
+
copy_button.click(copy_prompt, inputs=prompt_output, outputs=prompt_output)
|
| 98 |
+
|
| 99 |
+
return app
|
| 100 |
+
|
| 101 |
+
# Launch the app
|
| 102 |
+
app = main()
|
| 103 |
+
app.launch()
|