Skip to content

Commit 8b256c0

Browse files
committed
Enhance documentation with detailed docstrings for analysis module functions; improve code readability and maintainability
1 parent 0887c3b commit 8b256c0

File tree

1 file changed

+76
-22
lines changed

1 file changed

+76
-22
lines changed

document_analysis/document_analysis/analysis.py

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,38 @@
33
Blueprint, flash, g, redirect, render_template, request, url_for, current_app,
44
send_from_directory, jsonify
55
)
6-
from werkzeug.exceptions import abort
7-
from werkzeug.utils import secure_filename
8-
from document_analysis.auth import login_required
9-
from document_analysis.db import get_db
10-
import google.generativeai as genai
11-
import PyPDF2
12-
import docx
13-
import json
6+
from werkzeug.exceptions import abort # abort , used to return an HTTP status code
7+
from werkzeug.utils import secure_filename # secure_filename, used to secure the filename of the uploaded file
8+
from document_analysis.auth import login_required # login_required, used to require authentication to access a view
9+
from document_analysis.db import get_db # get_db, used to get a connection to the database
10+
import google.generativeai as genai # Import the generativeai module from the google package
11+
import PyPDF2 # Import the PyPDF2 module
12+
import docx # Import the docx module
13+
import json
1414

15-
bp = Blueprint('analysis', __name__)
15+
bp = Blueprint('analysis', __name__) # Blueprint object for the analysis module, that defines the routes and views for the analysis module
1616

1717
def allowed_file(filename):
18+
"""Check if a file has an allowed extension.
19+
20+
Args:
21+
filename (str): The name of the file to check.
22+
23+
Returns:
24+
bool: True if the file has an allowed extension, False otherwise.
25+
"""
1826
return '.' in filename and \
1927
filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_EXTENSIONS']
2028

2129
def extract_text(file):
30+
"""Extract text from a file.
31+
32+
Args:
33+
file (FileStorage): The file from which to extract text.
34+
35+
Returns:
36+
str: The extracted text from the file.
37+
"""
2238
filename = secure_filename(file.filename)
2339
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
2440
file.save(file_path)
@@ -41,17 +57,22 @@ def extract_text(file):
4157

4258
return text
4359

44-
@bp.route('/')
60+
@bp.route('/') # Define the index route for the analysis module
4561
def index():
4662
return render_template('analysis/index.html')
4763

48-
@bp.route('/landing')
64+
@bp.route('/landing') # Define the landing route for the analysis module
4965
def landing():
5066
return render_template('analysis/landing.html')
5167

52-
@bp.route('/dashboard')
68+
@bp.route('/dashboard') # Define the dashboard route for the analysis module it requires authentication to access
5369
@login_required
5470
def dashboard():
71+
"""Render the dashboard view for the analysis module.
72+
73+
Returns:
74+
_io.TextIOWrapper: The rendered dashboard view.
75+
"""
5576
db = get_db()
5677
documents = db.execute(
5778
'SELECT d.id, title, content, created, author_id, username'
@@ -73,17 +94,22 @@ def dashboard():
7394
documents=documents,
7495
recent_analysis=recent_analysis)
7596

76-
@bp.route('/upload', methods=['POST'])
97+
@bp.route('/upload', methods=['POST']) # Define the upload route for the analysis module it requires authentication to access
7798
@login_required
7899
def upload_document():
100+
"""Upload a document file and extract text from it.
101+
102+
Returns:
103+
str: A JSON response with a message indicating if the file was successfully processed or an error
104+
"""
79105
if 'file' not in request.files:
80106
return jsonify({"error": "No file part"}), 400
81107

82108
file = request.files['file']
83109
if file.filename == '':
84110
return jsonify({"error": "No selected file"}), 400
85111

86-
if file and allowed_file(file.filename):
112+
if file and allowed_file(file.filename): # allowed_file func call
87113
try:
88114
text = extract_text(file)
89115
db = get_db()
@@ -99,9 +125,17 @@ def upload_document():
99125

100126
return jsonify({"error": "File type not allowed"}), 400
101127

102-
@bp.route('/analyze/<int:id>', methods=['GET'])
128+
@bp.route('/analyze/<int:id>', methods=['GET']) # Define the analyze route for the analysis module it requires authentication to access
103129
@login_required
104130
def analyze(id):
131+
"""Analyze a document by generating a quiz, summary, keywords, or translation.
132+
133+
Args:
134+
id (int): The id of the document to analyze.
135+
136+
Returns:
137+
_io.TextIOWrapper: The rendered analysis view.
138+
"""
105139
document = get_db().execute(
106140
'SELECT d.id, title, content, created, author_id, username'
107141
' FROM document d JOIN user u ON d.author_id = u.id'
@@ -124,9 +158,14 @@ def analyze(id):
124158
document=document,
125159
analyses=analyses)
126160

127-
@bp.route('/generate_quiz', methods=['POST'])
161+
@bp.route('/generate_quiz', methods=['POST']) # Define the generate_quiz route for the analysis module it requires authentication to access
128162
@login_required
129163
def generate_quiz():
164+
"""Generate a quiz based on the provided text.
165+
166+
Returns:
167+
str: A JSON response with the generated quiz or an error message
168+
"""
130169
data = request.get_json()
131170
text = data.get('text')
132171
document_id = data.get('document_id')
@@ -143,7 +182,7 @@ def generate_quiz():
143182
144183
Text: {text}"""
145184

146-
model = genai.GenerativeModel('gemini-pro')
185+
model = genai.GenerativeModel('gemini-pro') # Create a generative model object.
147186
response = model.generate_content(prompt)
148187

149188
if document_id:
@@ -155,11 +194,16 @@ def generate_quiz():
155194
)
156195
db.commit()
157196

158-
return jsonify({"quiz": response.text})
197+
return jsonify({"quiz": response.text})
159198

160-
@bp.route('/summarize', methods=['POST'])
199+
@bp.route('/summarize', methods=['POST']) # Define the summarize route for the analysis module it requires authentication to access
161200
@login_required
162201
def summarize():
202+
"""Summarize the provided text.
203+
204+
Returns:
205+
str: A JSON response with the generated summary or an error message
206+
"""
163207
data = request.get_json()
164208
text = data.get('text')
165209
document_id = data.get('document_id')
@@ -184,9 +228,13 @@ def summarize():
184228

185229
return jsonify({"summary": response.text})
186230

187-
@bp.route('/extract_keywords', methods=['POST'])
231+
@bp.route('/extract_keywords', methods=['POST']) # Define the extract_keywords route for the analysis module it requires authentication to access
188232
@login_required
189233
def extract_keywords():
234+
"""Extract keywords from the provided text.
235+
236+
Returns: str: A JSON response with the extracted keywords or an error message
237+
"""
190238
data = request.get_json()
191239
text = data.get('text')
192240
document_id = data.get('document_id')
@@ -224,9 +272,14 @@ def extract_keywords():
224272

225273
return jsonify({"keywords": keywords})
226274

227-
@bp.route('/translate', methods=['POST'])
275+
@bp.route('/translate', methods=['POST']) # Define the translate route for the analysis module it requires authentication to access
228276
@login_required
229277
def translate():
278+
"""Translate the provided text to the target language.
279+
280+
Returns:
281+
str: A JSON response with the translated text or an error message
282+
"""
230283
data = request.get_json()
231284
text = data.get('text')
232285
target_language = data.get('target_language')
@@ -249,4 +302,5 @@ def translate():
249302
)
250303
db.commit()
251304

252-
return jsonify({"translation": response.text})
305+
return jsonify({"translation": response.text})
306+

0 commit comments

Comments
 (0)