File size: 2,869 Bytes
b511d75
8d1bd48
 
9d3c685
83a7945
 
b511d75
83a7945
 
 
 
8d1bd48
83a7945
 
 
 
 
 
b511d75
83a7945
2dea97c
83a7945
 
 
 
 
 
 
2dea97c
83a7945
2dea97c
3d66bed
2dea97c
83a7945
 
 
 
2dea97c
83a7945
 
2dea97c
 
 
83a7945
 
2dea97c
83a7945
9d3c685
83a7945
 
 
9d3c685
 
83a7945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dea97c
83a7945
 
 
 
2dea97c
83a7945
 
9d3c685
83a7945
 
2dea97c
9d3c685
83a7945
 
 
 
 
 
 
9d3c685
83a7945
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
)