a
File size: 7,500 Bytes
7975c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98c74d2
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import gradio as gr
from gradio_client import Client, handle_file
import re

# Instantiate a Client object from gradio_client pointing to the 'selfit-camera/Omni-Image-Editor' Space.
client = Client("selfit-camera/Omni-Image-Editor")

# Define a Python function for text-to-image generation
def generate_image(prompt):
    """
    Generate an image from a text prompt using the Omni Image Editor API.
    
    Args:
        prompt (str): Text description of the image to generate
        
    Returns:
        str: URL of the generated image or error message
    """
    try:
        # Call the client.predict() method with the user's prompt, aspect_ratio='16:9', and api_name='/text_to_image_interface'.
        result = client.predict(
            prompt=prompt,
            aspect_ratio="16:9",
            api_name="/text_to_image_interface"
        )

        # The predict method returns a tuple. The first element of this tuple is an HTML string containing the image.
        # Extract the image URL from this HTML string.
        html_string = result[0]
        match = re.search(r"src='([^']+)'", html_string)
        if match:
            image_url = match.group(1)
            return image_url
        else:
            # Handle cases where the URL might not be found
            return "https://via.placeholder.com/400x200?text=Error:Image+Not+Found"
    except Exception as e:
        return f"Error generating image: {str(e)}"

# Define a Python function for image editing
def edit_image(input_image, edit_prompt):
    """
    Edit an image based on a text prompt using the Omni Image Editor API.
    
    Args:
        input_image (str): Path to the image file or image object
        edit_prompt (str): Text description of the edits to apply
        
    Returns:
        str: URL of the edited image or error message
    """
    try:
        if input_image is None:
            return "Please upload an image first"
        
        # Use handle_file to properly handle the image upload
        result = client.predict(
            input_image=handle_file(input_image),
            prompt=edit_prompt,
            api_name="/edit_image_interface"
        )
        
        # Extract the image URL from the HTML response
        if isinstance(result, tuple) and len(result) > 0:
            html_string = result[0]
            match = re.search(r"src='([^']+)'", html_string)
            if match:
                image_url = match.group(1)
                return image_url
            else:
                return "https://via.placeholder.com/400x200?text=Error:Image+Not+Found"
        else:
            return str(result)
            
    except Exception as e:
        return f"Error editing image: {str(e)}"

# Define a Python function for image upscaling
def upscale_image(input_image):
    """
    Upscale an image to higher resolution using the Omni Image Editor API.
    
    Args:
        input_image (str): Path to the image file or image object to upscale
        
    Returns:
        str: URL of the upscaled image or error message
    """
    try:
        if input_image is None:
            return "Please upload an image first"
        
        # Use handle_file to properly handle the image upload
        result = client.predict(
            input_image=handle_file(input_image),
            api_name="/image_upscale_interface"
        )
        
        # Extract the image URL from the HTML response
        if isinstance(result, tuple) and len(result) > 0:
            html_string = result[0]
            match = re.search(r"src='([^']+)'", html_string)
            if match:
                image_url = match.group(1)
                return image_url
            else:
                return "https://via.placeholder.com/400x200?text=Error:Image+Not+Found"
        else:
            return str(result)
            
    except Exception as e:
        return f"Error upscaling image: {str(e)}"

# Create a Gradio application using gr.Blocks for more granular control.
with gr.Blocks(
    title='Omni Image Editor with Gradio',
    theme=gr.themes.Soft()
) as demo:
    gr.Markdown("# Omni Image Editor Studio")
    gr.Markdown("Generate images from text descriptions or edit existing images with AI-powered tools.")
    
    with gr.Tabs():
        # Text-to-Image Tab
        with gr.TabItem("Text to Image Generator"):
            gr.Markdown("### Generate Images from Text")
            gr.Markdown("Describe the image you want to generate in detail for best results.")
            
            with gr.Row():
                with gr.Column():
                    prompt_input = gr.Textbox(
                        label='Image Description',
                        placeholder='e.g., A futuristic city at sunset with flying cars, neon lights, cyberpunk style, high quality',
                        lines=3
                    )
                    generate_btn = gr.Button("🎨 Generate Image", variant="primary")
            
            generated_image = gr.Image(label='Generated Image', type='filepath')
            
            # Bind the generate_image function to the button click event
            generate_btn.click(
                fn=generate_image,
                inputs=[prompt_input],
                outputs=[generated_image]
            )
        
        # Image Editing Tab
        with gr.TabItem("Image Editor"):
            gr.Markdown("### Edit Images with AI")
            gr.Markdown("Upload an image and describe the changes you want to make.")
            
            with gr.Row():
                with gr.Column():
                    input_image = gr.Image(
                        label='Upload Image',
                        type='filepath'
                    )
                    
            with gr.Row():
                with gr.Column():
                    edit_prompt = gr.Textbox(
                        label='Edit Instructions',
                        placeholder='e.g., Change the sky to sunset colors, add stars, increase contrast',
                        lines=3
                    )
                    edit_btn = gr.Button("✨ Edit Image", variant="primary")
            
            edited_image = gr.Image(label='Edited Image', type='filepath')
            
            # Bind the edit_image function to the button click event
            edit_btn.click(
                fn=edit_image,
                inputs=[input_image, edit_prompt],
                outputs=[edited_image]
            )
        
        # Image Upscaling Tab
        with gr.TabItem("Image Upscaler"):
            gr.Markdown("### Upscale Images to Higher Resolution")
            gr.Markdown("Upload an image and enhance it to higher resolution using AI-powered upscaling.")
            
            with gr.Row():
                with gr.Column():
                    upscale_input = gr.Image(
                        label='Upload Image to Upscale',
                        type='filepath'
                    )
                    upscale_btn = gr.Button("⬆️ Upscale Image", variant="primary")
            
            upscaled_image = gr.Image(label='Upscaled Image', type='filepath')
            
            # Bind the upscale_image function to the button click event
            upscale_btn.click(
                fn=upscale_image,
                inputs=[upscale_input],
                outputs=[upscaled_image]
            )

# Launch the Gradio application.
if __name__ == "__main__":
    demo.launch(Share=True)