Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,169 +1,169 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
-
import asyncio
|
4 |
-
|
5 |
-
from text_to_image import TextToImage
|
6 |
-
from image_to_text import ImageToText
|
7 |
-
from image_to_image import ImageToImage
|
8 |
-
|
9 |
-
# ============================================
|
10 |
-
# Initialize Model Classes
|
11 |
-
# ============================================
|
12 |
-
|
13 |
-
text_to_image = TextToImage()
|
14 |
-
image_to_text = ImageToText()
|
15 |
-
image_to_image = ImageToImage()
|
16 |
-
|
17 |
-
# ============================================
|
18 |
-
# Gradio Interface Functions with Async and Error Handling
|
19 |
-
# ============================================
|
20 |
-
|
21 |
-
async def async_text_to_image(prompt):
|
22 |
-
"""
|
23 |
-
Asynchronous interface function for Text-to-Image generation with error handling.
|
24 |
-
"""
|
25 |
-
try:
|
26 |
-
image = await text_to_image.generate_image(prompt)
|
27 |
-
return image
|
28 |
-
except Exception as e:
|
29 |
-
raise gr.Error(f"Text-to-Image Generation Failed: {str(e)}")
|
30 |
-
|
31 |
-
async def async_image_to_text(image):
|
32 |
-
"""
|
33 |
-
Asynchronous interface function for Image-to-Text captioning with error handling.
|
34 |
-
"""
|
35 |
-
try:
|
36 |
-
caption = await image_to_text.generate_caption(image)
|
37 |
-
return caption
|
38 |
-
except Exception as e:
|
39 |
-
raise gr.Error(f"Image-to-Text Captioning Failed: {str(e)}")
|
40 |
-
|
41 |
-
async def async_image_to_image(image, prompt):
|
42 |
-
"""
|
43 |
-
Asynchronous interface function for Image-to-Image transformation with error handling.
|
44 |
-
"""
|
45 |
-
try:
|
46 |
-
transformed_image = await image_to_image.transform_image(image, prompt)
|
47 |
-
return transformed_image
|
48 |
-
except Exception as e:
|
49 |
-
raise gr.Error(f"Image-to-Image Transformation Failed: {str(e)}")
|
50 |
-
|
51 |
-
# ============================================
|
52 |
-
# Gradio UI Design
|
53 |
-
# ============================================
|
54 |
-
|
55 |
-
with gr.Blocks(css=".gradio-container {background-color: #
|
56 |
-
# Title Section
|
57 |
-
gr.Markdown("# π¨ AI Creativity Hub π")
|
58 |
-
gr.Markdown("### Unleash the power of AI to transform your ideas into reality!")
|
59 |
-
|
60 |
-
# Task Selection Radio
|
61 |
-
with gr.Tab("β¨ Choose Your Magic β¨"):
|
62 |
-
task = gr.Radio(
|
63 |
-
["πΌοΈ Text-to-Image", "π Image-to-Text", "ποΈ Image-to-Image"],
|
64 |
-
label="Select a Task",
|
65 |
-
interactive=True,
|
66 |
-
value="πΌοΈ Text-to-Image"
|
67 |
-
)
|
68 |
-
|
69 |
-
# Text-to-Image Section
|
70 |
-
with gr.Row(visible=False) as text_to_image_tab:
|
71 |
-
with gr.Column():
|
72 |
-
gr.Markdown("## πΌοΈ Text-to-Image Generator")
|
73 |
-
prompt_input = gr.Textbox(
|
74 |
-
label="π Enter your prompt:",
|
75 |
-
placeholder="e.g., A serene sunset over the mountains",
|
76 |
-
lines=2
|
77 |
-
)
|
78 |
-
generate_btn = gr.Button("π¨ Generate Image")
|
79 |
-
with gr.Row():
|
80 |
-
output_image = gr.Image(label="πΌοΈ Generated Image")
|
81 |
-
download_btn = gr.Button("π₯ Download Image")
|
82 |
-
|
83 |
-
# Image-to-Text Section
|
84 |
-
with gr.Row(visible=False) as image_to_text_tab:
|
85 |
-
with gr.Column():
|
86 |
-
gr.Markdown("## π Image-to-Text Captioning")
|
87 |
-
image_input = gr.Image(
|
88 |
-
label="πΈ Upload an image:",
|
89 |
-
type="pil"
|
90 |
-
)
|
91 |
-
generate_caption_btn = gr.Button("ποΈ Generate Caption")
|
92 |
-
caption_output = gr.Textbox(
|
93 |
-
label="π Generated Caption:",
|
94 |
-
lines=2
|
95 |
-
)
|
96 |
-
|
97 |
-
# Image-to-Image Section
|
98 |
-
with gr.Row(visible=False) as image_to_image_tab:
|
99 |
-
with gr.Column():
|
100 |
-
gr.Markdown("## ποΈ Image-to-Image Transformer")
|
101 |
-
init_image_input = gr.Image(
|
102 |
-
label="πΈ Upload an image:",
|
103 |
-
type="pil"
|
104 |
-
)
|
105 |
-
transformation_prompt = gr.Textbox(
|
106 |
-
label="π Enter transformation prompt:",
|
107 |
-
placeholder="e.g., Make it look like a Van Gogh painting",
|
108 |
-
lines=2
|
109 |
-
)
|
110 |
-
transform_btn = gr.Button("π Transform Image")
|
111 |
-
with gr.Row():
|
112 |
-
transformed_image = gr.Image(label="ποΈ Transformed Image")
|
113 |
-
download_transformed_btn = gr.Button("π₯ Download Image")
|
114 |
-
|
115 |
-
# Define Visibility Based on Task Selection
|
116 |
-
def toggle_visibility(selected_task):
|
117 |
-
return {
|
118 |
-
text_to_image_tab: selected_task == "πΌοΈ Text-to-Image",
|
119 |
-
image_to_text_tab: selected_task == "π Image-to-Text",
|
120 |
-
image_to_image_tab: selected_task == "ποΈ Image-to-Image",
|
121 |
-
}
|
122 |
-
|
123 |
-
task.change(
|
124 |
-
fn=toggle_visibility,
|
125 |
-
inputs=task,
|
126 |
-
outputs=[text_to_image_tab, image_to_text_tab, image_to_image_tab]
|
127 |
-
)
|
128 |
-
|
129 |
-
# Define Button Actions
|
130 |
-
generate_btn.click(
|
131 |
-
fn=async_text_to_image,
|
132 |
-
inputs=prompt_input,
|
133 |
-
outputs=output_image
|
134 |
-
)
|
135 |
-
|
136 |
-
download_btn.click(
|
137 |
-
fn=lambda img: img.save("generated_image.png") or "Image downloaded!",
|
138 |
-
inputs=output_image,
|
139 |
-
outputs=None
|
140 |
-
)
|
141 |
-
|
142 |
-
generate_caption_btn.click(
|
143 |
-
fn=async_image_to_text,
|
144 |
-
inputs=image_input,
|
145 |
-
outputs=caption_output
|
146 |
-
)
|
147 |
-
|
148 |
-
transform_btn.click(
|
149 |
-
fn=async_image_to_image,
|
150 |
-
inputs=[init_image_input, transformation_prompt],
|
151 |
-
outputs=transformed_image
|
152 |
-
)
|
153 |
-
|
154 |
-
download_transformed_btn.click(
|
155 |
-
fn=lambda img: img.save("transformed_image.png") or "Image downloaded!",
|
156 |
-
inputs=transformed_image,
|
157 |
-
outputs=None
|
158 |
-
)
|
159 |
-
|
160 |
-
# Footer Section with Quirky Elements
|
161 |
-
gr.Markdown("----")
|
162 |
-
gr.Markdown("### π Explore the endless possibilities with AI! π")
|
163 |
-
gr.Markdown("#### π Built with β€οΈ by [Your Name]")
|
164 |
-
|
165 |
-
# ============================================
|
166 |
-
# Launch the Gradio App
|
167 |
-
# ============================================
|
168 |
-
|
169 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import asyncio
|
4 |
+
|
5 |
+
from text_to_image import TextToImage
|
6 |
+
from image_to_text import ImageToText
|
7 |
+
from image_to_image import ImageToImage
|
8 |
+
|
9 |
+
# ============================================
|
10 |
+
# Initialize Model Classes
|
11 |
+
# ============================================
|
12 |
+
|
13 |
+
text_to_image = TextToImage()
|
14 |
+
image_to_text = ImageToText()
|
15 |
+
image_to_image = ImageToImage()
|
16 |
+
|
17 |
+
# ============================================
|
18 |
+
# Gradio Interface Functions with Async and Error Handling
|
19 |
+
# ============================================
|
20 |
+
|
21 |
+
async def async_text_to_image(prompt):
|
22 |
+
"""
|
23 |
+
Asynchronous interface function for Text-to-Image generation with error handling.
|
24 |
+
"""
|
25 |
+
try:
|
26 |
+
image = await text_to_image.generate_image(prompt)
|
27 |
+
return image
|
28 |
+
except Exception as e:
|
29 |
+
raise gr.Error(f"Text-to-Image Generation Failed: {str(e)}")
|
30 |
+
|
31 |
+
async def async_image_to_text(image):
|
32 |
+
"""
|
33 |
+
Asynchronous interface function for Image-to-Text captioning with error handling.
|
34 |
+
"""
|
35 |
+
try:
|
36 |
+
caption = await image_to_text.generate_caption(image)
|
37 |
+
return caption
|
38 |
+
except Exception as e:
|
39 |
+
raise gr.Error(f"Image-to-Text Captioning Failed: {str(e)}")
|
40 |
+
|
41 |
+
async def async_image_to_image(image, prompt):
|
42 |
+
"""
|
43 |
+
Asynchronous interface function for Image-to-Image transformation with error handling.
|
44 |
+
"""
|
45 |
+
try:
|
46 |
+
transformed_image = await image_to_image.transform_image(image, prompt)
|
47 |
+
return transformed_image
|
48 |
+
except Exception as e:
|
49 |
+
raise gr.Error(f"Image-to-Image Transformation Failed: {str(e)}")
|
50 |
+
|
51 |
+
# ============================================
|
52 |
+
# Gradio UI Design
|
53 |
+
# ============================================
|
54 |
+
|
55 |
+
with gr.Blocks(css=".gradio-container {background-color: #0f0700}") as demo:
|
56 |
+
# Title Section
|
57 |
+
gr.Markdown("# π¨ AI Creativity Hub π")
|
58 |
+
gr.Markdown("### Unleash the power of AI to transform your ideas into reality!")
|
59 |
+
|
60 |
+
# Task Selection Radio
|
61 |
+
with gr.Tab("β¨ Choose Your Magic β¨"):
|
62 |
+
task = gr.Radio(
|
63 |
+
["πΌοΈ Text-to-Image", "π Image-to-Text", "ποΈ Image-to-Image"],
|
64 |
+
label="Select a Task",
|
65 |
+
interactive=True,
|
66 |
+
value="πΌοΈ Text-to-Image"
|
67 |
+
)
|
68 |
+
|
69 |
+
# Text-to-Image Section
|
70 |
+
with gr.Row(visible=False) as text_to_image_tab:
|
71 |
+
with gr.Column():
|
72 |
+
gr.Markdown("## πΌοΈ Text-to-Image Generator")
|
73 |
+
prompt_input = gr.Textbox(
|
74 |
+
label="π Enter your prompt:",
|
75 |
+
placeholder="e.g., A serene sunset over the mountains",
|
76 |
+
lines=2
|
77 |
+
)
|
78 |
+
generate_btn = gr.Button("π¨ Generate Image")
|
79 |
+
with gr.Row():
|
80 |
+
output_image = gr.Image(label="πΌοΈ Generated Image")
|
81 |
+
download_btn = gr.Button("π₯ Download Image")
|
82 |
+
|
83 |
+
# Image-to-Text Section
|
84 |
+
with gr.Row(visible=False) as image_to_text_tab:
|
85 |
+
with gr.Column():
|
86 |
+
gr.Markdown("## π Image-to-Text Captioning")
|
87 |
+
image_input = gr.Image(
|
88 |
+
label="πΈ Upload an image:",
|
89 |
+
type="pil"
|
90 |
+
)
|
91 |
+
generate_caption_btn = gr.Button("ποΈ Generate Caption")
|
92 |
+
caption_output = gr.Textbox(
|
93 |
+
label="π Generated Caption:",
|
94 |
+
lines=2
|
95 |
+
)
|
96 |
+
|
97 |
+
# Image-to-Image Section
|
98 |
+
with gr.Row(visible=False) as image_to_image_tab:
|
99 |
+
with gr.Column():
|
100 |
+
gr.Markdown("## ποΈ Image-to-Image Transformer")
|
101 |
+
init_image_input = gr.Image(
|
102 |
+
label="πΈ Upload an image:",
|
103 |
+
type="pil"
|
104 |
+
)
|
105 |
+
transformation_prompt = gr.Textbox(
|
106 |
+
label="π Enter transformation prompt:",
|
107 |
+
placeholder="e.g., Make it look like a Van Gogh painting",
|
108 |
+
lines=2
|
109 |
+
)
|
110 |
+
transform_btn = gr.Button("π Transform Image")
|
111 |
+
with gr.Row():
|
112 |
+
transformed_image = gr.Image(label="ποΈ Transformed Image")
|
113 |
+
download_transformed_btn = gr.Button("π₯ Download Image")
|
114 |
+
|
115 |
+
# Define Visibility Based on Task Selection
|
116 |
+
def toggle_visibility(selected_task):
|
117 |
+
return {
|
118 |
+
text_to_image_tab: selected_task == "πΌοΈ Text-to-Image",
|
119 |
+
image_to_text_tab: selected_task == "π Image-to-Text",
|
120 |
+
image_to_image_tab: selected_task == "ποΈ Image-to-Image",
|
121 |
+
}
|
122 |
+
|
123 |
+
task.change(
|
124 |
+
fn=toggle_visibility,
|
125 |
+
inputs=task,
|
126 |
+
outputs=[text_to_image_tab, image_to_text_tab, image_to_image_tab]
|
127 |
+
)
|
128 |
+
|
129 |
+
# Define Button Actions
|
130 |
+
generate_btn.click(
|
131 |
+
fn=async_text_to_image,
|
132 |
+
inputs=prompt_input,
|
133 |
+
outputs=output_image
|
134 |
+
)
|
135 |
+
|
136 |
+
download_btn.click(
|
137 |
+
fn=lambda img: img.save("generated_image.png") or "Image downloaded!",
|
138 |
+
inputs=output_image,
|
139 |
+
outputs=None
|
140 |
+
)
|
141 |
+
|
142 |
+
generate_caption_btn.click(
|
143 |
+
fn=async_image_to_text,
|
144 |
+
inputs=image_input,
|
145 |
+
outputs=caption_output
|
146 |
+
)
|
147 |
+
|
148 |
+
transform_btn.click(
|
149 |
+
fn=async_image_to_image,
|
150 |
+
inputs=[init_image_input, transformation_prompt],
|
151 |
+
outputs=transformed_image
|
152 |
+
)
|
153 |
+
|
154 |
+
download_transformed_btn.click(
|
155 |
+
fn=lambda img: img.save("transformed_image.png") or "Image downloaded!",
|
156 |
+
inputs=transformed_image,
|
157 |
+
outputs=None
|
158 |
+
)
|
159 |
+
|
160 |
+
# Footer Section with Quirky Elements
|
161 |
+
gr.Markdown("----")
|
162 |
+
gr.Markdown("### π Explore the endless possibilities with AI! π")
|
163 |
+
gr.Markdown("#### π Built with β€οΈ by [Your Name]")
|
164 |
+
|
165 |
+
# ============================================
|
166 |
+
# Launch the Gradio App
|
167 |
+
# ============================================
|
168 |
+
|
169 |
+
demo.launch()
|