-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
268 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package cse.gradle.Server.APIs; | ||
|
||
public interface DallEApi { | ||
|
||
public String generateChatResponse(String prompt) throws Exception; | ||
} |
77 changes: 75 additions & 2 deletions
77
app/src/main/java/cse/gradle/Server/APIs/DallEApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,80 @@ | ||
package cse.gradle.Server.APIs; | ||
|
||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.file.Paths; | ||
import java.nio.file.Files; | ||
|
||
|
||
import org.json.JSONObject; | ||
|
||
public class DallEApiClient implements DallEApi { | ||
public String generateChatResponse(String[] messages) throws Exception { | ||
return ""; | ||
private static final String API_ENDPOINT = "https://api.openai.com/v1/images/generations"; | ||
private static final String API_KEY = | ||
"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"; | ||
private static final String MODEL = "dall-e-2"; | ||
|
||
public String generateChatResponse(String prompt) throws Exception { | ||
//return ""; | ||
// Set request parameters | ||
//String prompt = "Sackboy from Little Big Planet"; | ||
int n = 1; | ||
|
||
|
||
// Create a request body which you will pass into request object | ||
JSONObject requestBody = new JSONObject(); | ||
requestBody.put("model", MODEL); | ||
requestBody.put("prompt", prompt); | ||
requestBody.put("n", n); | ||
requestBody.put("size", "256x256"); | ||
|
||
|
||
// Create the HTTP client | ||
HttpClient client = HttpClient.newHttpClient(); | ||
|
||
|
||
// Create the request object | ||
HttpRequest request = HttpRequest | ||
.newBuilder() | ||
.uri(URI.create(API_ENDPOINT)) | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", String.format("Bearer %s", API_KEY)) | ||
.POST(HttpRequest.BodyPublishers.ofString(requestBody.toString())) | ||
.build(); | ||
|
||
|
||
// Send the request and receive the response | ||
HttpResponse<String> response = client.send( | ||
request, | ||
HttpResponse.BodyHandlers.ofString() | ||
); | ||
|
||
|
||
// Process the response | ||
String responseBody = response.body(); | ||
|
||
|
||
JSONObject responseJson = new JSONObject(responseBody); | ||
|
||
// TODO: Process the response | ||
|
||
String generatedImageURL = responseJson.getJSONArray("data").getJSONObject(0).getString("url"); | ||
|
||
System.out.println("DALL-E Response:"); | ||
System.out.println(generatedImageURL); | ||
|
||
|
||
// Download the Generated Image to Current Directory | ||
try( | ||
InputStream in = new URI(generatedImageURL).toURL().openStream() | ||
) | ||
{ | ||
Files.copy(in, Paths.get("image.jpg")); | ||
} | ||
|
||
return generatedImageURL; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
app/src/main/java/cse/gradle/Server/GenerateImageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package cse.gradle.Server; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.URISyntaxException; | ||
import java.util.Scanner; | ||
|
||
import org.bson.Document; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
|
||
import cse.gradle.Recipe; | ||
import cse.gradle.Server.APIs.ChatGPTApiClient; | ||
import cse.gradle.Server.APIs.DallEApiClient; | ||
import cse.gradle.Server.APIs.WhisperApiClient; | ||
|
||
public class GenerateImageHandler implements HttpHandler{ | ||
|
||
public GenerateImageHandler(){ | ||
|
||
} | ||
|
||
// receive request from Model and execute handlePut or handlePost accordingly | ||
@Override | ||
public void handle(HttpExchange httpExchange) throws IOException { | ||
String response = "Request Received"; | ||
String method = httpExchange.getRequestMethod(); | ||
try { | ||
if (method.equals("POST")) { | ||
response = handlePost(httpExchange); | ||
} else { | ||
throw new Exception("Unsupported HTTP method: " + method); | ||
} | ||
|
||
// Set the response character encoding to UTF-8 | ||
httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=UTF-8"); | ||
|
||
// Convert the response string to bytes with UTF-8 encoding | ||
byte[] responseBytes = response.getBytes("UTF-8"); | ||
|
||
// Send the response | ||
httpExchange.sendResponseHeaders(200, responseBytes.length); | ||
try (OutputStream os = httpExchange.getResponseBody()) { | ||
os.write(responseBytes); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
httpExchange.sendResponseHeaders(500, response.length()); | ||
try (OutputStream os = httpExchange.getResponseBody()) { | ||
os.write(response.getBytes()); | ||
} | ||
} | ||
} | ||
|
||
private String handlePost(HttpExchange httpExchange) throws IOException, URISyntaxException { | ||
// get mealType and ingredients from whisper | ||
//String mealTypeFilePath = "src/main/java/cse/gradle/Server/mealType.wav"; | ||
//String ingredientsFilePath = "src/main/java/cse/gradle/Server/ingredients.wav"; | ||
|
||
DallEApiClient dallEApi = new DallEApiClient(); | ||
String response = ""; | ||
|
||
try { | ||
|
||
InputStream inStream = httpExchange.getRequestBody(); | ||
Scanner scanner = new Scanner(inStream); | ||
StringBuilder getData = new StringBuilder(); | ||
|
||
while (scanner.hasNextLine()) { | ||
getData.append(scanner.nextLine()); | ||
} | ||
|
||
scanner.close(); | ||
inStream.close(); | ||
|
||
System.out.println("Received JSON data: " + getData); | ||
|
||
// Parse the JSON request body into a JsonNode tree | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode root = mapper.readTree(getData.toString()); | ||
|
||
// Get the username and password from the request body | ||
String recipeName = root.get("dallEPrompt").asText(); | ||
//String password = root.get("password").asText(); | ||
|
||
try { | ||
response = dallEApi.generateChatResponse(recipeName); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
/* | ||
// Check if username password pair is in the database | ||
Document user = usersDB.findOne("username", username); | ||
if (user != null) { | ||
// If the user exists, check if the password is correct | ||
if (user.get("password").equals(password)) { | ||
// If the password is correct, return the user's id | ||
return user.get("userId").toString(); | ||
} else { | ||
// If the password is incorrect, return an error message | ||
response += "Incorrect password for user " + username; | ||
throw new Exception(response); | ||
} | ||
} else { | ||
// If the user does not exist, return an error message | ||
response += "User " + username + " does not exist"; | ||
throw new Exception(response); | ||
} | ||
*/ | ||
} catch (Exception e) { | ||
// If an exception is thrown, return an error message | ||
e.printStackTrace(); | ||
} | ||
|
||
// JSONify recipe | ||
//Recipe newRecipe = new Recipe(response[2], response[3], response[1], response[0]); | ||
//String jsonRecipe = newRecipe.toDocument().toJson(); | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Oops, something went wrong.