XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

feat: Add synthesize endpoint for text-to-speech in MCPServer

a0e20efa
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +43 -0
Modified g4f/mcp/server.py +43 -0
@@ -340,12 +340,55 @@ class MCPServer:
340 340 # Serve the file
341 341 return web.FileResponse(target, headers=headers)
342 342
343 async def handle_synthesize(request: web.Request) -> web.Response:
344 """Handle synthesize requests for text-to-speech"""
345 provider_name = request.match_info.get('provider', '')
346 if not provider_name:
347 return web.Response(status=400, text="Provider not specified")
348
349 try:
350 from ..Provider import ProviderUtils
351 provider_handler = ProviderUtils.convert.get(provider_name)
352 if provider_handler is None:
353 return web.Response(status=404, text=f"Provider not found: {provider_name}")
354 except Exception as e:
355 return web.Response(status=404, text=f"Provider not found: {provider_name}")
356
357 if not hasattr(provider_handler, "synthesize"):
358 return web.Response(status=500, text=f"Provider doesn't support synthesize: {provider_name}")
359
360 # Get query parameters
361 params = dict(request.query)
362
363 try:
364 # Call the synthesize method
365 response_data = provider_handler.synthesize(params)
366
367 # Handle async generator
368 async def generate():
369 async for chunk in response_data:
370 yield chunk
371
372 content_type = getattr(provider_handler, "synthesize_content_type", "application/octet-stream")
373 return web.Response(
374 body=b"".join([chunk async for chunk in generate()]),
375 content_type=content_type,
376 headers={
377 "cache-control": "max-age=604800",
378 "access-control-allow-origin": "*",
379 }
380 )
381 except Exception as e:
382 sys.stderr.write(f"Synthesize error: {e}\n")
383 return web.Response(status=500, text=f"Synthesize error: {str(e)}")
384
343 385 # Create aiohttp application
344 386 app = web.Application()
345 387 app.router.add_options('/mcp', lambda request: web.Response(headers={"access-control-allow-origin": "*", "access-control-allow-methods": "POST, OPTIONS", "access-control-allow-headers": "Content-Type"}))
346 388 app.router.add_post('/mcp', handle_mcp_request)
347 389 app.router.add_get('/health', handle_health)
348 390 app.router.add_get('/media/{filename:.*}', handle_media)
391 app.router.add_get('/backend-api/v2/synthesize/{provider}', handle_synthesize)
349 392
350 393 # Start server
351 394 sys.stderr.write(f"Starting {self.server_info['name']} v{self.server_info['version']} (HTTP mode)\n")