Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import google.generativeai as genai | |
from PIL import Image | |
# Configuration for Hugging Face Spaces | |
MAX_SIZE = 512 # Reduced image size for HF memory limits | |
TIMEOUT = 18 # Stay safely under 20s timeout | |
STYLES = ["General", "Vector", "Realistic", "Kawaii"] | |
def process_image(image): | |
"""Optimize image for Hugging Face environment""" | |
img = image.convert("RGB") | |
img.thumbnail((MAX_SIZE, MAX_SIZE)) | |
return img | |
def generate_prompt(image, api_key, style): | |
try: | |
# Validate inputs | |
if not image: | |
return "β οΈ Please upload an image first" | |
if not api_key: | |
return "π API key required" | |
# Process image | |
img = process_image(image) | |
# Configure Gemini | |
genai.configure(api_key=api_key) | |
model = genai.GenerativeModel('gemini-pro-vision') | |
# Create prompt based on style | |
base_prompt = "Describe this design concisely for Flux 1.1 Pro" | |
if style == "Vector": | |
instruction = f"{base_prompt} in clean vector style with sharp lines" | |
elif style == "Realistic": | |
instruction = f"{base_prompt} with photorealistic details" | |
else: | |
instruction = base_prompt | |
# Generate with timeout safety | |
response = model.generate_content( | |
contents=[instruction, img], | |
request_options={"timeout": TIMEOUT} | |
) | |
return response.text if response.text else "β No response generated" | |
except Exception as e: | |
return f"β οΈ Error: {str(e)}" | |
# Gradio interface optimized for Hugging Face | |
with gr.Blocks(title="Flux Prompt Generator") as app: | |
gr.Markdown("# π¨ Flux AI Prompt Generator") | |
with gr.Row(): | |
api_key = gr.Textbox( | |
label="Google Gemini API Key", | |
type="password", | |
placeholder="Enter your API key..." | |
) | |
style = gr.Dropdown( | |
STYLES, | |
value="General", | |
label="Design Style" | |
) | |
image_input = gr.Image( | |
label="Upload Your Design", | |
type="pil", | |
height=300 | |
) | |
generate_btn = gr.Button("Generate Prompt", variant="primary") | |
output = gr.Textbox( | |
label="Generated Prompt", | |
placeholder="Your prompt will appear here...", | |
lines=5 | |
) | |
generate_btn.click( | |
fn=generate_prompt, | |
inputs=[image_input, api_key, style], | |
outputs=output | |
) | |
# Hugging Face compatible launch settings | |
app.launch(debug=False, show_error=True) |