Skip to content

Commit

Permalink
taking Ls
Browse files Browse the repository at this point in the history
  • Loading branch information
EAction13 committed Dec 6, 2023
1 parent 0019b39 commit d652384
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/cse/gradle/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ void setListeners(NewRecipePane recipePane, View appScenes, Scene cancelScene) {

recipePane.getGenerateRecipeButton().setOnAction(e -> {
Recipe newRecipe = null;
String imageURLString = null;
try {
model.performFileWriteRequest("mealType.wav");
model.performFileWriteRequest("ingredients.wav");
newRecipe = model.performRecipeGenerateRequest();
imageURLString = model.performImageGenerateRequest(newRecipe.getName());
newRecipe.setImgURL(imageURLString);
} catch (Exception exception) {
exception.printStackTrace();
}
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/cse/gradle/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.io.Files;

import cse.gradle.Server.APIs.ChatGPTApiClient;
import cse.gradle.Server.APIs.DallEApiClient;
import cse.gradle.Server.APIs.WhisperApiClient;

import java.net.URI;
Expand Down Expand Up @@ -272,6 +273,8 @@ public static String[] performRecipeGenerationRequest(String mealType, String in
return response;
}



public String performFileWriteRequest(String audioFile) throws MalformedURLException, IOException {
final String PUT_URL = "http://localhost:8100/generate?audioFile=" + audioFile;
final File uploadFile = new File(audioFile);
Expand Down Expand Up @@ -346,4 +349,41 @@ public Recipe performRecipeGenerateRequest() throws URISyntaxException, IOExcept

return Recipe.parseRecipeFromString(generatedRecipe);
}

public static String performImageGenerateRequest(String prompt){
try {
String urlString = "http://localhost:8100/";
URL url = new URI(urlString).toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);

//String jsonUser = "{\"username\": \"" + username + "\", \"password\": \"" + password + "\"}";
String jsonImageName = "{\"dallEPrompt\": \"" + prompt + "\"}";

try (OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream())) {
out.write(jsonImageName);
}

try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
// Set the Model's userId to the one returned by the server if the registration
// was successful
//if (!response.toString().contains("Error")) {
// this.userId = response.toString();
//}
return response.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
if (ex.getMessage().contains("Connection refused")) {
return "Error: Server down";
}
return "Error: " + ex.getMessage();
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/cse/gradle/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class Recipe {
private Date date;
private UUID id;

private String imgURLString;

// empty constructor
public Recipe() {
this.ingredients = "";
Expand Down Expand Up @@ -86,6 +88,8 @@ public Recipe(String ingredients,
this.id = id;
}



// getters
public String getIngredients() {
return ingredients;
Expand All @@ -111,6 +115,10 @@ public UUID getId() {
return id;
}

public String getImgUrl(){
return imgURLString;
}

// setters
public void setIngredients(String newIngredients) {
ingredients = newIngredients;
Expand All @@ -135,6 +143,10 @@ public void setDate(Date newDate){
public void setId(UUID newId) {
id = newId;
}

public void setImgURL(String imgURL){
this.imgURLString = imgURL;
}

// toString method for saving to file (csv)
public String toString() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/cse/gradle/Server/APIs/DallEApi.java
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 app/src/main/java/cse/gradle/Server/APIs/DallEApiClient.java
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 app/src/main/java/cse/gradle/Server/GenerateImageHandler.java
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;
}
}
1 change: 1 addition & 0 deletions app/src/main/java/cse/gradle/Server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static void startServer() throws IOException {
server.createContext("/register", new RegisterHandler(usersDb));
server.createContext("/share", new ShareHandler(usersDb));
server.createContext("/generate", new GenerateRecipeHandler(usersDb));
server.createContext("/generateImage", new GenerateImageHandler());

// Set the server's executor object to be threadPoolExecutor
server.setExecutor(threadPoolExecutor);
Expand Down
Binary file not shown.
Binary file added app/src/main/java/cse/gradle/Server/mealType.wav
Binary file not shown.
Loading

0 comments on commit d652384

Please sign in to comment.