mroccuper commited on
Commit
83a7945
Β·
verified Β·
1 Parent(s): 3d66bed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -63
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
- # 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-1.5-pro')
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)
 
 
 
 
 
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
+ )