Skip to content

Commit

Permalink
Adding a new button to test rule
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrt committed Jun 30, 2023
1 parent 5686b2e commit cae66b7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ repos:
- id: autopep8
files: demo/
exclude: only_lists.py
- repo: https://github.com/pre-commit/mirrors-prettier
rev: '' # Use the sha / tag you want to point at
hooks:
- id: prettier
- files: ^experimental/

37 changes: 37 additions & 0 deletions experimental/rule_inference/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ def improve_rules(data):
)


@socketio.on("test_rule")
def test_rule(data):
# Extract data
language = data.get("language", "")
rules = data.get("rules", "")
source_code = data.get("source_code", "")
target_code = data.get("target_code", "")

# Here, you can use your existing implementation to test the rule
# Assuming you have a function 'test_rules' in the 'PiranhaAgent' class
agent = PiranhaAgent(
source_code,
target_code,
language=language,
hints="",
)

completion = f"```toml{rules}```\n```md```"

try:
test_result = agent.validate_rule(completion)
# Emit the result back to the client
socketio.emit(
"test_result",
{
"test_result": "Success",
},
)
except Exception as e:
socketio.emit(
"test_result",
{
"test_result": "Error",
},
)


if __name__ == "__main__":
openai.api_key = os.getenv("OPENAI_API_KEY")
app.run(debug=True)
44 changes: 44 additions & 0 deletions experimental/rule_inference/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
submitButtonImprovement: document.getElementById(
"submit-button-improvement",
),
testButton: document.getElementById("test-button"),
};

elements.languageSelect.addEventListener("change", handleLanguageChange);
Expand Down Expand Up @@ -88,6 +89,24 @@
emitImproveEvent();
});

elements.testButton.addEventListener("click", async function () {
emitTestEvent();
});

// Add a function to emit test event
function emitTestEvent() {
const sourceCode = editors.codeBefore.getValue();
const targetCode = editors.codeAfter.getValue();
const rules = editors.queryEditor.getValue();
const language = elements.languageSelect.value;
// Here you may want to adjust the data to fit your backend needs
socket.emit("test_rule", {
source_code: sourceCode,
target_code: targetCode,
rules: rules,
language: language,
});
}
function emitRefactorEvent() {
const folderPath = document.getElementById("folder-input").value;
const rules = editors.queryEditor.getValue();
Expand Down Expand Up @@ -153,6 +172,31 @@
displayButton(false, "Apply Rules", "folder");
});

// Add a new socket listener for the test result
socket.on("test_result", function (data) {
console.log(data);
// Here you may want to handle the test result
let button = document.getElementById("test-button");

// Add appropriate CSS class based on the test result
button.classList.remove("btn-success", "btn-danger");
if (data.test_result === "Success") {
button.classList.add("btn-success");
} else {
button.classList.add("btn-danger");
}
button.textContent = data.test_result;

// Set a timeout to fade the button back to the original state
setTimeout(() => {
button.classList.remove("btn-success", "btn-danger");
button.classList.add("btn-primary");
button.textContent = "Test Rule";
}, 3000);

return button;
});

function updateInterface(rule) {
document.getElementById("query-container").style.display = "block";
document.getElementById("gpt-rule-explanation-container").style.display =
Expand Down
4 changes: 0 additions & 4 deletions experimental/rule_inference/static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,3 @@ a.highlighted {
#submit-folder-button:hover {
background-color: #0069d9;
}

.custom-container {
max-width: 80%; /* Adjust this value to your liking */
}
13 changes: 10 additions & 3 deletions experimental/rule_inference/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@
<body>
<h3 class="text-center">Piranha Inference Playground</h3>
<div id="playground-container" class="container custom-container">
<div class="row">
<div class="row align-items-center justify-content-between">
<div class="col-lg-2">
<select class="form-select" id="language-select">
<option value="java" selected="selected">Java</option>
<option value="kt">Kotlin</option>
</select>
</div>
<div class="col-lg-2">
<button id="test-button" class="btn btn-primary">
<span>Test Rule</span>
</button>
</div>
</div>

<div class="row">
<div class="col-lg-6" id="code-container">
<h5>Code before</h5>
Expand Down Expand Up @@ -81,8 +87,9 @@ <h5>
</button>
</div>
</div>

<div class="row justify-content-center">
<div class="col-lg-6" id="query-container" style="display: none">
<div class="col-lg-6" id="query-container" style="display: block">
<h5>Rules</h5>
<textarea id="query-input"></textarea>
</div>
Expand All @@ -98,7 +105,7 @@ <h5></h5>

<div class="row justify-content-center">
<div class="col-lg-12" id="explanation-container" style="display: none">
<h5>Request GPT to add enclosing_node or contains filters. Please be very specific describing what you want to accomplish</h5>
<h5>Request GPT to add enclosing node or contains filters.</h5>
<textarea id="explanation-input"></textarea>
</div>
<div class="col-auto">
Expand Down
2 changes: 2 additions & 0 deletions experimental/rule_inference/tmp.4TsIo14Xn6U7/sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1
class A {}

0 comments on commit cae66b7

Please sign in to comment.