Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,75 +2,88 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import google.generativeai as genai
|
4 |
from PIL import Image
|
5 |
-
import io
|
6 |
-
import base64
|
7 |
-
import time
|
8 |
|
9 |
-
# Configuration
|
10 |
-
|
11 |
-
TIMEOUT =
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
"Realistic": "Photorealistic details with accurate textures",
|
18 |
-
}
|
19 |
-
|
20 |
-
def optimize_image(img):
|
21 |
-
"""Fast image optimization for web"""
|
22 |
-
img = img.convert("RGB")
|
23 |
-
img.thumbnail((512, 512)) # Reduce image size
|
24 |
return img
|
25 |
|
26 |
def generate_prompt(image, api_key, style):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
#
|
57 |
-
with gr.Blocks() as app:
|
58 |
-
gr.Markdown("
|
59 |
|
60 |
with gr.Row():
|
61 |
-
api_key = gr.Textbox(
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
image_input = gr.Image(
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
generate_btn.click(
|
69 |
-
generate_prompt,
|
70 |
inputs=[image_input, api_key, style],
|
71 |
-
outputs=output
|
72 |
-
concurrency_limit=3 # Better HF resource management
|
73 |
)
|
74 |
|
75 |
-
#
|
76 |
-
app.
|
|
|
2 |
import gradio as gr
|
3 |
import google.generativeai as genai
|
4 |
from PIL import Image
|
|
|
|
|
|
|
5 |
|
6 |
+
# Configuration for Hugging Face Spaces
|
7 |
+
MAX_SIZE = 512 # Reduced image size for HF memory limits
|
8 |
+
TIMEOUT = 18 # Stay safely under 20s timeout
|
9 |
+
STYLES = ["General", "Vector", "Realistic", "Kawaii"]
|
10 |
|
11 |
+
def process_image(image):
|
12 |
+
"""Optimize image for Hugging Face environment"""
|
13 |
+
img = image.convert("RGB")
|
14 |
+
img.thumbnail((MAX_SIZE, MAX_SIZE))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return img
|
16 |
|
17 |
def generate_prompt(image, api_key, style):
|
18 |
+
try:
|
19 |
+
# Validate inputs
|
20 |
+
if not image:
|
21 |
+
return "β οΈ Please upload an image first"
|
22 |
+
if not api_key:
|
23 |
+
return "π API key required"
|
24 |
+
|
25 |
+
# Process image
|
26 |
+
img = process_image(image)
|
27 |
+
|
28 |
+
# Configure Gemini
|
29 |
+
genai.configure(api_key=api_key)
|
30 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
31 |
+
|
32 |
+
# Create prompt based on style
|
33 |
+
base_prompt = "Describe this design concisely for Flux 1.1 Pro"
|
34 |
+
if style == "Vector":
|
35 |
+
instruction = f"{base_prompt} in clean vector style with sharp lines"
|
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 |
+
contents=[instruction, img],
|
44 |
+
request_options={"timeout": TIMEOUT}
|
45 |
+
)
|
46 |
+
|
47 |
+
return response.text if response.text else "β No response generated"
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
return f"β οΈ Error: {str(e)}"
|
51 |
|
52 |
+
# Gradio interface optimized for Hugging Face
|
53 |
+
with gr.Blocks(title="Flux Prompt Generator") as app:
|
54 |
+
gr.Markdown("# π¨ Flux AI Prompt Generator")
|
55 |
|
56 |
with gr.Row():
|
57 |
+
api_key = gr.Textbox(
|
58 |
+
label="Google Gemini API Key",
|
59 |
+
type="password",
|
60 |
+
placeholder="Enter your API key..."
|
61 |
+
)
|
62 |
+
style = gr.Dropdown(
|
63 |
+
STYLES,
|
64 |
+
value="General",
|
65 |
+
label="Design Style"
|
66 |
+
)
|
67 |
|
68 |
+
image_input = gr.Image(
|
69 |
+
label="Upload Your Design",
|
70 |
+
type="pil",
|
71 |
+
height=300
|
72 |
+
)
|
73 |
+
|
74 |
+
generate_btn = gr.Button("Generate Prompt", variant="primary")
|
75 |
+
|
76 |
+
output = gr.Textbox(
|
77 |
+
label="Generated Prompt",
|
78 |
+
placeholder="Your prompt will appear here...",
|
79 |
+
lines=5
|
80 |
+
)
|
81 |
|
82 |
generate_btn.click(
|
83 |
+
fn=generate_prompt,
|
84 |
inputs=[image_input, api_key, style],
|
85 |
+
outputs=output
|
|
|
86 |
)
|
87 |
|
88 |
+
# Hugging Face compatible launch settings
|
89 |
+
app.launch(debug=False, show_error=True)
|