File size: 2,050 Bytes
55ecfb5
 
ea5da81
 
 
 
 
55ecfb5
 
 
 
 
 
 
ea5da81
 
 
 
 
 
 
 
 
 
55ecfb5
 
 
 
ea5da81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55ecfb5
 
 
 
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
from optimum.intel import OVStableDiffusionPipeline  # Changed from OVPipelineForText2Image
from openvino import Core, Model  # Updated to use non-deprecated openvino module
import gradio as gr

# Load the model
model_id = "AIFunOver/FLUX.1-dev-openvino-fp16"
try:
    model = OVStableDiffusionPipeline.from_pretrained(
        model_id,
        export=True,  # Enable export to OpenVINO IR if needed
        device="AUTO"  # Use AUTO for flexible device selection
    )
    # Save the model after export to avoid re-conversion
    model.save_pretrained("./flux1_dev_openvino_fp16")
except Exception as e:
    print(f"Error loading model: {e}")
    exit(1)

# Define the image generation function
def generate_image(prompt, num_inference_steps=50, guidance_scale=7.5):
    try:
        # Generate image using the model
        result = model(
            prompt=prompt,
            num_inference_steps=int(num_inference_steps),  # Ensure integer input
            guidance_scale=float(guidance_scale),  # Ensure float input
            height=512,
            width=512
        )
        image = result.images[0]  # Extract the generated image
        return image
    except Exception as e:
        return f"Error generating image: {e}"

# Set up Gradio interface
interface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Enter your image description here..."),
        gr.Slider(label="Inference Steps", minimum=1, maximum=100, value=50, step=1),
        gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=7.5, step=0.1)
    ],
    outputs=gr.Image(label="Generated Image"),
    title="FLUX.1-dev Image Generation with OpenVINO",
    description="Enter a text prompt to generate an image using the FLUX.1-dev model optimized with OpenVINO."
)

# Launch the Gradio app
if __name__ == "__main__":
    try:
        interface.launch(server_name="0.0.0.0", server_port=7860)  # Explicit server settings
    except Exception as e:
        print(f"Error launching Gradio interface: {e}")