Upload app(1).py
Browse files
app(1).py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
# Instantiate a Client object from gradio_client pointing to the 'selfit-camera/Omni-Image-Editor' Space.
|
| 6 |
+
client = Client("selfit-camera/Omni-Image-Editor")
|
| 7 |
+
|
| 8 |
+
# Define a Python function for text-to-image generation
|
| 9 |
+
def generate_image(prompt):
|
| 10 |
+
"""
|
| 11 |
+
Generate an image from a text prompt using the Omni Image Editor API.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
prompt (str): Text description of the image to generate
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
str: URL of the generated image or error message
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
# Call the client.predict() method with the user's prompt, aspect_ratio='16:9', and api_name='/text_to_image_interface'.
|
| 21 |
+
result = client.predict(
|
| 22 |
+
prompt=prompt,
|
| 23 |
+
aspect_ratio="16:9",
|
| 24 |
+
api_name="/text_to_image_interface"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# The predict method returns a tuple. The first element of this tuple is an HTML string containing the image.
|
| 28 |
+
# Extract the image URL from this HTML string.
|
| 29 |
+
html_string = result[0]
|
| 30 |
+
match = re.search(r"src='([^']+)'", html_string)
|
| 31 |
+
if match:
|
| 32 |
+
image_url = match.group(1)
|
| 33 |
+
return image_url
|
| 34 |
+
else:
|
| 35 |
+
# Handle cases where the URL might not be found
|
| 36 |
+
return "https://via.placeholder.com/400x200?text=Error:Image+Not+Found"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error generating image: {str(e)}"
|
| 39 |
+
|
| 40 |
+
# Define a Python function for image editing
|
| 41 |
+
def edit_image(input_image, edit_prompt):
|
| 42 |
+
"""
|
| 43 |
+
Edit an image based on a text prompt using the Omni Image Editor API.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
input_image (str): Path to the image file or image object
|
| 47 |
+
edit_prompt (str): Text description of the edits to apply
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
str: URL of the edited image or error message
|
| 51 |
+
"""
|
| 52 |
+
try:
|
| 53 |
+
if input_image is None:
|
| 54 |
+
return "Please upload an image first"
|
| 55 |
+
|
| 56 |
+
# Use handle_file to properly handle the image upload
|
| 57 |
+
result = client.predict(
|
| 58 |
+
input_image=handle_file(input_image),
|
| 59 |
+
prompt=edit_prompt,
|
| 60 |
+
api_name="/edit_image_interface"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Extract the image URL from the HTML response
|
| 64 |
+
if isinstance(result, tuple) and len(result) > 0:
|
| 65 |
+
html_string = result[0]
|
| 66 |
+
match = re.search(r"src='([^']+)'", html_string)
|
| 67 |
+
if match:
|
| 68 |
+
image_url = match.group(1)
|
| 69 |
+
return image_url
|
| 70 |
+
else:
|
| 71 |
+
return "https://via.placeholder.com/400x200?text=Error:Image+Not+Found"
|
| 72 |
+
else:
|
| 73 |
+
return str(result)
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return f"Error editing image: {str(e)}"
|
| 77 |
+
|
| 78 |
+
# Create a Gradio application using gr.Blocks for more granular control.
|
| 79 |
+
with gr.Blocks(
|
| 80 |
+
title='Omni Image Editor with Gradio',
|
| 81 |
+
theme=gr.themes.Soft()
|
| 82 |
+
) as demo:
|
| 83 |
+
gr.Markdown("# Omni Image Editor Studio")
|
| 84 |
+
gr.Markdown("Generate images from text descriptions or edit existing images with AI-powered tools.")
|
| 85 |
+
|
| 86 |
+
with gr.Tabs():
|
| 87 |
+
# Text-to-Image Tab
|
| 88 |
+
with gr.TabItem("Text to Image Generator"):
|
| 89 |
+
gr.Markdown("### Generate Images from Text")
|
| 90 |
+
gr.Markdown("Describe the image you want to generate in detail for best results.")
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
prompt_input = gr.Textbox(
|
| 95 |
+
label='Image Description',
|
| 96 |
+
placeholder='e.g., A futuristic city at sunset with flying cars, neon lights, cyberpunk style, high quality',
|
| 97 |
+
lines=3
|
| 98 |
+
)
|
| 99 |
+
generate_btn = gr.Button("🎨 Generate Image", variant="primary")
|
| 100 |
+
|
| 101 |
+
generated_image = gr.Image(label='Generated Image', type='filepath')
|
| 102 |
+
|
| 103 |
+
# Bind the generate_image function to the button click event
|
| 104 |
+
generate_btn.click(
|
| 105 |
+
fn=generate_image,
|
| 106 |
+
inputs=[prompt_input],
|
| 107 |
+
outputs=[generated_image]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Image Editing Tab
|
| 111 |
+
with gr.TabItem("Image Editor"):
|
| 112 |
+
gr.Markdown("### Edit Images with AI")
|
| 113 |
+
gr.Markdown("Upload an image and describe the changes you want to make.")
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
input_image = gr.Image(
|
| 118 |
+
label='Upload Image',
|
| 119 |
+
type='filepath'
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column():
|
| 124 |
+
edit_prompt = gr.Textbox(
|
| 125 |
+
label='Edit Instructions',
|
| 126 |
+
placeholder='e.g., Change the sky to sunset colors, add stars, increase contrast',
|
| 127 |
+
lines=3
|
| 128 |
+
)
|
| 129 |
+
edit_btn = gr.Button("✨ Edit Image", variant="primary")
|
| 130 |
+
|
| 131 |
+
edited_image = gr.Image(label='Edited Image', type='filepath')
|
| 132 |
+
|
| 133 |
+
# Bind the edit_image function to the button click event
|
| 134 |
+
edit_btn.click(
|
| 135 |
+
fn=edit_image,
|
| 136 |
+
inputs=[input_image, edit_prompt],
|
| 137 |
+
outputs=[edited_image]
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Launch the Gradio application.
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
demo.launch(share=True)
|