From 516b66439f5ef74992c7449011d231c7b1ed2482 Mon Sep 17 00:00:00 2001 From: user86 Date: Sat, 10 Oct 2020 08:13:52 +0530 Subject: [PATCH] Feat: Integrated uploading file and question generation; needs fixes --- app.py | 8 ++- info.txt | 1 - requirements.txt | 31 +++++++++++ templates/quiz.html | 129 +++++++++++++++++--------------------------- workers.py | 14 ++++- 5 files changed, 98 insertions(+), 85 deletions(-) delete mode 100644 info.txt diff --git a/app.py b/app.py index 4b159c9..853d683 100644 --- a/app.py +++ b/app.py @@ -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__": diff --git a/info.txt b/info.txt deleted file mode 100644 index 5639b0b..0000000 --- a/info.txt +++ /dev/null @@ -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. \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2f1f862..5141dfa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/templates/quiz.html b/templates/quiz.html index c3dd774..97c92cc 100644 --- a/templates/quiz.html +++ b/templates/quiz.html @@ -1,85 +1,54 @@ - - - - - - MLH Quizzet - - - + Home + {% if uploaded == true %} + + {% for i in range(size) %} +
+
+

Question {{ i+1 }}

+
+

+ {{ questions[i+1]['question'] }} +

+
+
+
+ {% for j in questions[i+1]['options'] %} + +
- -
- - - - - - -
-
-
-
- {% else %} -

Could not upload file

- {% endif %} - - + + + + {% endfor %} + {% else %} +

Could not upload file

+ {% endif %} + + + \ No newline at end of file diff --git a/workers.py b/workers.py index 996f02c..0e224ed 100644 --- a/workers.py +++ b/workers.py @@ -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)