mroccuper commited on
Commit
2dea97c
Β·
verified Β·
1 Parent(s): 9d3c685

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -58
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 optimized for Hugging Face
10
- MAX_RETRIES = 3
11
- TIMEOUT = 10 # Keep under 20s Hugging Face limit
 
12
 
13
- # Simplified style instructions for faster processing
14
- STYLE_INSTRUCTIONS = {
15
- "General": "Describe as Flux prompt focusing on key elements",
16
- "Vector": "Clean vector style with sharp lines and B&W",
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
- for attempt in range(MAX_RETRIES):
28
- try:
29
- start_time = time.time()
30
-
31
- # Configure API
32
- genai.configure(api_key=api_key)
33
- model = genai.GenerativeModel('gemini-pro-vision')
34
-
35
- # Optimize image
36
- img = optimize_image(image)
37
- img_byte_arr = io.BytesIO()
38
- img.save(img_byte_arr, format='JPEG', quality=85)
39
-
40
- # Timeout control
41
- if time.time() - start_time > TIMEOUT - 2:
42
- raise TimeoutError("Image processing too slow")
43
-
44
- response = model.generate_content([
45
- STYLE_INSTRUCTIONS[style],
46
- {"mime_type": "image/jpeg", "data": img_byte_arr.getvalue()}
47
- ], request_options={"timeout": TIMEOUT})
48
-
49
- return response.text
50
-
51
- except Exception as e:
52
- if attempt == MAX_RETRIES - 1:
53
- return f"Error: {str(e)}"
54
- time.sleep(1)
 
 
 
 
 
55
 
56
- # Hugging Face optimized interface
57
- with gr.Blocks() as app:
58
- gr.Markdown("## πŸš€ HF-Friendly Flux Generator")
59
 
60
  with gr.Row():
61
- api_key = gr.Textbox(label="Gemini Key", type="password")
62
- style = gr.Dropdown(list(STYLE_INSTRUCTIONS), label="Style")
 
 
 
 
 
 
 
 
63
 
64
- image_input = gr.Image(type="pil", label="Upload Design")
65
- generate_btn = gr.Button("Generate", variant="primary")
66
- output = gr.Textbox(label="Prompt", interactive=False)
 
 
 
 
 
 
 
 
 
 
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
- # HF-specific launch settings
76
- app.queue(concurrency_count=3, max_size=20).launch(debug=True)
 
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)