Spaces:
Runtime error
Runtime error
File size: 5,897 Bytes
9d9968c |
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 |
import gradio as gr
from PIL import Image
import asyncio
from text_to_image import TextToImage
from image_to_text import ImageToText
from image_to_image import ImageToImage
# ============================================
# Initialize Model Classes
# ============================================
text_to_image = TextToImage()
image_to_text = ImageToText()
image_to_image = ImageToImage()
# ============================================
# Gradio Interface Functions with Async and Error Handling
# ============================================
async def async_text_to_image(prompt):
"""
Asynchronous interface function for Text-to-Image generation with error handling.
"""
try:
image = await text_to_image.generate_image(prompt)
return image
except Exception as e:
raise gr.Error(f"Text-to-Image Generation Failed: {str(e)}")
async def async_image_to_text(image):
"""
Asynchronous interface function for Image-to-Text captioning with error handling.
"""
try:
caption = await image_to_text.generate_caption(image)
return caption
except Exception as e:
raise gr.Error(f"Image-to-Text Captioning Failed: {str(e)}")
async def async_image_to_image(image, prompt):
"""
Asynchronous interface function for Image-to-Image transformation with error handling.
"""
try:
transformed_image = await image_to_image.transform_image(image, prompt)
return transformed_image
except Exception as e:
raise gr.Error(f"Image-to-Image Transformation Failed: {str(e)}")
# ============================================
# Gradio UI Design
# ============================================
with gr.Blocks(css=".gradio-container {background-color: #f0f8ff}") as demo:
# Title Section
gr.Markdown("# π¨ AI Creativity Hub π")
gr.Markdown("### Unleash the power of AI to transform your ideas into reality!")
# Task Selection Radio
with gr.Tab("β¨ Choose Your Magic β¨"):
task = gr.Radio(
["πΌοΈ Text-to-Image", "π Image-to-Text", "ποΈ Image-to-Image"],
label="Select a Task",
interactive=True,
value="πΌοΈ Text-to-Image"
)
# Text-to-Image Section
with gr.Row(visible=False) as text_to_image_tab:
with gr.Column():
gr.Markdown("## πΌοΈ Text-to-Image Generator")
prompt_input = gr.Textbox(
label="π Enter your prompt:",
placeholder="e.g., A serene sunset over the mountains",
lines=2
)
generate_btn = gr.Button("π¨ Generate Image")
with gr.Row():
output_image = gr.Image(label="πΌοΈ Generated Image")
download_btn = gr.Button("π₯ Download Image")
# Image-to-Text Section
with gr.Row(visible=False) as image_to_text_tab:
with gr.Column():
gr.Markdown("## π Image-to-Text Captioning")
image_input = gr.Image(
label="πΈ Upload an image:",
type="pil"
)
generate_caption_btn = gr.Button("ποΈ Generate Caption")
caption_output = gr.Textbox(
label="π Generated Caption:",
lines=2
)
# Image-to-Image Section
with gr.Row(visible=False) as image_to_image_tab:
with gr.Column():
gr.Markdown("## ποΈ Image-to-Image Transformer")
init_image_input = gr.Image(
label="πΈ Upload an image:",
type="pil"
)
transformation_prompt = gr.Textbox(
label="π Enter transformation prompt:",
placeholder="e.g., Make it look like a Van Gogh painting",
lines=2
)
transform_btn = gr.Button("π Transform Image")
with gr.Row():
transformed_image = gr.Image(label="ποΈ Transformed Image")
download_transformed_btn = gr.Button("π₯ Download Image")
# Define Visibility Based on Task Selection
def toggle_visibility(selected_task):
return {
text_to_image_tab: selected_task == "πΌοΈ Text-to-Image",
image_to_text_tab: selected_task == "π Image-to-Text",
image_to_image_tab: selected_task == "ποΈ Image-to-Image",
}
task.change(
fn=toggle_visibility,
inputs=task,
outputs=[text_to_image_tab, image_to_text_tab, image_to_image_tab]
)
# Define Button Actions
generate_btn.click(
fn=async_text_to_image,
inputs=prompt_input,
outputs=output_image
)
download_btn.click(
fn=lambda img: img.save("generated_image.png") or "Image downloaded!",
inputs=output_image,
outputs=None
)
generate_caption_btn.click(
fn=async_image_to_text,
inputs=image_input,
outputs=caption_output
)
transform_btn.click(
fn=async_image_to_image,
inputs=[init_image_input, transformation_prompt],
outputs=transformed_image
)
download_transformed_btn.click(
fn=lambda img: img.save("transformed_image.png") or "Image downloaded!",
inputs=transformed_image,
outputs=None
)
# Footer Section with Quirky Elements
gr.Markdown("----")
gr.Markdown("### π Explore the endless possibilities with AI! π")
gr.Markdown("#### π Built with β€οΈ by [Your Name]")
# ============================================
# Launch the Gradio App
# ============================================
demo.launch()
|