Skip to content

Commit

Permalink
refactor: Some refactoring, improvements and updated README.md.
Browse files Browse the repository at this point in the history
Refactored some in `Log.java`. Moved colors to an enum, added colors and added `logColor(msg, color)`.
Fixed wrong message being written when creating a directory in `WriteObjectToFile.java` if it fails.
Added configuration properties and a logging table to `README.md`.
  • Loading branch information
Livila committed Feb 20, 2024
1 parent a7c0eea commit 0880a19
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 46 deletions.
55 changes: 49 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ rm .git/hooks/pre-commit

## Getting started with the large language model (LLM) ##

### Download the language model (follow one of these instructions) ###
Download the bin-file directly from [Hugging Face](https://huggingface.co/):
### Download the language model ###
* Either download the bin-file directly from [Hugging Face](https://huggingface.co/):
[ggml-model-gpt4all-falcon-q4_0.bin](https://huggingface.co/nomic-ai/gpt4all-falcon-ggml/resolve/main/ggml-model-gpt4all-falcon-q4_0.bin?download=true) (4.06 GB)

Choose downloads from the website:
* Or choose downloads from the website:
[https://huggingface.co/nomic-ai/gpt4all-falcon-ggml/tree/main](https://huggingface.co/nomic-ai/gpt4all-falcon-ggml/tree/main)

You may also download any other model of your taste and use that one instead.
* Or you may also download any [other model](https://huggingface.co/models) of your taste and use that one instead.

### Set up the configuration ###
### Setting up the configuration ###

#### Add model and set the configuration to use LLM ####

Expand All @@ -61,4 +61,47 @@ You may also download any other model of your taste and use that one instead.
Run `Main.java` in the project. <!-- TODO: Add run option/script that works "on the go". -->

#### Results ####
The results will be shown in the `/results/` folder,
The results will be shown in the `/results/` folder. By default, the result naming is `result_{nr}` where `{nr}` is represented by a number that increases for each execution.

## Configuration file ##
You can edit some configuration in `config.properties`.

<details>
<summary>
Default configuration (click to expand)
</summary>

###### *config.properties*
```shell
LOGGING_LEVEL=INFO
# This is related to TEMP_FILE below.
TEMP_FILE_ENABLED=false
# If enabled, this file will be deleted and replaced every run instead of using the results files.
TEMP_FILE=debugFile.txt
# Directory of where the result files are saved. They range from 0 to 99.
PATH_TO_RESULTS=results
# Set this property to a path that should be excluded. It's regex based, so writing [file1, file2] would exclude any file containing file1 and file2.
EXCLUDED_PATHS=[]
# Prefix for the result files.
RESULT_NAME_PREFIX=result_{nr}
# The LLM located in project root.
LLM_FILE=models/ggml-model-gpt4all-falcon-q4_0.bin
# Run with LLM - needs a model defined and will take more computing power and time to run.
RUN_WITH_LLM=false
# Directory path to where the code is to be scanned.
PATH_TO_CODE=src/test/resources/
```
</details>

#### LOGGING_LEVEL
The different options are the following:

| | FATAL | ERROR | WARNING | INFO | DEBUG | TRACE |
|----------|:-----:|:-----:|:-------:|:----:|:-----:|:-----:|
| OFF/NONE | | | | | | |
| FATAL | X | | | | | |
| ERROR | X | X | | | | |
| WARNING | X | X | X | | | |
| INFO | X | X | X | X | | |
| DEBUG | X | X | X | X | X | |
| TRACE | X | X | X | X | X | X |
2 changes: 1 addition & 1 deletion config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ RESULT_NAME_PREFIX=result_{nr}
# The LLM located in project root.
LLM_FILE=model/ggml-model-gpt4all-falcon-q4_0.bin
# Run with LLM - needs a model defined and will take more computing power and time to run.
RUN_WITH_LLM=true
RUN_WITH_LLM=false
# Directory path to where the code is to be scanned.
PATH_TO_CODE=src/test/resources/
5 changes: 4 additions & 1 deletion src/main/java/CodeCheck/ConfigInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public interface ConfigInterface {
class Config {
enum LoggingLevel {
NONE(0),
N(NONE.lvl),
OFF(NONE.lvl),
O(OFF.lvl),
ERROR(1),
ERR(ERROR.lvl),
E(ERROR.lvl),
Expand All @@ -28,7 +31,7 @@ enum LoggingLevel {

private final int lvl;

public Boolean logOn(LoggingLevel lvl) {
public boolean logOn(LoggingLevel lvl) {
return lvl.lvl <= this.lvl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/CodeCheck/LLM.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void cleanModel() {
return true;
});

int[] formattedTime = Log.getFormattedDurationTime(startTime);
int[] formattedTime = Util.getFormattedDurationTime(startTime);
Log.log(String.format("This LLM run completed in %02d hours, %02d minutes, %02d seconds and %02d milliseconds.",
formattedTime[0], formattedTime[1], formattedTime[2], formattedTime[3]));
}
Expand Down
66 changes: 35 additions & 31 deletions src/main/java/CodeCheck/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
public interface Log {

// Styling
String ANSI_RESET = "\u001B[0m";
String ANSI_RED = "\u001B[31m";
String ANSI_GREEN = "\u001B[32m";
String ANSI_YELLOW = "\u001B[33m";
String ANSI_BLUE = "\u001B[34m";
String ANSI_PURPLE = "\u001B[35m";

enum Color {
NONE { @Override public String toString() { return ""; } },
RESET { @Override public String toString() { return WHITE.toString(); } },
WHITE { @Override public String toString() { return "\u001B[0m"; } },
RED { @Override public String toString() { return "\u001B[31m"; } },
GREEN { @Override public String toString() { return "\u001B[32m"; } },
YELLOW { @Override public String toString() { return "\u001B[033m"; } },
BLUE { @Override public String toString() { return "\u001B[34m"; } },
PURPLE { @Override public String toString() { return "\u001B[35m"; } },
BLACK { @Override public String toString() { return "\u001B[36m"; } },
CYAN { @Override public String toString() { return "\u001B[37m"; } };
}

static void log(String msg) {
log(msg, ConfigInterface.Config.LoggingLevel.INFO);
Expand All @@ -33,10 +38,14 @@ static void trace(String msg) {
log(msg, ConfigInterface.Config.LoggingLevel.TRACE);
}

private static void log(String msg, ConfigInterface.Config.LoggingLevel lvl) {
static void log(String msg, ConfigInterface.Config.LoggingLevel lvl) {
ConfigInterface.Config.LoggingLevel logLvl = ConfigInterface.conf.getLogLvl();
if (logLvl.logOn(lvl)) {
logReduced("[%s] ".formatted(LocalDateTime.now()) + "[" + lvl.name() + "] " + msg + System.lineSeparator(), lvl);
logReduced("%s [%s] %s".formatted(
LocalDateTime.now(),
lvl.name(),
msg + System.lineSeparator()),
lvl);
}
}

Expand All @@ -49,17 +58,17 @@ static void log(String msg, ConfigInterface.Config.LoggingLevel level, Object...
}

static void logReduced(String msg, ConfigInterface.Config.LoggingLevel level, boolean newLine) {
String colorLevel = switch (level) {
case NONE -> "";
case INFO, I -> ANSI_RESET;
case DEBUG, D -> ANSI_GREEN;
case TRACE, T -> ANSI_BLUE;
case WARNING, WARN, W -> ANSI_YELLOW;
case ERROR, ERR, E -> ANSI_RED;
Color colorLevel = switch (level) {
case NONE, N, OFF, O -> Color.NONE; // No change in color.
case ERROR, ERR, E -> Color.RED;
case WARNING, WARN, W -> Color.YELLOW;
case INFO, I -> Color.WHITE;
case DEBUG, D -> Color.GREEN;
case TRACE, T -> Color.BLUE;
};

if (newLine) System.out.println(colorLevel + msg + ANSI_RESET);
else System.out.print(colorLevel + msg + ANSI_RESET);
if (newLine) System.out.println(colorLevel + msg + Color.RESET);
else System.out.print(colorLevel + msg + Color.RESET);
}

static void logReduced(String msg, ConfigInterface.Config.LoggingLevel level) {
Expand All @@ -74,18 +83,13 @@ static void logReduced(String msg, Object... formatting) {
logReduced(String.format(msg, formatting), ConfigInterface.Config.LoggingLevel.INFO, false);
}

/**
* Calculate the duration from the start time and the current time.
*
* @param startTime Input the start time.
* @return An integer array of time the duration in HH:MM:SS:MS. 0 = h, 1 = m, 2 = s, 3 = ms
*/
static int[] getFormattedDurationTime(long startTime) {
long millis = (System.currentTimeMillis() - startTime);
int seconds = (int) millis / 1000;
int minutes = (seconds % 3600) / 60;
int hours = seconds / 3600;

return new int[]{hours, minutes, seconds % 60, (int) millis % 1000};
static void logColor(String msg, Color color) {
System.out.printf(
"%s%s [%s] %s%n",
color,
LocalDateTime.now(),
ConfigInterface.Config.LoggingLevel.INFO,
msg
);
}
}
15 changes: 15 additions & 0 deletions src/main/java/CodeCheck/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,19 @@ static void write(String filePath, String msg, boolean append) {
e.printStackTrace();
}
}

/**
* Calculate the duration from the start time and the current time.
*
* @param startTime Input the start time.
* @return An integer array of time the duration in HH:MM:SS:MS. 0 = h, 1 = m, 2 = s, 3 = ms
*/
static int[] getFormattedDurationTime(long startTime) {
long millis = (System.currentTimeMillis() - startTime);
int seconds = (int) millis / 1000;
int minutes = (seconds % 3600) / 60;
int hours = seconds / 3600;

return new int[]{hours, minutes, seconds % 60, (int) millis % 1000};
}
}
15 changes: 9 additions & 6 deletions src/main/java/CodeCheck/WriteObjectToFile.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package CodeCheck;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteObjectToFile {

Expand All @@ -21,10 +19,15 @@ public WriteObjectToFile() throws Exception {
Log.log("Debug mode enabled, deleting the temporary file %s before continuing... ".formatted(PATH_TO_RESULTS + File.separator + TEMP_FILE));
}

// Create results directory if it doesn't exist.
if (!(new File(PATH_TO_RESULTS).exists())) {
new File(PATH_TO_RESULTS).mkdir();
Log.log("Created directory %s...", PATH_TO_RESULTS);
// Create directory if it doesn't exist.
File file = new File(PATH_TO_RESULTS);
if (!file.isDirectory()) {
if (file.mkdirs()) {
Log.logColor("Created directory %s...".formatted(file.getAbsolutePath()), Log.Color.PURPLE);
} else {
Log.error("Could not create directory %s...".formatted(file.getAbsolutePath()));
throw new Exception("Could not create directory %s...".formatted(file.getAbsolutePath()));
}
}

// Look for the next file to write towards.
Expand Down

0 comments on commit 0880a19

Please sign in to comment.