Skip to content

Commit

Permalink
Merge pull request #104 from ucsd-cse110-fa23/image-preview
Browse files Browse the repository at this point in the history
Image preview
  • Loading branch information
dt0602 authored Dec 4, 2023
2 parents d747824 + 4513f2e commit c2ab573
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/main/java/client/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

//import com.google.common.io.Files;

import client.model.Model;
import client.view.AccountPopup;
import client.view.AppFrame;
import client.view.DetailsPopup;
import client.view.LoginPopup;
import client.view.RecipePopup;
import java.nio.file.Files;
import client.view.AccountPopup;
import java.nio.file.Paths;
import java.io.InputStream;
import java.net.URI;

public class Controller {
private AppFrame appFrame;
Expand Down Expand Up @@ -75,6 +82,8 @@ private void handleStopRecordingButton(ActionEvent event) {
recipePopup.getRecipe().getName().setText(instructions[0]);
recipePopup.getRecipe().getIngredient().setText(instructions[1]);
recipePopup.getRecipe().getInstruction().setText(instructions[2]);
String url = Model.generateImage(recipePopup.getRecipe().getName().getText());
recipePopup.getRecipe().getImageURL().setText(url);
recipePopup.mealTypeSet = false;

if (recipePopup.getRecipe().isComplete()) {
Expand Down Expand Up @@ -105,6 +114,8 @@ private void handleRefreshButton(ActionEvent event) {
detailsPopup.getRecipe().getName().setText(instructions[0]);
detailsPopup.getRecipe().getIngredient().setText(instructions[1]);
detailsPopup.getRecipe().getInstruction().setText(instructions[2]);
String url = Model.generateImage(instructions[0]);
detailsPopup.getRecipe().getImageURL().setText(url);
detailsPopup.setRecipe(detailsPopup.getRecipe());
} catch (IOException | InterruptedException | URISyntaxException e1) {
e1.printStackTrace();
Expand Down
Empty file.
41 changes: 41 additions & 0 deletions src/main/java/client/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,47 @@ public static String sendAudio(String url, String filePath) throws IOException,
return response.body();
}

public static String generateImage(String prompt) throws IOException, InterruptedException {
String API_ENDPOINT = "https://api.openai.com/v1/images/generations";
String API_KEY = "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";
String MODEL = "dall-e-2";
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");
return generatedImageURL;

}

// private static String sendAudio(String urlString, String filePath) throws
// IOException{
// final String POST_URL = urlString;
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/client/view/DetailsPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Path;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.*;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Paths;
import java.net.URISyntaxException;
import java.net.URL;

//import com.google.common.io.Files;
import client.model.Model;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

Expand All @@ -19,12 +35,15 @@ public class DetailsPopup extends Stage {
private TextField ingredients;
private TextArea instruction;

private ImageView recipeImage;

private Button editButton;
private Button deleteButton;
private Button saveButton;
private Button backButton;
private Button refreshButton;


public DetailsPopup() {
// setTitle(name.getText());
setWidth(525);
Expand Down Expand Up @@ -63,11 +82,16 @@ public DetailsPopup() {
refreshButton = new Button("Refresh");
refreshButton.setStyle(defaultButtonStyle);

Image defaultImage = new Image("https://oaidalleapiprodscus.blob.core.windows.net/private/org-Sd9bwBmEf5IDns4KIh3k3fXp/user-DgtoVZso5Yqo57hZ4b0PreAg/img-J5bN9XvbTsOKfNxM4jZ5nZeA.png?st=2023-12-04T00%3A19%3A56Z&se=2023-12-04T02%3A19%3A56Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-12-03T22%3A44%3A30Z&ske=2023-12-04T22%3A44%3A30Z&sks=b&skv=2021-08-06&sig=xfmsjhcQXD23/A8KzCCNsB/Q0tJ5h3IknRwQNI7hnLo%3D");
recipeImage = new ImageView(defaultImage);
recipeImage.setFitWidth(200);
recipeImage.setFitHeight(200);

// Create a layout for the controls
VBox layout = new VBox(10); // 10 pixels spacing
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: #93c994;");
layout.getChildren().addAll(name, ingredients, instruction, backButton, editButton, deleteButton, saveButton,
layout.getChildren().addAll(recipeImage, name, ingredients, instruction, backButton, editButton, deleteButton, saveButton,
refreshButton);

// Add an action for the "Add" button
Expand Down Expand Up @@ -104,11 +128,14 @@ public Recipe getRecipe() {
return this.recipe;
}

public void setRecipe(Recipe recipe) {
public void setRecipe(Recipe recipe) throws IOException, InterruptedException, URISyntaxException{
this.recipe = recipe;
name.setText(recipe.getName().getText());
ingredients.setText(recipe.getIngredient().getText());
instruction.setText(recipe.getInstruction().getText());
String url = recipe.getImageURL().getText();
Image image = new Image(url);
recipeImage.setImage(image);
}

public Button getEditButton() {
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/client/view/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javafx.scene.layout.*;
import javafx.geometry.Insets;
import java.util.Map;
import java.io.IOException;
import java.net.URISyntaxException;

//import RecipeManagement.RecipePopup.DetailsPopup;

Expand All @@ -23,9 +25,10 @@ public class Recipe extends HBox {
private TextField mealType;
private TextField ingredient;
private TextField instruction;
private TextField imageURL;
private Button detailButton;
public Map<String, String[]> recipe = new HashMap<>();
String[] recipeDetails = new String[3];
String[] recipeDetails = new String[4];

public Recipe(AppFrame appframe) {

Expand Down Expand Up @@ -71,10 +74,12 @@ public Recipe(AppFrame appframe) {
this.mealType = new TextField();
this.ingredient = new TextField();
this.instruction = new TextField();
this.imageURL = new TextField();

recipeDetails[0] = mealType.toString();
recipeDetails[1] = ingredient.toString();
recipeDetails[2] = instruction.toString();
recipeDetails[3] = imageURL.toString();
recipe.put(name.toString(), recipeDetails);

this.getChildren().addAll(recipeInfo);
Expand All @@ -85,7 +90,7 @@ public Recipe(AppFrame appframe) {
public void saveRecipetoDB() {
server.SendRecipeDB.sendRecipeDB(server.Login.getID(), this.name.getText(),
this.ingredient.getText(),
this.instruction.getText(), this.mealType.getText());
this.instruction.getText(), this.mealType.getText(), this.imageURL.getText());
}

public TextField getName() {
Expand All @@ -104,13 +109,25 @@ public TextField getInstruction() {
return this.instruction;
}

public TextField getImageURL() {
return this.imageURL;
}

public Button getDetailButton() {
return this.detailButton;
}

public void addListeners() {
detailButton.setOnAction(e -> {
detailsPopup.setRecipe(this);
try {
detailsPopup.setRecipe(this);
} catch (IOException | InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
detailsPopup.show();
});
}
Expand All @@ -119,7 +136,7 @@ public void addListeners() {
// mealtype -> ingredients -> name -> instructions)
public Boolean isComplete() {
return !this.name.getText().isEmpty() && !this.mealType.getText().isEmpty()
&& !this.ingredient.getText().isEmpty() && !this.instruction.getText().isEmpty();
&& !this.ingredient.getText().isEmpty() && !this.instruction.getText().isEmpty() && !this.imageURL.getText().isEmpty();
}

public void deleteRecipe() {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/client/view/RecipeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public void saveRecipes() {
String ingredients = Recipe.getIngredient().getText();
String instruction = Recipe.getInstruction().getText();
String mealType = Recipe.getMealType().getText();
fw.write(name + "-" + ingredients + "-" + instruction + "-" + mealType + "\n");
String url = Recipe.getImageURL().getText();
fw.write(name + "-" + ingredients + "-" + instruction + "-" + mealType + "-" + url + "\n");
}
fw.close();
} catch (Exception e) {
Expand Down Expand Up @@ -132,11 +133,13 @@ public void loadTasks() {
String recipeIngredients = recipeDoc.getString("recipeIngredients");
String recipeInstructions = recipeDoc.getString("recipeInstructions");
String mealType = recipeDoc.getString("mealType");
String url = recipeDoc.getString("url");

recipe.getName().setText(recipeName);
recipe.getIngredient().setText(recipeIngredients);
recipe.getInstruction().setText(recipeInstructions);
recipe.getMealType().setText(mealType);
recipe.getImageURL().setText(url);
recipeContainer.add(recipe);
this.getChildren().add(recipe);
}
Expand Down
Binary file modified src/main/java/server/Create.class
Binary file not shown.
Binary file modified src/main/java/server/CreateAccount.class
Binary file not shown.
Binary file modified src/main/java/server/LoadRecipes.class
Binary file not shown.
Binary file modified src/main/java/server/Login.class
Binary file not shown.
Binary file modified src/main/java/server/MongoDB.class
Binary file not shown.
Binary file modified src/main/java/server/SendRecipeDB.class
Binary file not shown.
5 changes: 3 additions & 2 deletions src/main/java/server/SendRecipeDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class SendRecipeDB {

public static void sendRecipeDB(String id, String recipeName, String recipeIngredients, String recipeInstructions,
String mealType) {
String mealType, String url) {
String uri = "mongodb+srv://aditijain:[email protected]/?retryWrites=true&w=majority";
try (MongoClient mongoClient = MongoClients.create(uri)) {

Expand All @@ -22,7 +22,8 @@ public static void sendRecipeDB(String id, String recipeName, String recipeIngre
Document recipe = new Document("_id", new ObjectId())
.append("userID", server.Login.getID()).append("recipeName", recipeName)
.append("recipeIngredients", recipeIngredients).append("recipeInstructions", recipeInstructions)
.append("mealType", mealType);
.append("mealType", mealType)
.append("url", url);
accountsCollection.insertOne(recipe);

System.out.println("Recipe Stored for user:" + id);
Expand Down
12 changes: 7 additions & 5 deletions src/test/java/RecipeListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ public void testSaveRecipes() {
recipe1.getMealType().setText("breakfast");
recipe1.getIngredient().setText("ingredient");
recipe1.getInstruction().setText("instruction");
recipe1.getImageURL().setText("https...");
recipe1.addRecipe();

Recipe recipe2 = new Recipe(mockAppFrame);
recipe2.getName().setText("Recipe 2");
recipe2.getMealType().setText("breakfast");
recipe2.getIngredient().setText("ingredient");
recipe2.getInstruction().setText("instruction");
recipe2.getMealType().setText("breakfast2");
recipe2.getIngredient().setText("ingredient2");
recipe2.getInstruction().setText("instruction2");
recipe2.getImageURL().setText("https123");
recipe2.addRecipe();

try {
Expand All @@ -110,10 +112,10 @@ public void testSaveRecipes() {

List<String> lines = Files.readAllLines(Path.of("recipes.csv"));

String[] parts1 = lines.get(0).split("-");
String[] parts1 = lines.get(0).split("-", 4);
assertEquals("Recipe 1", parts1[0]);

String[] parts2 = lines.get(1).split("-");
String[] parts2 = lines.get(1).split("-", 4);
assertEquals("Recipe 2", parts2[0]);
} catch (IOException e) {
fail("Error occurred: " + e.getMessage());
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/RecipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void testRecipeInitialization() {
assertNotNull(recipe.getIngredient());
assertNotNull(recipe.getInstruction());
assertNotNull(recipe.getDetailButton());
assertNotNull(recipe.getImageURL());
}

@Test
Expand All @@ -44,6 +45,7 @@ public void testRecipeCompletion() {
recipe.getMealType().setText("Dinner");
recipe.getIngredient().setText("Chicken, vegetables");
recipe.getInstruction().setText("Stir-fry the ingredients.");
recipe.getImageURL().setText("https...");

assertTrue(recipe.isComplete());
}
Expand Down

0 comments on commit c2ab573

Please sign in to comment.