Add image generation feature with new component and service integration

This commit is contained in:
liosha84
2025-07-03 20:23:21 +03:00
parent afb12a131e
commit f211a0d146
29 changed files with 6038 additions and 259 deletions
@@ -0,0 +1,39 @@
package com.jambotronGroup.jambotron.ZhiPuAi;
import com.jambotronGroup.jambotron.model.NameValueItem;
import org.springframework.ai.image.Image;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.zhipuai.ZhiPuAiImageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600, allowCredentials="true")
@RestController
@RequestMapping("/api/zhipuai")
public class ImageController {
@Autowired
ZhiPuAiImageService _zhiPuAiImageService;
@GetMapping("/image/{query}")
public ResponseEntity<Image> getImage(@PathVariable("query") String query) {
try {
Image returnValue = _zhiPuAiImageService.generateImage(query).getResult().getOutput();
//userRepository.findAll().forEach(users::add);
if (returnValue == null) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(returnValue, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
@@ -1,44 +1,45 @@
package com.jambotronGroup.jambotron.ZhiPuAi;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.zhipuai.ZhiPuAiImageModel;
import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
//import org.springframework.web.reactive.function.client.WebClient;
//
//@Service
//public class ZhiPuAiImageService {
//
// @Value("${spring.ai.zhipuai.base-url}")
// private String baseUrl;
//
// @Value("${spring.ai.zhipuai.api-key}")
// private String apiKey;
//
// private final WebClient webClient;
//
// public ZhiPuAiImageService(WebClient.Builder webClientBuilder) {
// this.webClient = webClientBuilder.baseUrl(baseUrl).build();
// }
//
// public String generateImage(String prompt) {
// return webClient.post()
// .uri("/image/generate")
// .header("Authorization", "Bearer " + apiKey)
// .bodyValue(new ImageRequest(prompt))
// .retrieve()
// .bodyToMono(String.class)
// .block();
// }
//
// private static class ImageRequest {
// private final String prompt;
//
// public ImageRequest(String prompt) {
// this.prompt = prompt;
// }
//
// public String getPrompt() {
// return prompt;
// }
// }
//}
@Service
public class ZhiPuAiImageService {
@Value("${spring.ai.zhipuai.base-url}")
private String baseUrl;
@Value("${spring.ai.zhipuai.api-key}")
private String apiKey;
private ZhiPuAiImageApi _zhiPuAiImageApi;
private ZhiPuAiImageModel _zhiPuAiImageModel;
public ZhiPuAiImageService() {
_zhiPuAiImageApi = new ZhiPuAiImageApi("628447c8c65845a48a7226391464a2ea.Dw8ci6TiW0BRF5LI");
_zhiPuAiImageModel = new ZhiPuAiImageModel(_zhiPuAiImageApi);
}
public ImageResponse generateImage(String prompt) {
// Create an ImagePrompt object with the desired prompt
ImagePrompt imagePrompt = new ImagePrompt(prompt);
// Call the generate method to get the image response
ImageResponse imageResponse = _zhiPuAiImageModel.call(imagePrompt);
// Return the image response
return imageResponse;
}
}
@@ -23,6 +23,8 @@ public class SystemController {
@GetMapping("/data-source-properties")
public ResponseEntity<List<NameValueItem>> getDataSourceProperties(@RequestParam(required = false) String title) {
try {
@@ -1,15 +0,0 @@
package com.jambotronGroup.jambotron.controllers;
public class ZhiPuAiController {
// This class is a placeholder for the ZhiPuAiController.
// You can implement methods to handle requests related to ZhiPuAi functionality.
// For example, you might want to add methods for generating images or processing text.
// Example method (to be implemented):
// @GetMapping("/generate-image")
// public ResponseEntity<String> generateImage(@RequestParam String prompt) {
// String imageUrl = zhiPuAiService.generateImage(prompt);
// return new ResponseEntity<>(imageUrl, HttpStatus.OK);
// }
}
@@ -120,6 +120,8 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
.requestMatchers("/api/test/**").permitAll()
.requestMatchers("/api/tutorials").permitAll()
.requestMatchers("/api/zhipuai/image/**").permitAll()
.requestMatchers("/api/users").permitAll()
.requestMatchers("/api/users/**").permitAll()
@@ -12,4 +12,6 @@ public interface SystemService {
List<NameValueItem> getDataSourceProperties();
List<NameValueItem> getImage();
}
@@ -7,6 +7,10 @@ import com.jambotronGroup.jambotron.model.NameValueItem;
import com.jambotronGroup.jambotron.utils.EntityLoggingConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.zhipuai.ZhiPuAiImageModel;
import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.ApplicationContext;
@@ -29,11 +33,14 @@ public class SystemServiceImpl implements SystemService {
@Autowired
DataSourceProperties _dataSourceProperties;
@Autowired
ZhiPuAiImageModel imageClient;
private static final Logger logger = LoggerFactory.getLogger(SystemServiceImpl.class);
@Override
public List<NameValueItem> getDataSourceProperties(){
List<NameValueItem> returnValue = new ArrayList<NameValueItem>();
@@ -45,6 +52,28 @@ public class SystemServiceImpl implements SystemService {
return returnValue;
}
@Override
public List<NameValueItem> getImage(){
List<NameValueItem> returnValue = new ArrayList<NameValueItem>();
ZhiPuAiImageApi api = new ZhiPuAiImageApi("628447c8c65845a48a7226391464a2ea.Dw8ci6TiW0BRF5LI");
ZhiPuAiImageModel imageClient = new ZhiPuAiImageModel(api);
//imageClient.
//imageClient.getDefaultOptions().setUser("44621746535994937");
ImageResponse imageResponse = imageClient.call(new ImagePrompt("Ukr zaliznicya station in the style cubisme"));
returnValue.add(new NameValueItem("response", "imageResponse."));
return returnValue;
}
@Override
public List<Bean> getLodedBeans() {
// Get all bean names from the application context and map them to Bean objects