Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee My腾云code

Happy_EveryDay

可以平凡 不可以平庸 无爱则无忧,无欲则无求,无怒而无敌,无怨才是佛。所有烦恼,都是放不下的执著 开源技群 328035181 MyGitee

sb-ai-alibaba ImageModel AuidoModel20250715

参考sb-ai-alibaba 20250715

 一、ImageModel  

1、

package com.ds.aialibabaother.controller;

import com.alibaba.cloud.ai.dashscope.api.DashScopeImageApi;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

@RestController
public class alibabaImageModelController
{

@Autowired
private DashScopeImageModel imageModel;

@GetMapping("/image")
public void image(@RequestParam(value = "msg",
defaultValue = "生成一只小猫") String msg, HttpServletResponse response) {
ImageResponse imageResponse = imageModel.call(
new ImagePrompt(
msg,
DashScopeImageOptions.builder()
.withModel(DashScopeImageApi.DEFAULT_IMAGE_MODEL)
.withN(1)
.withHeight(1024)
.withWidth(1024)
.build()
)
);
//获取生成图像地址
String imageUrl =imageResponse.getResult().getOutput().getUrl();
//在浏览器中打开图片
try {
URL url= URI.create(imageUrl).toURL();
InputStream inputStream = url.openStream();
response.setHeader("Content-Type", MediaType.IMAGE_PNG_VALUE);
response.getOutputStream().write(inputStream.readAllBytes());
response.getOutputStream().flush();
}catch (Exception e){
e.printStackTrace();
}
}
}


2、

 

 

 

 二、AuidoModel

1、
package com.ds.aialibabaother.controller;

import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisModel;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisOptions;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisPrompt;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

@RestController
public class alibabaAuidoModelController {

@Autowired
private DashScopeSpeechSynthesisModel speechSynthesisModel;

private static final String TEXT = "床前明月光,疑是地上霜。举头望明月,低头思故乡";
private static final String PATH = "D:\\Users\\Administrator\\IdeaProjects\\DS-AI\\ai-alibaba-other\\src\\main\\resources\\tts";
@GetMapping("/tts")
public void tts() {
//创建
DashScopeSpeechSynthesisOptions options = DashScopeSpeechSynthesisOptions.builder()
.withSpeed(1.0)
.withPitch(0.9)
.withVolume(60)
.build();
// 调用模型
SpeechSynthesisResponse response = speechSynthesisModel.call(
new SpeechSynthesisPrompt(TEXT, options)
);
File file = new File(PATH+"/output.mp3");
try (FileOutputStream fos = new FileOutputStream(file)){
ByteBuffer byteBuffer = response.getResult().getOutput().getAudio();
fos.write(byteBuffer.array());
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
}
}
}


2、

 3、

 



posted on 2025-07-15 15:33  cn2025  阅读(38)  评论(0)    收藏  举报

导航