Describer-Pro / app.py
mroccuper's picture
Update app.py
83a7945 verified
raw
history blame
2.87 kB
import os
import gradio as gr
import google.generativeai as genai
from PIL import Image
import io
import time
# Hugging Face-safe configuration
MAX_SIZE = 768
TIMEOUT = 15
CONCURRENCY = 2 # Reduced for HF stability
STYLE_INSTRUCTIONS = {
"General": "Describe this design concisely for Flux",
"Vector": "Clean vector style with sharp lines",
"Realistic": "Photorealistic details",
"Kawaii": "Cute cartoon style with pastel colors"
}
def hf_safe_generate(image, api_key, style):
try:
start_time = time.time()
# Convert and resize image
img = image.convert("RGB")
img.thumbnail((MAX_SIZE, MAX_SIZE))
buffer = io.BytesIO()
img.save(buffer, format="JPEG", quality=85)
# Configure API
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-1.5-pro')
# Timeout check
if time.time() - start_time > 5:
return "Error: Image processing too slow"
response = model.generate_content(
[STYLE_INSTRUCTIONS[style],
{"mime_type": "image/jpeg", "data": buffer.getvalue()},
request_options={"timeout": TIMEOUT}
)
return response.text[:500] if response.text else "No response"
except Exception as e:
return f"Error: {str(e)}"
# Enhanced interface with original features
with gr.Blocks(title="Flux Pro HF", theme=gr.themes.Soft()) as app:
gr.Markdown("## πŸš€ **Flux Prompt Generator**")
with gr.Row():
with gr.Column():
api_key = gr.Textbox(
label="πŸ”‘ Gemini API Key",
type="password",
placeholder="Enter your API key..."
)
style = gr.Dropdown(
list(STYLE_INSTRUCTIONS.keys()),
value="General",
label="🎨 Design Style",
interactive=True
)
image = gr.Image(
label="πŸ–ΌοΈ Upload Design",
type="pil",
height=300
)
with gr.Row():
generate_btn = gr.Button("✨ Generate Prompt", variant="primary")
copy_btn = gr.Button("πŸ“‹ Copy")
output = gr.Textbox(
label="πŸ“ Generated Prompt",
lines=5,
max_lines=8,
interactive=False
)
# Event handlers
generate_btn.click(
hf_safe_generate,
inputs=[image, api_key, style],
outputs=output
)
copy_btn.click(
lambda x: x,
inputs=output,
outputs=output,
_js="(text) => navigator.clipboard.writeText(text)"
)
# Fixed queue configuration for Hugging Face
app.queue(concurrency_count=CONCURRENCY).launch(
debug=False,
max_threads=1,
show_error=True
)