From 4e3557b9e9569231ff95ecdbc88f3ac2df597a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=82=A6?= Date: Mon, 19 May 2025 14:21:14 +0800 Subject: [PATCH] webapi /api/speech --- GLMSearch/Program.cs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/GLMSearch/Program.cs b/GLMSearch/Program.cs index a466542..67ced77 100644 --- a/GLMSearch/Program.cs +++ b/GLMSearch/Program.cs @@ -1,3 +1,5 @@ +using GLMSearch.Tool; +using Microsoft.AspNetCore.Mvc; using WebSSE; var builder = WebApplication.CreateBuilder(args); @@ -27,4 +29,42 @@ app.MapControllers(); //app.MapMcpSse(); app.MapMcp(); +app.MapPost("/api/speech", ([FromBody] SpeechRequest request) => +{ + if (string.IsNullOrWhiteSpace(request.Input)) + return Results.BadRequest("Input is required."); + if (string.IsNullOrWhiteSpace(request.Voice)) + request.Voice = "zh-CN-XiaoxiaoNeural"; + if (string.IsNullOrWhiteSpace(request.Instructions)) + request.Instructions = "newscast"; + if (string.IsNullOrWhiteSpace(request.ResponseFormat)) + request.ResponseFormat = "audio-16khz-32kbitrate-mono-mp3"; + try + { + var url = Speech2.SpeechGenerate( + request.Input, + request.Voice, + request.Instructions, + request.ResponseFormat + ); + if (!string.IsNullOrEmpty(url)) + { + return Results.Ok(new { success = true, url, markdown = $"[µã»÷²¥·ÅÓïÒô]({url})" }); + } + return Results.StatusCode(500); + } + catch (Exception ex) + { + return Results.StatusCode(500); + } +}); + app.Run(); + +public class SpeechRequest +{ + public string Input { get; set; } + public string Voice { get; set; } + public string Instructions { get; set; } + public string ResponseFormat { get; set; } +}