Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -83,33 +83,41 @@ def flux_krea_generate(
|
|
83 |
seed=seed if seed != -1 else random.randint(1, 1000000000)
|
84 |
)
|
85 |
|
86 |
-
# Save the image
|
87 |
-
# This creates a local file that Gradio can serve with a public URL
|
88 |
if image:
|
89 |
-
#
|
90 |
-
unique_id = str(uuid.uuid4())[:8]
|
91 |
-
temp_filename = f"flux_krea_{generation_id}_{unique_id}.png"
|
92 |
-
|
93 |
-
# Save to temp directory
|
94 |
temp_file = tempfile.NamedTemporaryFile(
|
95 |
delete=False,
|
96 |
suffix=".png",
|
97 |
-
prefix=f"
|
98 |
)
|
99 |
image.save(temp_file.name)
|
100 |
temp_file.close()
|
101 |
|
102 |
-
#
|
103 |
-
#
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
print(f'\033[1mMCP Generation {generation_id} completed with {provider}!\033[0m')
|
|
|
107 |
|
108 |
-
# Return JSON with
|
109 |
result = {
|
110 |
"success": True,
|
111 |
-
"image_url":
|
112 |
-
"
|
|
|
113 |
"generation_id": generation_id,
|
114 |
"provider": provider,
|
115 |
"model": "black-forest-labs/FLUX.1-Krea-dev",
|
@@ -125,7 +133,8 @@ def flux_krea_generate(
|
|
125 |
"metadata": {
|
126 |
"tool": "flux_krea_generate",
|
127 |
"timestamp": str(generation_id),
|
128 |
-
"mcp_compatible": True
|
|
|
129 |
}
|
130 |
}
|
131 |
|
@@ -152,7 +161,14 @@ def flux_krea_generate(
|
|
152 |
"success": False,
|
153 |
"error": error_message,
|
154 |
"image_url": None,
|
155 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
})
|
157 |
|
158 |
# For UI compatibility - this function returns a PIL Image for the Gradio interface
|
@@ -161,9 +177,11 @@ def flux_krea_generate_ui(*args) -> Optional[Image.Image]:
|
|
161 |
result_json = flux_krea_generate(*args)
|
162 |
try:
|
163 |
result = json.loads(result_json)
|
164 |
-
if result.get("success") and result.get("
|
165 |
-
|
166 |
-
|
|
|
|
|
167 |
pass
|
168 |
return None
|
169 |
|
@@ -298,7 +316,9 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css, title="FLUX.1-Krea MCP Server
|
|
298 |
<h3>📡 MCP Server Information</h3>
|
299 |
<p><strong>Server Endpoint:</strong> <code>/gradio_api/mcp/sse</code></p>
|
300 |
<p><strong>Tool Name:</strong> <code>flux_krea_generate</code></p>
|
301 |
-
<p>
|
|
|
|
|
302 |
</div>
|
303 |
""")
|
304 |
|
|
|
83 |
seed=seed if seed != -1 else random.randint(1, 1000000000)
|
84 |
)
|
85 |
|
86 |
+
# Save the image and return a Gradio-accessible URL
|
|
|
87 |
if image:
|
88 |
+
# Save to a temporary file that Gradio can serve
|
|
|
|
|
|
|
|
|
89 |
temp_file = tempfile.NamedTemporaryFile(
|
90 |
delete=False,
|
91 |
suffix=".png",
|
92 |
+
prefix=f"flux_krea_mcp_{generation_id}_"
|
93 |
)
|
94 |
image.save(temp_file.name)
|
95 |
temp_file.close()
|
96 |
|
97 |
+
# Create the Gradio file URL that will be accessible to MCP clients
|
98 |
+
# This matches the format you saw: /gradio_api/file=<file_path>
|
99 |
+
gradio_file_url = f"/gradio_api/file={temp_file.name}"
|
100 |
+
|
101 |
+
# For hosted spaces, we also need the full URL
|
102 |
+
# Try to detect if we're running on Spaces
|
103 |
+
space_name = os.getenv("SPACE_ID")
|
104 |
+
if space_name:
|
105 |
+
# Running on HF Spaces
|
106 |
+
full_url = f"https://{space_name}.hf.space{gradio_file_url}"
|
107 |
+
else:
|
108 |
+
# Running locally
|
109 |
+
server_port = os.getenv("GRADIO_SERVER_PORT", "7860")
|
110 |
+
full_url = f"http://localhost:{server_port}{gradio_file_url}"
|
111 |
|
112 |
print(f'\033[1mMCP Generation {generation_id} completed with {provider}!\033[0m')
|
113 |
+
print(f'🌐 Image accessible at: {full_url}')
|
114 |
|
115 |
+
# Return JSON with accessible URLs and metadata
|
116 |
result = {
|
117 |
"success": True,
|
118 |
+
"image_url": full_url,
|
119 |
+
"gradio_file_url": gradio_file_url,
|
120 |
+
"local_path": temp_file.name,
|
121 |
"generation_id": generation_id,
|
122 |
"provider": provider,
|
123 |
"model": "black-forest-labs/FLUX.1-Krea-dev",
|
|
|
133 |
"metadata": {
|
134 |
"tool": "flux_krea_generate",
|
135 |
"timestamp": str(generation_id),
|
136 |
+
"mcp_compatible": True,
|
137 |
+
"accessible_url": full_url
|
138 |
}
|
139 |
}
|
140 |
|
|
|
161 |
"success": False,
|
162 |
"error": error_message,
|
163 |
"image_url": None,
|
164 |
+
"gradio_file_url": None,
|
165 |
+
"local_path": None,
|
166 |
+
"generation_id": generation_id,
|
167 |
+
"metadata": {
|
168 |
+
"tool": "flux_krea_generate",
|
169 |
+
"mcp_compatible": True,
|
170 |
+
"error": True
|
171 |
+
}
|
172 |
})
|
173 |
|
174 |
# For UI compatibility - this function returns a PIL Image for the Gradio interface
|
|
|
177 |
result_json = flux_krea_generate(*args)
|
178 |
try:
|
179 |
result = json.loads(result_json)
|
180 |
+
if result.get("success") and result.get("local_path"):
|
181 |
+
# Return the PIL Image for the UI
|
182 |
+
return Image.open(result["local_path"])
|
183 |
+
except Exception as e:
|
184 |
+
print(f"UI wrapper error: {e}")
|
185 |
pass
|
186 |
return None
|
187 |
|
|
|
316 |
<h3>📡 MCP Server Information</h3>
|
317 |
<p><strong>Server Endpoint:</strong> <code>/gradio_api/mcp/sse</code></p>
|
318 |
<p><strong>Tool Name:</strong> <code>flux_krea_generate</code></p>
|
319 |
+
<p><strong>Image URLs:</strong> Returns accessible Gradio file URLs like <code>/gradio_api/file=<path></code></p>
|
320 |
+
<p>This server exposes the image generation function as an MCP tool that returns JSON with accessible image URLs for LLM integration.</p>
|
321 |
+
<p><em>✅ Fixed: LLMs can now access generated images via proper Gradio file URLs</em></p>
|
322 |
</div>
|
323 |
""")
|
324 |
|