img
This commit is contained in:
parent
d8e360d7f2
commit
819076c659
@ -30,6 +30,9 @@ public class HtmlGeneratorController {
|
||||
@Autowired
|
||||
private HtmlGenerTorService htmlGenerTorService;
|
||||
|
||||
@Autowired
|
||||
private HtmlBuilder5 htmlBuilder5;
|
||||
|
||||
@Autowired
|
||||
private Design_lbraryService designLibraryService;
|
||||
|
||||
@ -79,7 +82,7 @@ public class HtmlGeneratorController {
|
||||
for (Map.Entry<String, Object> entry : jsonInput.entrySet()) {
|
||||
Map<String, Object> root = (Map<String, Object>) entry.getValue();
|
||||
root.put("element", entry.getKey()); // use key as tag like "section"
|
||||
html.append(HtmlBuilder5.buildHtml(root));
|
||||
html.append(htmlBuilder5.buildHtml(root, jsonInput));
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(new EntityResponse(html.toString()));
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
package com.realnet.OpenAi.Controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.realnet.OpenAi.Services.UnsplashService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/photos")
|
||||
public class PhotoSearchController {
|
||||
|
||||
@Autowired
|
||||
private UnsplashService unsplashService;
|
||||
|
||||
@GetMapping("/search")
|
||||
public List<String> searchPhotos(@RequestParam String keyword) {
|
||||
return unsplashService.getImageUrls(keyword);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package com.realnet.OpenAi.Models;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class UnsplashPhotoResponse {
|
||||
private List<Result> results;
|
||||
|
||||
public List<Result> getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public void setResults(List<Result> results) {
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Result {
|
||||
private Urls urls;
|
||||
|
||||
public Urls getUrls() {
|
||||
return urls;
|
||||
}
|
||||
|
||||
public void setUrls(Urls urls) {
|
||||
this.urls = urls;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Urls {
|
||||
private String regular;
|
||||
|
||||
public String getRegular() {
|
||||
return regular;
|
||||
}
|
||||
|
||||
public void setRegular(String regular) {
|
||||
this.regular = regular;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,23 @@
|
||||
package com.realnet.OpenAi.Services;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.realnet.utils.PortConstant;
|
||||
|
||||
@Service
|
||||
public class HtmlBuilder5 {
|
||||
|
||||
public static String buildHtml(Object nodeObj) {
|
||||
@Autowired
|
||||
private UnsplashService unsplashService;
|
||||
|
||||
public String buildHtml(Object nodeObj, Map<String, Object> jsonInput) {
|
||||
if (!(nodeObj instanceof Map))
|
||||
return "";
|
||||
Map<String, Object> node = (Map<String, Object>) nodeObj;
|
||||
@ -18,7 +30,7 @@ public class HtmlBuilder5 {
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof Map) {
|
||||
((Map<String, Object>) value).put("element", tag);
|
||||
return buildHtml(value);
|
||||
return buildHtml(value, jsonInput);
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +42,7 @@ public class HtmlBuilder5 {
|
||||
// ✅ CUSTOM: Render <img> using keywords and Unsplash image
|
||||
if ("img".equals(tag) && node.containsKey("keywords")) {
|
||||
String keywords = getString(node, "keywords", "");
|
||||
String imageUrl = getImageFromKeyword(keywords);
|
||||
String imageUrl = getImageFromKeyword(keywords, jsonInput);
|
||||
html.append("<img");
|
||||
|
||||
// Set attributes like class, alt, etc.
|
||||
@ -91,7 +103,7 @@ public class HtmlBuilder5 {
|
||||
Object titleObj = node.get("title");
|
||||
if (titleObj instanceof Map) {
|
||||
// Expected: title is an object with element/class/text
|
||||
html.append(buildHtml(titleObj));
|
||||
html.append(buildHtml(titleObj, jsonInput));
|
||||
} else {
|
||||
// Fallback: use default <h3> if element not defined
|
||||
html.append("<h3>").append(titleObj.toString()).append("</h3>");
|
||||
@ -102,7 +114,7 @@ public class HtmlBuilder5 {
|
||||
if (node.containsKey("description")) {
|
||||
Object descObj = node.get("description");
|
||||
if (descObj instanceof Map) {
|
||||
html.append(buildHtml(descObj));
|
||||
html.append(buildHtml(descObj, jsonInput));
|
||||
} else {
|
||||
html.append("<p>").append(descObj.toString()).append("</p>");
|
||||
}
|
||||
@ -113,11 +125,11 @@ public class HtmlBuilder5 {
|
||||
if (children instanceof List) {
|
||||
for (Object child : (List<Object>) children) {
|
||||
if (child instanceof Map) {
|
||||
html.append(buildHtml((Map<String, Object>) child));
|
||||
html.append(buildHtml((Map<String, Object>) child, jsonInput));
|
||||
}
|
||||
}
|
||||
} else if (children instanceof Map) {
|
||||
html.append(buildHtml((Map<String, Object>) children));
|
||||
html.append(buildHtml((Map<String, Object>) children, jsonInput));
|
||||
}
|
||||
|
||||
// Handle content (like in 'section' root)
|
||||
@ -125,11 +137,11 @@ public class HtmlBuilder5 {
|
||||
if (content instanceof List) {
|
||||
for (Object child : (List<Object>) content) {
|
||||
if (child instanceof Map) {
|
||||
html.append(buildHtml((Map<String, Object>) child));
|
||||
html.append(buildHtml((Map<String, Object>) child, jsonInput));
|
||||
}
|
||||
}
|
||||
} else if (content instanceof Map) {
|
||||
html.append(buildHtml((Map<String, Object>) content));
|
||||
html.append(buildHtml((Map<String, Object>) content, jsonInput));
|
||||
}
|
||||
|
||||
// Close tag
|
||||
@ -232,16 +244,45 @@ public class HtmlBuilder5 {
|
||||
return html.toString();
|
||||
}
|
||||
|
||||
private static String getImageFromKeyword(String keywords) {
|
||||
// Convert space-separated keywords to Unsplash format
|
||||
private String getImageFromKeyword(String keywords, Map<String, Object> jsonInput) {
|
||||
String query = keywords.trim().replace(" ", ",");
|
||||
// Return Unsplash proxy image URL (free to use)
|
||||
// "https://images.unsplash.com/photo-1506744038136-46273834b3fb?auto=format&fit=crop&w=800&q=80"
|
||||
|
||||
// return "https://source.unsplash.com/800x400/?" + query;
|
||||
// 1. Get list of image URLs from Unsplash
|
||||
List<String> imageUrls = unsplashService.getImageUrls(keywords);
|
||||
|
||||
return "https://images.unsplash.com/" + query + "?auto=format&fit=crop&w=800&q=80";
|
||||
// 2. Prepare prompt for Gemini
|
||||
StringBuilder promptBuilder = new StringBuilder();
|
||||
promptBuilder.append(
|
||||
"Given the following JSON section and keyword, pick the most relevant image from this list.\n\n");
|
||||
promptBuilder.append("Keyword: ").append(keywords).append("\n");
|
||||
promptBuilder.append("JSON:\n").append(jsonInput.toString()).append("\n\n");
|
||||
promptBuilder.append("Image URLs:\n");
|
||||
|
||||
for (int i = 0; i < imageUrls.size(); i++) {
|
||||
promptBuilder.append((i + 1)).append(". ").append(imageUrls.get(i)).append("\n");
|
||||
}
|
||||
|
||||
promptBuilder.append("\nReturn only the single most relevant image URL.");
|
||||
|
||||
// 3. Create body for Gemini call
|
||||
Map<String, String> geminiBody = new HashMap<>();
|
||||
geminiBody.put("query", promptBuilder.toString());
|
||||
|
||||
// 4. Send POST to Gemini API
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String geminiUrl = PortConstant.OLLAMA_BACK_DOMAIN + "/api/gemini";
|
||||
|
||||
// try {
|
||||
// ResponseEntity<String> response = restTemplate.postForEntity(geminiUrl, geminiBody, String.class);
|
||||
// if (response.getStatusCode().is2xxSuccessful()) {
|
||||
// return response.getBody(); // Assuming Gemini returns plain URL string
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace(); // or log
|
||||
// }
|
||||
|
||||
// 5. Fallback: use first Unsplash image
|
||||
return imageUrls.isEmpty() ? null : imageUrls.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
package com.realnet.OpenAi.Services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.realnet.OpenAi.Models.UnsplashPhotoResponse;
|
||||
|
||||
@Service
|
||||
public class UnsplashService {
|
||||
|
||||
@Value("${unsplash.api.url}")
|
||||
private String apiUrl;
|
||||
|
||||
@Value("${unsplash.access.key}")
|
||||
private String accessKey;
|
||||
|
||||
public List<String> getImageUrls(String keyword) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String uri = UriComponentsBuilder.fromHttpUrl(apiUrl).queryParam("query", keyword)
|
||||
.queryParam("client_id", accessKey).queryParam("per_page", 10).toUriString();
|
||||
|
||||
UnsplashPhotoResponse response = restTemplate.getForObject(uri, UnsplashPhotoResponse.class);
|
||||
|
||||
List<String> imageUrls = new ArrayList<>();
|
||||
if (response != null && response.getResults() != null) {
|
||||
for (UnsplashPhotoResponse.Result r : response.getResults()) {
|
||||
if (r.getUrls() != null && r.getUrls().getRegular() != null) {
|
||||
imageUrls.add(r.getUrls().getRegular());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return imageUrls;
|
||||
}
|
||||
}
|
||||
@ -19,8 +19,6 @@ import org.springframework.stereotype.Service;
|
||||
import com.realnet.OpenAi.Services.HtmlBuilder3;
|
||||
import com.realnet.dlf.Entity.Design_lbrary;
|
||||
import com.realnet.dlf.Repository.Design_lbraryRepository;
|
||||
import com.realnet.realm.Entity.Realm;
|
||||
import com.realnet.realm.Services.RealmService;
|
||||
import com.realnet.users.entity1.AppUser;
|
||||
import com.realnet.users.service1.AppUserServiceImpl;
|
||||
|
||||
@ -33,8 +31,6 @@ public class Design_lbraryService {
|
||||
private Design_lbraryRepository designLibraryRepository;
|
||||
@Autowired
|
||||
private AppUserServiceImpl userService;
|
||||
@Autowired
|
||||
private RealmService realmService;
|
||||
|
||||
public Design_lbrary Savedata(Design_lbrary data) {
|
||||
|
||||
@ -51,7 +47,6 @@ public class Design_lbraryService {
|
||||
}
|
||||
|
||||
public List<Design_lbrary> getdetails() {
|
||||
List<Realm> realm = realmService.findByUserId(getUser().getUserId());
|
||||
List<Design_lbrary> all = designLibraryRepository.findAll(getUser().getUserId());
|
||||
|
||||
return all;
|
||||
|
||||
@ -39,6 +39,7 @@ public class PortConstant {
|
||||
public static String SITE_BUILDER;
|
||||
public static String LOCAL_HOST;
|
||||
public static String SUREOPS_NEW_DOMAIN;
|
||||
public static String OLLAMA_BACK_DOMAIN;
|
||||
|
||||
public static String PROTOCOL;
|
||||
|
||||
@ -84,6 +85,8 @@ public class PortConstant {
|
||||
SITE_BUILDER = getUrlFromApi(backendPortalDomain, "SITE_BUILDER");
|
||||
SUREOPS_NEW_DOMAIN = getUrlFromApi(backendPortalDomain, "SUREOPS_NEW_DOMAIN");
|
||||
|
||||
OLLAMA_BACK_DOMAIN = getUrlFromApi(backendPortalDomain, "OLLAMA_BACK_DOMAIN");
|
||||
|
||||
DOMAIN = getUrlFromApi(backendPortalDomain, "DOMAIN");
|
||||
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user