Spaces:
Runtime error
Runtime error
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() | |