bakyt92 commited on
Commit
18839b3
Β·
1 Parent(s): 807b907

Initial commit

Browse files
Files changed (2) hide show
  1. app.py +216 -0
  2. requirements.txt +35 -0
app.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from PIL import Image
4
+ import io
5
+ import openai
6
+ from gradio_image_annotation import image_annotator
7
+
8
+ # Image Generation Functions
9
+ def generate_ideogram_image(api_key, prompt, aspect_ratio="ASPECT_1_1"):
10
+ """Generate image using Ideogram API"""
11
+ if not api_key or not prompt:
12
+ return None, "Please provide both API key and prompt"
13
+
14
+ url = "https://api.ideogram.ai/generate"
15
+ headers = {
16
+ "Api-Key": api_key,
17
+ "Content-Type": "application/json"
18
+ }
19
+
20
+ payload = {
21
+ "image_request": {
22
+ "prompt": prompt,
23
+ "aspect_ratio": aspect_ratio,
24
+ "model": "V_2",
25
+ "magic_prompt_option": "AUTO"
26
+ }
27
+ }
28
+
29
+ try:
30
+ response = requests.post(url, json=payload, headers=headers)
31
+ if response.status_code == 200:
32
+ result = response.json()
33
+ if result.get("data"):
34
+ image_url = result["data"][0]["url"]
35
+ # Download and return the image
36
+ img_response = requests.get(image_url)
37
+ image = Image.open(io.BytesIO(img_response.content))
38
+ return image, "Image generated successfully!"
39
+ else:
40
+ return None, "No image data received"
41
+ else:
42
+ return None, f"Error: {response.status_code} - {response.text}"
43
+ except Exception as e:
44
+ return None, f"Error generating image: {str(e)}"
45
+
46
+ def generate_dalle_image(api_key, prompt, size="1024x1024"):
47
+ """Generate image using DALL-E 3 API"""
48
+ if not api_key or not prompt:
49
+ return None, "Please provide both API key and prompt"
50
+
51
+ try:
52
+ client = openai.OpenAI(api_key=api_key)
53
+ response = client.images.generate(
54
+ model="dall-e-3",
55
+ prompt=prompt,
56
+ size=size,
57
+ quality="standard",
58
+ n=1,
59
+ )
60
+
61
+ # Download the image from URL
62
+ image_url = response.data[0].url
63
+ img_response = requests.get(image_url)
64
+ image = Image.open(io.BytesIO(img_response.content))
65
+ return image, "Image generated successfully with DALL-E 3!"
66
+
67
+ except Exception as e:
68
+ return None, f"Error generating image: {str(e)}"
69
+
70
+ def process_uploaded_image(image):
71
+ """Process uploaded image"""
72
+ if image is None:
73
+ return None, "No image uploaded"
74
+ return image, "Image uploaded successfully!"
75
+
76
+ # Image Annotation Functions
77
+ def get_annotations_json(annotations):
78
+ """Extract annotations as JSON"""
79
+ return annotations.get("boxes", [])
80
+
81
+ def save_annotated_image(annotations):
82
+ """Return the annotated image"""
83
+ if annotations and annotations.get("image") is not None:
84
+ return annotations["image"]
85
+ return None
86
+
87
+ # Create the Gradio Interface
88
+ with gr.Blocks(title="AI Image Generator & Annotator MCP Server") as demo:
89
+ gr.Markdown("# 🎨 AI Image Generator & Annotator")
90
+ gr.Markdown("Generate images with AI or upload your own, then add annotations!")
91
+
92
+ # Tab 1: Image Generation
93
+ with gr.Tab("πŸ–ΌοΈ Image Generation", id="generation_tab"):
94
+ gr.Markdown("## Generate or Upload Images")
95
+
96
+ with gr.Row():
97
+ with gr.Column():
98
+ # API Key inputs
99
+ ideogram_key = gr.Textbox(
100
+ label="Ideogram API Key",
101
+ type="password",
102
+ placeholder="Enter your Ideogram API key"
103
+ )
104
+ dalle_key = gr.Textbox(
105
+ label="OpenAI API Key",
106
+ type="password",
107
+ placeholder="Enter your OpenAI API key"
108
+ )
109
+
110
+ # Prompt input
111
+ prompt = gr.Textbox(
112
+ label="Image Prompt",
113
+ placeholder="Describe the image you want to generate...",
114
+ lines=3
115
+ )
116
+
117
+ # Generation options
118
+ with gr.Row():
119
+ ideogram_aspect = gr.Dropdown(
120
+ choices=["ASPECT_1_1", "ASPECT_16_9", "ASPECT_9_16", "ASPECT_4_3", "ASPECT_3_4"],
121
+ value="ASPECT_1_1",
122
+ label="Ideogram Aspect Ratio"
123
+ )
124
+ dalle_size = gr.Dropdown(
125
+ choices=["1024x1024", "1792x1024", "1024x1792"],
126
+ value="1024x1024",
127
+ label="DALL-E Size"
128
+ )
129
+
130
+ # Generation buttons
131
+ with gr.Row():
132
+ ideogram_btn = gr.Button("🎨 Generate with Ideogram", variant="primary")
133
+ dalle_btn = gr.Button("πŸ€– Generate with DALL-E 3", variant="primary")
134
+
135
+ # Upload option
136
+ gr.Markdown("### Or Upload Your Own Image")
137
+ upload_image = gr.Image(label="Upload Image", type="pil")
138
+ upload_btn = gr.Button("πŸ“ Process Uploaded Image")
139
+
140
+ with gr.Column():
141
+ # Output
142
+ generated_image = gr.Image(label="Generated/Uploaded Image", type="pil")
143
+ status_message = gr.Textbox(label="Status", interactive=False)
144
+
145
+ # Event handlers for Tab 1
146
+ ideogram_btn.click(
147
+ generate_ideogram_image,
148
+ inputs=[ideogram_key, prompt, ideogram_aspect],
149
+ outputs=[generated_image, status_message]
150
+ )
151
+
152
+ dalle_btn.click(
153
+ generate_dalle_image,
154
+ inputs=[dalle_key, prompt, dalle_size],
155
+ outputs=[generated_image, status_message]
156
+ )
157
+
158
+ upload_btn.click(
159
+ process_uploaded_image,
160
+ inputs=[upload_image],
161
+ outputs=[generated_image, status_message]
162
+ )
163
+
164
+ # Tab 2: Image Annotation
165
+ with gr.Tab("πŸ“ Image Annotation", id="annotation_tab"):
166
+ gr.Markdown("## Add Signs and Annotations to Images")
167
+ gr.Markdown("Upload an image or use one from the generation tab, then add bounding boxes and labels.")
168
+
169
+ with gr.Row():
170
+ with gr.Column():
171
+ # Image annotation component
172
+ annotator = image_annotator(
173
+ label="Image Annotator",
174
+ label_list=["Person", "Object", "Vehicle", "Animal", "Building", "Sign", "Text", "Custom"],
175
+ label_colors=[
176
+ (255, 0, 0), # Red for Person
177
+ (0, 255, 0), # Green for Object
178
+ (0, 0, 255), # Blue for Vehicle
179
+ (255, 255, 0), # Yellow for Animal
180
+ (255, 0, 255), # Magenta for Building
181
+ (0, 255, 255), # Cyan for Sign
182
+ (128, 0, 128), # Purple for Text
183
+ (255, 165, 0) # Orange for Custom
184
+ ],
185
+ image_type="pil"
186
+ )
187
+
188
+ with gr.Row():
189
+ get_annotations_btn = gr.Button("πŸ“Š Get Annotations", variant="secondary")
190
+ save_image_btn = gr.Button("πŸ’Ύ Save Annotated Image", variant="primary")
191
+
192
+ with gr.Column():
193
+ # Outputs
194
+ annotated_output = gr.Image(label="Annotated Image")
195
+ annotations_json = gr.JSON(label="Annotations Data")
196
+
197
+ # Event handlers for Tab 2
198
+ get_annotations_btn.click(
199
+ get_annotations_json,
200
+ inputs=[annotator],
201
+ outputs=[annotations_json]
202
+ )
203
+
204
+ save_image_btn.click(
205
+ save_annotated_image,
206
+ inputs=[annotator],
207
+ outputs=[annotated_output]
208
+ )
209
+
210
+ # Launch as MCP Server
211
+ if __name__ == "__main__":
212
+ demo.launch(
213
+ share=True, # Create public link for testing
214
+ server_name="0.0.0.0",
215
+ server_port=7860
216
+ )
requirements.txt ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Core web interface and MCP server
2
+ gradio>=4.0.0
3
+ gradio_client>=0.8.0
4
+
5
+ # Image annotation component
6
+ gradio_image_annotation>=0.3.0
7
+
8
+ # HTTP requests for API calls
9
+ requests>=2.31.0
10
+
11
+ # Image processing
12
+ Pillow>=10.0.0
13
+
14
+ # OpenAI API for DALL-E
15
+ openai>=1.10.0
16
+
17
+ # Additional dependencies for image handling
18
+ numpy>=1.24.0
19
+ opencv-python>=4.8.0
20
+
21
+ # For MCP server functionality
22
+ fastapi>=0.104.0
23
+ uvicorn>=0.24.0
24
+
25
+ # For handling environment variables (optional but recommended)
26
+ python-dotenv>=1.0.0
27
+
28
+ # For enhanced error handling and logging
29
+ loguru>=0.7.0
30
+
31
+ # For data validation (useful for API responses)
32
+ pydantic>=2.5.0
33
+
34
+ # For async operations (if needed for future features)
35
+ aiohttp>=3.9.0