本地电脑使用Docker部署FunASR服务之后,怎么使用Springboot集成本地部署的FunASR服务,并进行语音识别?希望给出详细的代码。
4条回答 默认 最新
檀越@新空间 2025-07-23 15:45关注下午好🌅🌅🌅
本答案参考通义千问在本地使用 Docker 部署 FunASR 服务后,你可以通过 Spring Boot 调用该服务进行语音识别。以下是详细的步骤和代码示例,帮助你完成整个集成过程。
1. 确保 FunASR 服务已正确部署
首先,你需要确保 FunASR 服务已经成功运行在本地 Docker 容器中,并且可以通过网络访问。
1.1 启动 FunASR 容器(示例)
docker run -d -p 8080:8080 funasr/inference-server注意: 这里假设 FunASR 的默认端口是
8080,实际请根据你的 Docker 镜像配置确认。1.2 检查容器是否运行正常
docker ps你应该看到类似以下输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abc123456789 funasr/inference-server "/start.sh" 5 minutes ago Up 5 minutes 0.0.0.0:8080->8080/tcp my-funasr
2. 在 Spring Boot 中调用 FunASR 服务
Spring Boot 可以通过 HTTP 请求调用 FunASR 提供的 API 接口,比如
/api/asr或/v1/asr(具体路径取决于 FunASR 的接口设计)。2.1 添加依赖(Maven)
在
pom.xml中添加以下依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
3. 编写 Spring Boot 控制器调用 FunASR
3.1 创建一个 REST 控制器
import org.springframework.http.*; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/api") public class AsrController { private final RestTemplate restTemplate; public AsrController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @PostMapping("/recognize") public ResponseEntity<String> recognize(@RequestParam("file") String file) { // 假设上传的是音频文件,这里可以替换为实际的文件处理逻辑 // 示例中直接构造请求体 Map<String, Object> requestBody = new HashMap<>(); requestBody.put("audio", file); // 根据 FunASR 的接口要求调整参数名 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers); String url = "http://localhost:8080/api/asr"; // 替换为你的 FunASR 服务地址 ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class); return ResponseEntity.status(response.getStatusCode()).body(response.getBody()); } }
4. 配置 RestTemplate Bean
在 Spring Boot 应用中,需要注入
RestTemplate实例。4.1 配置类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class AppConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
5. 测试接口
启动 Spring Boot 应用后,使用 Postman 或 curl 发送 POST 请求测试接口:
curl -X POST http://localhost:8080/api/recognize \ -H "Content-Type: application/json" \ -d '{"audio": "your_audio_file_base64"}'注意: 你需要根据 FunASR 的接口文档确定正确的请求格式(如 Base64、文件路径等)。
6. 可能遇到的问题及解决方案
| 问题 | 解决方案 | |------|----------| | 无法连接到 FunASR 服务 | 检查 Docker 容器是否运行,确认端口映射正确,使用
curl http://localhost:8080测试服务可用性 | | 接口参数不匹配 | 查阅 FunASR 的官方文档,确认请求参数格式(如 JSON 结构) | | 跨域问题 | 如果前端与 Spring Boot 不同源,需配置 CORS |
7. 可选:使用 FeignClient 进行服务调用
如果你希望使用声明式 REST 客户端,可以使用 FeignClient。
7.1 添加 Feign 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>7.2 创建 Feign Client 接口
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestPart; @FeignClient(name = "funasr-service", url = "http://localhost:8080") public interface FunASRClient { @PostMapping("/api/asr") String recognize(@RequestBody Map<String, Object> request); }7.3 在控制器中使用 FeignClient
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Map; @RestController @RequestMapping("/api") public class AsrController { @Autowired private FunASRClient funASRClient; @PostMapping("/recognize") public ResponseEntity<String> recognize(@RequestBody Map<String, Object> request) { String result = funASRClient.recognize(request); return ResponseEntity.ok(result); } }
总结
关键步骤:
- 使用 Docker 部署 FunASR 服务;
- 在 Spring Boot 中通过
RestTemplate或FeignClient调用 FunASR 的 API; - 根据 FunASR 的接口文档构建请求参数;
- 处理可能的网络、参数或跨域问题。
如有更多关于 FunASR 接口的具体信息,欢迎提供,我可以进一步优化代码。
解决 无用评论 打赏 举报