Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
yaashwardhan authored Mar 13, 2023
1 parent 4bf7912 commit e989feb
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 39 deletions.
12 changes: 9 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ def generate_questions_endpoint():
data = request.get_json()
context = data['context']
question_count = int(data['questionCount'])
incorrect_options_count = int(data['incorrectOptionsCount'])
questions = generate_question(context, question_count, incorrect_options_count)
return jsonify({'questions': json.dumps(questions)})
questions, correctAnswers, distractor1, distractor2, distractor3= generate_question(context, question_count)
response = {
'questions': questions,
'correctAnswers': correctAnswers,
'distractor1': distractor1,
'distractor2': distractor2,
'distractor3': distractor3
}
return jsonify(response)

# if __name__ == '__main__':
# app.run(debug=False)
Expand Down
37 changes: 25 additions & 12 deletions generate_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,35 @@ def get_distractors (word,origsentence,sense2vecmodel,sentencemodel,top_n,lambda



def generate_question(context, questionCount, IncorrectOptionsCount):
def generate_question(context, questionCount):
summary_text = context
np = get_keywords(context, summary_text, questionCount)
output = ""
questions = []
correctAnswers = []
distractor1 = []
distractor2 = []
distractor3 = []
for answer in np:
ques = get_question(summary_text, answer, question_model, question_tokenizer)
distractors = get_distractors(answer.capitalize(), ques, s2v, sentence_transformer_model, 40, 0.2)
output = output + ques + "\n"
output = output + "Ans: " + answer.capitalize() + "\n"

questions.append(ques)
correctAnswers.append(answer.capitalize())
if len(distractors) > 0:
for distractor in distractors[:IncorrectOptionsCount]:
output = output + distractor + "\n"
for i, distractor in enumerate(distractors[:4]):
if i == 0:
distractor1.append(distractor)
elif i == 1:
distractor2.append(distractor)
elif i == 2:
distractor3.append(distractor)
else:
output = output + "Ignore Question. Couldnt Find Distractors" + "\n"
output = output + "\n"

output = output + "\n"
return output
for i, distractor in enumerate(distractors[:4]):
if i == 0:
distractor1.append("Couldnt Generate")
elif i == 1:
distractor2.append("Couldnt Generate")
elif i == 2:
distractor3.append("Couldnt Generate")

print(questions, correctAnswers, distractor1, distractor2, distractor3)
return questions, correctAnswers, distractor1, distractor2, distractor3
62 changes: 52 additions & 10 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<script src="static/scripts/sliderDynamics.js"></script>
<link href="https://fonts.googleapis.com/css?family=Poppins" rel="stylesheet">
<script src="http://127.0.0.1:5500/livereload.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.14.0/Sortable.min.js"></script>
<link rel="shortcut icon" href="#">
<body>
<nav>
<img src="static/assets/icon.png" id="logo" />
Expand All @@ -26,7 +28,7 @@
<label for="context" class="subsubheadings" style="font-size: 12px;">Word Count: <span id="word-count" style="color: greenyellow;">0</span></label><br><br>
<div style="display:flex;">
<div style="display:flex; flex-direction: column; align-items: flex-start;">
<textarea class="contextPara" id="context" name="context" rows="20" cols="60" style="color: #ffc475;">Python is a programming language that was created in the late 1980s by Guido van Rossum. Its design philosophy emphasizes code readability and simplicity, which means that Python code is easy to read and write. This makes it a great language for beginners, as well as experienced developers who want to work more efficiently.
<textarea class="contextPara" id="context" name="context" rows="20" cols="60" style="color: #ffc475;">Python is a programming language that was created in the late 1980s by Guido van Rossum. Its design philosophy emphasizes code readability and simplicity, which means that Python code is easy to read and write. This makes it a great language for beginners who want to work more efficiently.

Python is a versatile language that can be used for a wide variety of tasks. It's commonly used in web development to build websites and web applications. It's also popular in scientific computing and data analysis, where it's used to analyze and manipulate large datasets.

Expand All @@ -35,24 +37,64 @@
Additionally, Python is an open-source language, which means that it's free to use and modify. Overall, Python is a powerful and versatile programming language that's easy to learn and use. Whether you're just starting out or you're an experienced developer, Python can help you build a wide variety of projects and accomplish your goals more quickly and efficiently.</textarea><br>
<div style="margin-right:0px;">
<label for="question-count" class="subsubheadings">Number of Questions: <span id="slider-value" style="color: greenyellow;font-weight: 800;font-size: 18px;">0</span></label><br>
<input type="range" id="question-count" min="0" max="10" step="1" value="0"><br><br>
</div>
<div>
<label for="incorrect-options-count" class="subsubheadings">Number of Incorrect Options:</label>
<input type="number" id="incorrect-options-count" name="incorrect-options-count" min="1" max="4" value="3"><br><br>
</div>

<input type="range" id="question-count" min="1" max="10" step="1" value="1"><br><br>
</div>
<input type="submit" class="bn632" value="Generate Questions">
</div>
<div style="display:flex; flex-direction: column;margin-left:40px;">
<div id="loading-screen">
<img width=220px src="static/assets/NN-spinner.gif">
<img width=420px src="static/assets/NN-spinner.gif">
</div>
<div id="questions" style="color:white; font-family: 'Poppins', sans-serif;"></div>

<div id="questions" class = "questionHeading" style="color:white; font-family: 'Poppins', sans-serif;"></div>

</div>
</div>
</form>
<button id="save-button" style="float: right;" class="bn632">Save Questions</button>
</div>
</body>


<script>
// Save questions to JSON file
$('#save-button').click(function() {
var questionsData = [];

$('.question').each(function() {
var question = $(this).find('p').text();
var options = [];

$(this).find('.option').each(function() {
var option = $(this).text();
options.push(option);
});

var questionData = {
'question': question,
'options': options
};

questionsData.push(questionData);
});

var json = JSON.stringify(questionsData, null, 2).replace(/\u2326/g, '').replace(/\u2630/g, '');
console.log(json);
var blob = new Blob([json], {type: "application/json"});
var url = URL.createObjectURL(blob);

var a = document.createElement('a');
a.download = 'questions.json';
a.href = url;
a.textContent = 'Download questions';
document.body.appendChild(a);

a.click();

document.body.removeChild(a);
URL.revokeObjectURL(url);
});
</script>

</html>

67 changes: 58 additions & 9 deletions static/scripts/postAjaxHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,81 @@ $(document).ready(function() {
event.preventDefault();
var context = $('#context').val();
var questionCount = $('#question-count').val();
var incorrectOptionsCount = $('#incorrect-options-count').val();


// Show loading screen
$('#loading-screen').show();

$('#questions').html('');
$.ajax({
type: 'POST',
url: '/generate-questions',
contentType: 'application/json',
data: JSON.stringify({
'context': context,
'questionCount': questionCount,
'incorrectOptionsCount': incorrectOptionsCount
}),
success: function(response) {
var questionsHtml = response['questions'].replace(/\\n/g, '<br>').replace(/"/g, '');
$('#questions').html(questionsHtml);
console.log(response);
var questions = response['questions'];
var correctAnswers = response['correctAnswers'];
var distractor1 = response['distractor1'];
var distractor2 = response['distractor2'];
var distractor3 = response['distractor3'];
var html = '';

for (var i = 0; i < questions.length; i++) {
var options = [correctAnswers[i]].concat(distractor1[i]).concat(distractor2[i]).concat(distractor3[i]);
html += '<div class="question">';
html += '<p>' + questions[i] + '</p>';
html += '<ul class="options" data-question="' + i + '" data-group="' + i + '">';
html += '<li class="option" style="color: greenyellow;" data-value="' + options[0] + '">' + "<span style='color:dimgrey;'>&#9776;</span>" + options[0] + '<span class="remove-option" style="color:#CC5500;">&#8998;</span></li>';
html += '<li class="option" data-value="' + options[1] + '">' + "<span style='color:dimgrey;'>&#9776;</span>" + options[1] +'<span class="remove-option" style="color:#CC5500;">&#8998;</span></li>';
html += '<li class="option" data-value="' + options[2] + '">' + "<span style='color:dimgrey;'>&#9776;</span>" + options[2] +'<span class="remove-option" style="color:#CC5500;">&#8998;</span></li>';
html += '<li class="option" data-value="' + options[3] + '">' + "<span style='color:dimgrey;'>&#9776;</span>" + options[3] +'<span class="remove-option" style="color:#CC5500;">&#8998;</span></li>';

html += '<div class="custom-option"><input type="text" placeholder="Add answer"><span class="add-option">+</span></div>';

html += '</ul>';
html += '</div>';
}
$('#questions').html(html);
console.log(questions);
console.log(correctAnswers);
console.log(distractor1);
console.log(distractor2);
console.log(distractor3);
$('.options').each(function() {
new Sortable(this, {
group: $(this).data('group'),
animation: 150,
filter: '.add-option'
});
});


// Remove option
$(document).on('click', '.remove-option', function() {
$(this).parent().remove();
});

// Add custom option
$(document).on('click', '.add-option', function() {
var $input = $(this).prev('input');
var value = $input.val();
if (value.trim() !== '') {
var $ul = $(this).parent().parent();
var questionIndex = $ul.attr('data-question');
var $li = $('<li>', {'class': 'option', 'data-value': value}).text(value);
$li.append('<span class="remove-option" style="color:#CC5500;">&#8998;</span>');
$ul.append($li);
$input.val('');
}
});


// Hide loading screen
$('#loading-screen').hide();
},
error: function(xhr, status, error) {
console.log(xhr.responseText);

// Hide loading screen
$('#loading-screen').hide();
}
});
Expand Down
6 changes: 2 additions & 4 deletions static/scripts/sliderDynamics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ document.addEventListener("DOMContentLoaded", function() {
function updateSliderMax() {
const wordCount = textarea.value.trim().split(/\s+/).length;

if (wordCount <= 100) {
if (wordCount <= 200) {
slider.max = 3;
} else if (wordCount <= 200) {
slider.max = 5;
} else {
slider.max = 10;
slider.max = 5;
}
}

Expand Down
48 changes: 47 additions & 1 deletion static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ display: none;
font-family: 'Poppins',
sans-serif;
}

.questionHeading{
margin: 0px;
color:#eeeeee;
font-size: 18px;
font-weight: 500;
font-family: 'Poppins',
sans-serif;
}
.contextPara {
background-color: #202831;
border: 1px solid dimgrey;
Expand Down Expand Up @@ -146,4 +155,41 @@ nav {
}
.bn632:hover{
background-color: #1db954;
}
}

.option{
margin: 0px;
color:#eeeeee;
font-size: 14px;
font-weight: 300;
font-family: 'Poppins',
sans-serif;
}

ul.options {
margin: 0;
padding: 0;
}

li.option {
display: flex; /* make the element a flex container */
justify-content: space-between; /* align text to the left and icon to the right */
align-items: center; /* vertically center text and icon */
width: 100%; /* set the width of the element to 100% */
background-color: #202831; /* add a grey background color */
padding: 8px; /* add some padding to the element */
margin-bottom: 2px;
box-sizing: border-box; /* include padding in the width calculation */

}

li.option span:first-child {
flex: 1;
text-align: left;
}


li.option span.remove-option {
flex: 5;
color: #CC5500;text-align: right;
}

0 comments on commit e989feb

Please sign in to comment.