File size: 2,609 Bytes
ece86db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import warnings
warnings.filterwarnings("ignore")
import gradio as gr
from gradio_client import Client, handle_file

def generate_image(prompt, image_url=None, image_file=None):
    # Initialize the client
    client = Client("yanze/PuLID-FLUX")

    # Determine input image
    if image_url:
        id_image = handle_file(image_url)
    elif image_file:
        id_image = handle_file(image_file.name)
    else:
        return "Error: Please provide an image URL or upload an image file."

    # Predict
    try:
        result = client.predict(
            prompt=prompt,
            id_image=id_image,
            start_step=0,
            guidance=2,
            seed="-1",
            true_cfg=1,
            width=896,
            height=1152,
            num_steps=20,
            id_weight=1,
            neg_prompt="bad quality, worst quality, text, signature, watermark, extra limbs",
            timestep_to_start_cfg=1,
            max_sequence_length=128,
            api_name="/generate_image"
        )

        # Extract the base URL and file path
        base_url = "https://yanze-pulid-flux.hf.space/file="
        file_path = result[0]  # The first element contains the file path of the primary result
        full_url = f"{base_url}{file_path}"

        return full_url
    except Exception as e:
        return f"Error during prediction: {str(e)}"

# Gradio interface
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# Image Generation App\nUpload an image or provide an image URL, and enter a prompt to generate a new image.")

        with gr.Row():
            prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt, e.g., portrait, color, cinematic")
            image_url = gr.Textbox(label="Image URL", placeholder="Enter the image URL (optional)")

        image_file = gr.File(label="Upload Image", file_types=["image"])

        with gr.Row():
            submit_button = gr.Button("Generate Image")

        output = gr.Textbox(label="Generated Image URL")
        output_image = gr.Image(label="Generated Image")

        def process(prompt, image_url, image_file):
            result_url = generate_image(prompt, image_url, image_file)
            if result_url.startswith("http"):
                return result_url, result_url
            else:
                return result_url, None

        submit_button.click(
            fn=process,
            inputs=[prompt, image_url, image_file],
            outputs=[output, output_image]
        )

    return demo

if __name__ == "__main__":
    demo = gradio_interface()
    demo.launch()