Commit
·
7b49e90
1
Parent(s):
195f21b
mcp_server change
Browse files- mcp_server.py +202 -13
- requirements.txt +2 -1
mcp_server.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
from src.utils.change_format import change_format
|
3 |
from src.utils.remove_background import remove_background_from_url
|
4 |
from src.utils.visualize_image import visualize_base64_image
|
@@ -8,19 +8,208 @@ from src.utils.add_text import add_text_to_image
|
|
8 |
from src.utils.watermark import add_watermark, remove_watermark
|
9 |
from src.utils.describe import describe_image
|
10 |
from src.utils.compress import compress_image
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
if __name__ == "__main__":
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from src.utils.change_format import change_format
|
3 |
from src.utils.remove_background import remove_background_from_url
|
4 |
from src.utils.visualize_image import visualize_base64_image
|
|
|
8 |
from src.utils.watermark import add_watermark, remove_watermark
|
9 |
from src.utils.describe import describe_image
|
10 |
from src.utils.compress import compress_image
|
11 |
+
import base64
|
12 |
+
from PIL import Image
|
13 |
+
import io
|
14 |
+
import requests
|
15 |
|
16 |
+
def image_to_base64(image):
|
17 |
+
if image is None:
|
18 |
+
return None
|
19 |
+
buffer = io.BytesIO()
|
20 |
+
image.save(buffer, format="PNG")
|
21 |
+
return base64.b64encode(buffer.getvalue()).decode()
|
22 |
|
23 |
+
def base64_to_image(base64_str):
|
24 |
+
if not base64_str:
|
25 |
+
return None
|
26 |
+
image_data = base64.b64decode(base64_str)
|
27 |
+
return Image.open(io.BytesIO(image_data))
|
28 |
+
|
29 |
+
def url_to_base64(url):
|
30 |
+
response = requests.get(url)
|
31 |
+
return base64.b64encode(response.content).decode()
|
32 |
+
|
33 |
+
def gradio_remove_background(image):
|
34 |
+
if image is None:
|
35 |
+
return None
|
36 |
+
base64_img = image_to_base64(image)
|
37 |
+
result = remove_background_from_url(f"data:image/png;base64,{base64_img}")
|
38 |
+
return base64_to_image(result)
|
39 |
+
|
40 |
+
def gradio_describe_image(image):
|
41 |
+
if image is None:
|
42 |
+
return "No image provided"
|
43 |
+
base64_img = image_to_base64(image)
|
44 |
+
return describe_image(base64_img)
|
45 |
+
|
46 |
+
def gradio_change_format(image, format_type):
|
47 |
+
if image is None:
|
48 |
+
return None
|
49 |
+
base64_img = image_to_base64(image)
|
50 |
+
result = change_format(base64_img, format_type)
|
51 |
+
return base64_to_image(result)
|
52 |
+
|
53 |
+
def gradio_generate_image(prompt, width=512, height=512):
|
54 |
+
result = generate_image(prompt, width, height)
|
55 |
+
return base64_to_image(result)
|
56 |
+
|
57 |
+
def gradio_apply_filter(image, filter_type):
|
58 |
+
if image is None:
|
59 |
+
return None
|
60 |
+
base64_img = image_to_base64(image)
|
61 |
+
result = apply_filter(base64_img, filter_type)
|
62 |
+
return base64_to_image(result)
|
63 |
+
|
64 |
+
def gradio_add_text(image, text, x=50, y=50, font_size=20, color="white"):
|
65 |
+
if image is None:
|
66 |
+
return None
|
67 |
+
base64_img = image_to_base64(image)
|
68 |
+
result = add_text_to_image(base64_img, text, x, y, font_size, color)
|
69 |
+
return base64_to_image(result)
|
70 |
+
|
71 |
+
def gradio_add_watermark(image, watermark_text, opacity=0.5):
|
72 |
+
if image is None:
|
73 |
+
return None
|
74 |
+
base64_img = image_to_base64(image)
|
75 |
+
result = add_watermark(base64_img, watermark_text, opacity)
|
76 |
+
return base64_to_image(result)
|
77 |
+
|
78 |
+
def gradio_remove_watermark(image):
|
79 |
+
if image is None:
|
80 |
+
return None
|
81 |
+
base64_img = image_to_base64(image)
|
82 |
+
result = remove_watermark(base64_img)
|
83 |
+
return base64_to_image(result)
|
84 |
+
|
85 |
+
def gradio_compress_image(image, quality=80):
|
86 |
+
if image is None:
|
87 |
+
return None
|
88 |
+
base64_img = image_to_base64(image)
|
89 |
+
result = compress_image(base64_img, quality)
|
90 |
+
return base64_to_image(result)
|
91 |
+
|
92 |
+
def create_gradio_interface():
|
93 |
+
with gr.Blocks(title="Image Processing Service", theme=gr.themes.Soft()) as demo:
|
94 |
+
gr.Markdown("# 🖼️ Image Processing Service")
|
95 |
+
gr.Markdown("Servicio completo de procesamiento de imágenes")
|
96 |
+
|
97 |
+
with gr.Tabs():
|
98 |
+
with gr.Tab("🎨 Generate Image"):
|
99 |
+
with gr.Row():
|
100 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate")
|
101 |
+
with gr.Column():
|
102 |
+
width_input = gr.Slider(256, 1024, 512, label="Width")
|
103 |
+
height_input = gr.Slider(256, 1024, 512, label="Height")
|
104 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
105 |
+
generated_output = gr.Image(label="Generated Image")
|
106 |
+
|
107 |
+
generate_btn.click(
|
108 |
+
gradio_generate_image,
|
109 |
+
[prompt_input, width_input, height_input],
|
110 |
+
generated_output
|
111 |
+
)
|
112 |
+
|
113 |
+
with gr.Tab("🔍 Describe Image"):
|
114 |
+
with gr.Row():
|
115 |
+
describe_input = gr.Image(label="Upload Image", type="pil")
|
116 |
+
description_output = gr.Textbox(label="Description", lines=4)
|
117 |
+
|
118 |
+
describe_input.change(gradio_describe_image, describe_input, description_output)
|
119 |
+
|
120 |
+
with gr.Tab("✂️ Remove Background"):
|
121 |
+
with gr.Row():
|
122 |
+
bg_input = gr.Image(label="Upload Image", type="pil")
|
123 |
+
bg_output = gr.Image(label="Background Removed")
|
124 |
+
|
125 |
+
bg_input.change(gradio_remove_background, bg_input, bg_output)
|
126 |
+
|
127 |
+
with gr.Tab("🎭 Apply Filters"):
|
128 |
+
with gr.Row():
|
129 |
+
filter_input = gr.Image(label="Upload Image", type="pil")
|
130 |
+
with gr.Column():
|
131 |
+
filter_type = gr.Dropdown(
|
132 |
+
["blur", "sharpen", "vintage", "black_white", "sepia"],
|
133 |
+
label="Filter Type",
|
134 |
+
value="blur"
|
135 |
+
)
|
136 |
+
filter_output = gr.Image(label="Filtered Image")
|
137 |
+
|
138 |
+
filter_input.change(gradio_apply_filter, [filter_input, filter_type], filter_output)
|
139 |
+
filter_type.change(gradio_apply_filter, [filter_input, filter_type], filter_output)
|
140 |
+
|
141 |
+
with gr.Tab("📝 Add Text"):
|
142 |
+
with gr.Row():
|
143 |
+
text_input = gr.Image(label="Upload Image", type="pil")
|
144 |
+
with gr.Column():
|
145 |
+
text_content = gr.Textbox(label="Text", placeholder="Enter text to add")
|
146 |
+
with gr.Row():
|
147 |
+
text_x = gr.Number(label="X Position", value=50)
|
148 |
+
text_y = gr.Number(label="Y Position", value=50)
|
149 |
+
with gr.Row():
|
150 |
+
font_size = gr.Slider(10, 100, 20, label="Font Size")
|
151 |
+
text_color = gr.ColorPicker(label="Color", value="#FFFFFF")
|
152 |
+
text_output = gr.Image(label="Image with Text")
|
153 |
+
|
154 |
+
inputs = [text_input, text_content, text_x, text_y, font_size, text_color]
|
155 |
+
for inp in inputs:
|
156 |
+
inp.change(gradio_add_text, inputs, text_output)
|
157 |
+
|
158 |
+
with gr.Tab("💧 Watermark"):
|
159 |
+
with gr.Tabs():
|
160 |
+
with gr.Tab("Add Watermark"):
|
161 |
+
with gr.Row():
|
162 |
+
watermark_input = gr.Image(label="Upload Image", type="pil")
|
163 |
+
with gr.Column():
|
164 |
+
watermark_text = gr.Textbox(label="Watermark Text")
|
165 |
+
watermark_opacity = gr.Slider(0.1, 1.0, 0.5, label="Opacity")
|
166 |
+
watermark_output = gr.Image(label="Watermarked Image")
|
167 |
+
|
168 |
+
inputs = [watermark_input, watermark_text, watermark_opacity]
|
169 |
+
for inp in inputs:
|
170 |
+
inp.change(gradio_add_watermark, inputs, watermark_output)
|
171 |
+
|
172 |
+
with gr.Tab("Remove Watermark"):
|
173 |
+
with gr.Row():
|
174 |
+
unwatermark_input = gr.Image(label="Upload Image", type="pil")
|
175 |
+
unwatermark_output = gr.Image(label="Watermark Removed")
|
176 |
+
|
177 |
+
unwatermark_input.change(gradio_remove_watermark, unwatermark_input, unwatermark_output)
|
178 |
+
|
179 |
+
with gr.Tab("🗜️ Compress"):
|
180 |
+
with gr.Row():
|
181 |
+
compress_input = gr.Image(label="Upload Image", type="pil")
|
182 |
+
with gr.Column():
|
183 |
+
quality_slider = gr.Slider(10, 100, 80, label="Quality %")
|
184 |
+
compress_output = gr.Image(label="Compressed Image")
|
185 |
+
|
186 |
+
compress_input.change(gradio_compress_image, [compress_input, quality_slider], compress_output)
|
187 |
+
quality_slider.change(gradio_compress_image, [compress_input, quality_slider], compress_output)
|
188 |
+
|
189 |
+
with gr.Tab("🔄 Change Format"):
|
190 |
+
with gr.Row():
|
191 |
+
format_input = gr.Image(label="Upload Image", type="pil")
|
192 |
+
with gr.Column():
|
193 |
+
format_type = gr.Dropdown(
|
194 |
+
["PNG", "JPEG", "WEBP", "BMP"],
|
195 |
+
label="Output Format",
|
196 |
+
value="PNG"
|
197 |
+
)
|
198 |
+
format_output = gr.Image(label="Converted Image")
|
199 |
+
|
200 |
+
format_input.change(gradio_change_format, [format_input, format_type], format_output)
|
201 |
+
format_type.change(gradio_change_format, [format_input, format_type], format_output)
|
202 |
+
|
203 |
+
gr.Markdown("---")
|
204 |
+
gr.Markdown("💡 **Status**: Active | Procesamiento de imágenes en tiempo real")
|
205 |
+
|
206 |
+
return demo
|
207 |
|
208 |
if __name__ == "__main__":
|
209 |
+
demo = create_gradio_interface()
|
210 |
+
demo.launch(
|
211 |
+
server_name="0.0.0.0",
|
212 |
+
server_port=7860,
|
213 |
+
share=True,
|
214 |
+
show_error=True
|
215 |
+
)
|
requirements.txt
CHANGED
@@ -4,4 +4,5 @@ Pillow
|
|
4 |
rembg
|
5 |
onnxruntime
|
6 |
openai
|
7 |
-
opencv-python
|
|
|
|
4 |
rembg
|
5 |
onnxruntime
|
6 |
openai
|
7 |
+
opencv-python
|
8 |
+
langchain_openai
|