Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,88 +2,101 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
MAX_SIZE =
|
| 8 |
-
TIMEOUT =
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
def
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
img = process_image(image)
|
| 27 |
|
| 28 |
-
# Configure
|
| 29 |
genai.configure(api_key=api_key)
|
| 30 |
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
elif style == "Realistic":
|
| 37 |
-
instruction = f"{base_prompt} with photorealistic details"
|
| 38 |
-
else:
|
| 39 |
-
instruction = base_prompt
|
| 40 |
-
|
| 41 |
-
# Generate with timeout safety
|
| 42 |
response = model.generate_content(
|
| 43 |
-
|
|
|
|
| 44 |
request_options={"timeout": TIMEOUT}
|
| 45 |
)
|
| 46 |
|
| 47 |
-
return response.text if response.text else "
|
| 48 |
-
|
| 49 |
except Exception as e:
|
| 50 |
-
return f"
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
with gr.Blocks(title="Flux
|
| 54 |
-
gr.Markdown("
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
output = gr.Textbox(
|
| 77 |
-
label="Generated Prompt",
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
)
|
| 81 |
-
|
|
|
|
| 82 |
generate_btn.click(
|
| 83 |
-
|
| 84 |
-
inputs=[
|
| 85 |
outputs=output
|
| 86 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
app.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
+
# Hugging Face-safe configuration
|
| 9 |
+
MAX_SIZE = 768
|
| 10 |
+
TIMEOUT = 15
|
| 11 |
+
CONCURRENCY = 2 # Reduced for HF stability
|
| 12 |
|
| 13 |
+
STYLE_INSTRUCTIONS = {
|
| 14 |
+
"General": "Describe this design concisely for Flux",
|
| 15 |
+
"Vector": "Clean vector style with sharp lines",
|
| 16 |
+
"Realistic": "Photorealistic details",
|
| 17 |
+
"Kawaii": "Cute cartoon style with pastel colors"
|
| 18 |
+
}
|
| 19 |
|
| 20 |
+
def hf_safe_generate(image, api_key, style):
|
| 21 |
try:
|
| 22 |
+
start_time = time.time()
|
| 23 |
+
|
| 24 |
+
# Convert and resize image
|
| 25 |
+
img = image.convert("RGB")
|
| 26 |
+
img.thumbnail((MAX_SIZE, MAX_SIZE))
|
| 27 |
+
buffer = io.BytesIO()
|
| 28 |
+
img.save(buffer, format="JPEG", quality=85)
|
|
|
|
| 29 |
|
| 30 |
+
# Configure API
|
| 31 |
genai.configure(api_key=api_key)
|
| 32 |
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 33 |
|
| 34 |
+
# Timeout check
|
| 35 |
+
if time.time() - start_time > 5:
|
| 36 |
+
return "Error: Image processing too slow"
|
| 37 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
response = model.generate_content(
|
| 39 |
+
[STYLE_INSTRUCTIONS[style],
|
| 40 |
+
{"mime_type": "image/jpeg", "data": buffer.getvalue()},
|
| 41 |
request_options={"timeout": TIMEOUT}
|
| 42 |
)
|
| 43 |
|
| 44 |
+
return response.text[:500] if response.text else "No response"
|
| 45 |
+
|
| 46 |
except Exception as e:
|
| 47 |
+
return f"Error: {str(e)}"
|
| 48 |
|
| 49 |
+
# Enhanced interface with original features
|
| 50 |
+
with gr.Blocks(title="Flux Pro HF", theme=gr.themes.Soft()) as app:
|
| 51 |
+
gr.Markdown("## π **Flux Prompt Generator**")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
api_key = gr.Textbox(
|
| 56 |
+
label="π Gemini API Key",
|
| 57 |
+
type="password",
|
| 58 |
+
placeholder="Enter your API key..."
|
| 59 |
+
)
|
| 60 |
+
style = gr.Dropdown(
|
| 61 |
+
list(STYLE_INSTRUCTIONS.keys()),
|
| 62 |
+
value="General",
|
| 63 |
+
label="π¨ Design Style",
|
| 64 |
+
interactive=True
|
| 65 |
+
)
|
| 66 |
+
image = gr.Image(
|
| 67 |
+
label="πΌοΈ Upload Design",
|
| 68 |
+
type="pil",
|
| 69 |
+
height=300
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
generate_btn = gr.Button("β¨ Generate Prompt", variant="primary")
|
| 74 |
+
copy_btn = gr.Button("π Copy")
|
| 75 |
+
|
| 76 |
output = gr.Textbox(
|
| 77 |
+
label="π Generated Prompt",
|
| 78 |
+
lines=5,
|
| 79 |
+
max_lines=8,
|
| 80 |
+
interactive=False
|
| 81 |
)
|
| 82 |
+
|
| 83 |
+
# Event handlers
|
| 84 |
generate_btn.click(
|
| 85 |
+
hf_safe_generate,
|
| 86 |
+
inputs=[image, api_key, style],
|
| 87 |
outputs=output
|
| 88 |
)
|
| 89 |
+
|
| 90 |
+
copy_btn.click(
|
| 91 |
+
lambda x: x,
|
| 92 |
+
inputs=output,
|
| 93 |
+
outputs=output,
|
| 94 |
+
_js="(text) => navigator.clipboard.writeText(text)"
|
| 95 |
+
)
|
| 96 |
|
| 97 |
+
# Fixed queue configuration for Hugging Face
|
| 98 |
+
app.queue(concurrency_count=CONCURRENCY).launch(
|
| 99 |
+
debug=False,
|
| 100 |
+
max_threads=1,
|
| 101 |
+
show_error=True
|
| 102 |
+
)
|