5
File size: 2,274 Bytes
dd16cb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from gradio_client import Client
import gradio as gr

# Initialize Gradio Client
client = Client("prithivMLmods/FireRed-Image-Edit-1.0-Fast")

# Define Prediction Function
def predict_image(images, prompt, seed, randomize_seed, guidance_scale, steps):
    """
    Calls the external model's /infer endpoint using the Gradio client
    and returns the prediction result.

    Args:
        images: Input image(s).
        prompt: Text prompt for image editing.
        seed: Random seed.
        randomize_seed: Boolean to randomize seed.
        guidance_scale: Guidance scale for the model.
        steps: Number of inference steps.

    Returns:
        The prediction result from the model (e.g., an image).
    """
    try:
        # Ensure images is always a list, even if only one image is provided
        images_list = [images] if not isinstance(images, list) else images
        result = client.predict(
            images_list,
            prompt,
            seed,
            randomize_seed,
            guidance_scale,
            steps,
            api_name='/infer'
        )
        return result
    except Exception as e:
        print(f"Error during prediction: {e}")
        return None

# Define input components
input_images = gr.Image(type="filepath", label="Input Image")
input_prompt = gr.Textbox(label="Prompt")
input_seed = gr.Slider(minimum=0, maximum=2147483647, step=1, label="Seed", value=0)
input_randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
input_guidance_scale = gr.Slider(minimum=0.0, maximum=20.0, step=0.1, label="Guidance Scale", value=7.5)
input_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Inference Steps", value=20)

# Create a list of input components
input_components = [
    input_images,
    input_prompt,
    input_seed,
    input_randomize_seed,
    input_guidance_scale,
    input_steps
]

# Define the output component
output_image = gr.Image(label="Edited Image")

# Create the Gradio interface
iface = gr.Interface(
    fn=predict_image,
    inputs=input_components,
    outputs=output_image,
    title="FireRed Image Editor"
)

# Launch the Gradio app (optional for local testing, not needed for Spaces deployment if app.py is run directly)
if __name__ == "__main__":
    iface.launch()