Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -229,79 +229,6 @@ def create_gradio_interface():
|
|
| 229 |
|
| 230 |
return demo
|
| 231 |
|
| 232 |
-
# カスタムFastAPIルートを追加
|
| 233 |
-
def setup_mcp_routes(app: FastAPI):
|
| 234 |
-
"""MCPエンドポイントをFastAPIアプリに追加"""
|
| 235 |
-
|
| 236 |
-
@app.post("/api/mcp/list_tools")
|
| 237 |
-
async def mcp_list_tools():
|
| 238 |
-
"""MCPツールのリストを返す"""
|
| 239 |
-
return {
|
| 240 |
-
"tools": [
|
| 241 |
-
{
|
| 242 |
-
"name": "generate_image",
|
| 243 |
-
"description": "Gemini 2.0 Flash Previewを使用して画像を生成します",
|
| 244 |
-
"inputSchema": {
|
| 245 |
-
"type": "object",
|
| 246 |
-
"properties": {
|
| 247 |
-
"prompt": {
|
| 248 |
-
"type": "string",
|
| 249 |
-
"description": "生成したい画像の説明"
|
| 250 |
-
}
|
| 251 |
-
},
|
| 252 |
-
"required": ["prompt"]
|
| 253 |
-
}
|
| 254 |
-
}
|
| 255 |
-
]
|
| 256 |
-
}
|
| 257 |
-
|
| 258 |
-
@app.post("/api/mcp/call_tool")
|
| 259 |
-
async def mcp_call_tool(request: Request):
|
| 260 |
-
"""MCPツールを実行する"""
|
| 261 |
-
try:
|
| 262 |
-
data = await request.json()
|
| 263 |
-
tool_name = data.get("name")
|
| 264 |
-
arguments = data.get("arguments", {})
|
| 265 |
-
|
| 266 |
-
if tool_name == "generate_image":
|
| 267 |
-
prompt = arguments.get("prompt", "")
|
| 268 |
-
|
| 269 |
-
# 画像生成を実行
|
| 270 |
-
image, message = generate_image(prompt)
|
| 271 |
-
|
| 272 |
-
if image:
|
| 273 |
-
# 画像をbase64エンコード
|
| 274 |
-
buffered = io.BytesIO()
|
| 275 |
-
image.save(buffered, format="PNG")
|
| 276 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 277 |
-
|
| 278 |
-
return JSONResponse({
|
| 279 |
-
"success": True,
|
| 280 |
-
"message": message,
|
| 281 |
-
"image_base64": img_str
|
| 282 |
-
})
|
| 283 |
-
else:
|
| 284 |
-
return JSONResponse({
|
| 285 |
-
"success": False,
|
| 286 |
-
"message": message
|
| 287 |
-
})
|
| 288 |
-
|
| 289 |
-
return JSONResponse({
|
| 290 |
-
"success": False,
|
| 291 |
-
"message": f"Unknown tool: {tool_name}"
|
| 292 |
-
}, status_code=400)
|
| 293 |
-
|
| 294 |
-
except Exception as e:
|
| 295 |
-
logger.error(f"MCPエラー: {str(e)}")
|
| 296 |
-
return JSONResponse({
|
| 297 |
-
"success": False,
|
| 298 |
-
"message": f"Error: {str(e)}"
|
| 299 |
-
}, status_code=500)
|
| 300 |
-
|
| 301 |
-
@app.get("/api/health")
|
| 302 |
-
async def health_check():
|
| 303 |
-
"""ヘルスチェックエンドポイント"""
|
| 304 |
-
return {"status": "healthy", "service": "image-gen-mcp"}
|
| 305 |
|
| 306 |
# メイン実行部分
|
| 307 |
if __name__ == "__main__":
|
|
@@ -326,9 +253,79 @@ if __name__ == "__main__":
|
|
| 326 |
logger.info("画像生成MCPサーバーを起動中...")
|
| 327 |
demo = create_gradio_interface()
|
| 328 |
|
| 329 |
-
# Gradio
|
| 330 |
app = demo.app
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
# Hugging Face Spaces用の設定
|
| 334 |
demo.queue()
|
|
|
|
| 229 |
|
| 230 |
return demo
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
# メイン実行部分
|
| 234 |
if __name__ == "__main__":
|
|
|
|
| 253 |
logger.info("画像生成MCPサーバーを起動中...")
|
| 254 |
demo = create_gradio_interface()
|
| 255 |
|
| 256 |
+
# Gradio内部のFastAPIアプリを取得
|
| 257 |
app = demo.app
|
| 258 |
+
|
| 259 |
+
# MCPエンドポイントを直接追加
|
| 260 |
+
@app.post("/api/mcp/list_tools")
|
| 261 |
+
async def mcp_list_tools():
|
| 262 |
+
"""MCPツールのリストを返す"""
|
| 263 |
+
return {
|
| 264 |
+
"tools": [
|
| 265 |
+
{
|
| 266 |
+
"name": "generate_image",
|
| 267 |
+
"description": "Gemini 2.0 Flash Previewを使用して画像を生成します",
|
| 268 |
+
"inputSchema": {
|
| 269 |
+
"type": "object",
|
| 270 |
+
"properties": {
|
| 271 |
+
"prompt": {
|
| 272 |
+
"type": "string",
|
| 273 |
+
"description": "生成したい画像の説明"
|
| 274 |
+
}
|
| 275 |
+
},
|
| 276 |
+
"required": ["prompt"]
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
]
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
@app.post("/api/mcp/call_tool")
|
| 283 |
+
async def mcp_call_tool(request: Request):
|
| 284 |
+
"""MCPツールを実行する"""
|
| 285 |
+
try:
|
| 286 |
+
data = await request.json()
|
| 287 |
+
tool_name = data.get("name")
|
| 288 |
+
arguments = data.get("arguments", {})
|
| 289 |
+
|
| 290 |
+
if tool_name == "generate_image":
|
| 291 |
+
prompt = arguments.get("prompt", "")
|
| 292 |
+
|
| 293 |
+
# 画像生成を実行
|
| 294 |
+
image, message = generate_image(prompt)
|
| 295 |
+
|
| 296 |
+
if image:
|
| 297 |
+
# 画像をbase64エンコード
|
| 298 |
+
buffered = io.BytesIO()
|
| 299 |
+
image.save(buffered, format="PNG")
|
| 300 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 301 |
+
|
| 302 |
+
return JSONResponse({
|
| 303 |
+
"success": True,
|
| 304 |
+
"message": message,
|
| 305 |
+
"image_base64": img_str
|
| 306 |
+
})
|
| 307 |
+
else:
|
| 308 |
+
return JSONResponse({
|
| 309 |
+
"success": False,
|
| 310 |
+
"message": message
|
| 311 |
+
})
|
| 312 |
+
|
| 313 |
+
return JSONResponse({
|
| 314 |
+
"success": False,
|
| 315 |
+
"message": f"Unknown tool: {tool_name}"
|
| 316 |
+
}, status_code=400)
|
| 317 |
+
|
| 318 |
+
except Exception as e:
|
| 319 |
+
logger.error(f"MCPエラー: {str(e)}")
|
| 320 |
+
return JSONResponse({
|
| 321 |
+
"success": False,
|
| 322 |
+
"message": f"Error: {str(e)}"
|
| 323 |
+
}, status_code=500)
|
| 324 |
+
|
| 325 |
+
@app.get("/api/health")
|
| 326 |
+
async def health_check():
|
| 327 |
+
"""ヘルスチェックエンドポイント"""
|
| 328 |
+
return {"status": "healthy", "service": "image-gen-mcp"}
|
| 329 |
|
| 330 |
# Hugging Face Spaces用の設定
|
| 331 |
demo.queue()
|