joyson commited on
Commit
56fca0b
Β·
verified Β·
1 Parent(s): d002b82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -169
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: #f0f8ff}") 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()
 
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()