Feat: Integrated uploading file and question generation; needs fixes

This commit is contained in:
user86 2020-10-10 08:13:52 +05:30
parent 3076819535
commit 516b66439f
5 changed files with 98 additions and 85 deletions

8
app.py
View File

@ -2,7 +2,7 @@ import os
from flask import Flask, render_template, redirect, url_for
from flask.globals import request
from werkzeug.utils import secure_filename
from workers import pdf2text
from workers import pdf2text, txt2questions
# Constants
UPLOAD_FOLDER = './pdf/'
@ -36,16 +36,20 @@ def quiz():
file_path = os.path.join(
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename))
file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower()
questions = ''
# Save uploaded file
uploaded_file.save(file_path)
# Get contents of file
uploaded_content = pdf2text(file_path, file_exten)
questions = txt2questions(uploaded_content)
# File upload + convert success
if uploaded_content is not None:
UPLOAD_STATUS = True
except Exception as e:
print(e)
return render_template('quiz.html', uploaded=UPLOAD_STATUS)
return render_template('quiz.html', uploaded=UPLOAD_STATUS, questions=questions, size=len(questions))
if __name__ == "__main__":

View File

@ -1 +0,0 @@
I have worked as a backend developer for creating REST API based based systems and worked with front-end integration for MEAN stack apps. For developing apps with a decoupled architecture. I have focused on security and performance to improve response time. I am well versed in Python, NodeJS and related tools like redis for building and maintaining a scalable architecture. I also have deployed production ready apps used by hundreds of people using AWS EC2, S3 and other cloud based tools. I frequently contribute to open source and support open source tools by creating issues, PRs for tools that I use frequently to try and give back to the community.

View File

@ -1,8 +1,39 @@
blis==0.4.1
catalogue==1.0.0
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
cymem==2.0.3
en-core-web-md @ https://github.com/explosion/spacy-models/releases/download/en_core_web_md-2.3.1/en_core_web_md-2.3.1.tar.gz
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.3.1/en_core_web_sm-2.3.1.tar.gz
Flask==1.1.2
gensim==3.8.3
idna==2.10
importlib-metadata==2.0.0
itsdangerous==1.1.0
Jinja2==2.11.2
joblib==0.17.0
MarkupSafe==1.1.1
murmurhash==1.0.2
nltk==3.5
numpy==1.19.2
pkg-resources==0.0.0
plac==1.1.3
preshed==3.0.2
PyPDF2==1.26.0
regex==2020.9.27
requests==2.24.0
scikit-learn==0.23.2
scipy==1.5.2
six==1.15.0
sklearn==0.0
smart-open==3.0.0
spacy==2.3.2
srsly==1.0.2
thinc==7.4.1
threadpoolctl==2.1.0
tqdm==4.50.2
urllib3==1.25.10
wasabi==0.8.0
Werkzeug==1.0.1
zipp==3.3.0

View File

@ -1,30 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css"
/>
<link
rel="stylesheet"
type="text/css"
href="{{ url_for('static',filename='css/quiz.css') }}"
/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/quiz.css') }}" />
<title>MLH Quizzet</title>
</head>
<body>
<nav
class="navbar is-dark is-fixed-top has-text-centered"
role="navigation"
aria-label="main navigation"
>
<nav class="navbar is-dark is-fixed-top has-text-centered" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item has-text-centered" href="#"
><strong class="is-size-3 has-text-centered">Question 1</strong></a
>
<a class="navbar-item has-text-centered" href="#"><strong class="is-size-3 has-text-centered">MLH
Quizzet</strong></a>
<div class="navbar-burger" data-target="navMenu">
<span></span>
<span></span>
@ -34,52 +23,32 @@
</nav>
<a href="{{ url_for('index') }}"> Home </a>
{% if uploaded == true %}
{% for i in range(size) %}
<section class="section-1" id="section-1">
<main>
<p class="has-text-weight-bold has-text-black is-size-2">Question {{ i+1 }}</p>
<div class="text-container">
<h3 class="has-text-weight-bold has-text-black is-size-3">
MLH Quizzet
</h3>
<p class="has-text-weight-bold has-text-black is-size-4">
What does CSS stand for?
{{ questions[i+1]['question'] }}
</p>
</div>
<form>
<div class="quiz-options">
<input
type="radio"
class="input-radio one-a"
id="one-a"
name="yes-1"
required
/>
{% for j in questions[i+1]['options'] %}
<input type="radio" class="input-radio one-a" id="one-a" name="yes-1" required />
<label class="radio-label" for="one-a">
<span class="alphabet">A</span> Cascading Style Sheets
</label>
<input
type="radio"
class="input-radio one-b"
id="one-b"
name="yes-1"
/>
<label class="radio-label" for="one-b">
<span class="alphabet">B</span> Creative Screen Styling
</label>
<input
type="radio"
class="input-radio one-c"
id="one-c"
name="yes-1"
/>
<label class="radio-label" for="one-c">
<span class="alphabet">C</span> Cascading Style Screen
</label>
<span class="alphabet">{{ j+1 }}</span> {{ questions[i+1]['options'][j+1] }}
<br />
{% endfor %}
</div>
</form>
</main>
</section>
{% endfor %}
{% else %}
<h1>Could not upload file</h1>
{% endif %}
</body>
</html>

View File

@ -1,16 +1,19 @@
from PyPDF2 import PdfFileReader
from question_generation_main import QuestionGeneration
def pdf2text(file_path, file_exten) -> str:
""" Converts a given file to text content """
_content = None
_content = ''
# Identify file type and get its contents
if file_exten == 'pdf':
with open(file_path, 'rb') as pdf_file:
_pdf_reader = PdfFileReader(pdf_file)
_content = _pdf_reader
# for p in range(_pdf_reader.numPages):
# _content += _pdf_reader.getPage(p).extractText()
_content = _pdf_reader.getPage(0).extractText()
print('PDF operation done!')
elif file_exten == 'txt':
@ -19,3 +22,10 @@ def pdf2text(file_path, file_exten) -> str:
print('TXT operation done!')
return _content
def txt2questions(doc, n=1, o=4):
""" Get all questions and options """
qGen = QuestionGeneration(n, o)
return qGen.generate_questions_dict(doc)