Skip to content

Commit

Permalink
feat: connect react.js to flask via proxy server
Browse files Browse the repository at this point in the history
  • Loading branch information
JinCoreana committed Jun 26, 2023
1 parent faed152 commit 3acef02
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 85 deletions.
20 changes: 7 additions & 13 deletions cv_anonymisation/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from flask import Flask, flash, request, redirect, url_for, render_template, send_from_directory
from flask import Flask, flash, request, redirect, make_response, render_template, send_from_directory
from werkzeug.utils import secure_filename
from utils.anonymizer import anonymize

Expand All @@ -23,7 +23,7 @@ def allowed_file(filename):
@app.route('/uploads/<string:name>')
def download_file(name):

return send_from_directory(app.config["DOWNLOAD_FOLDER"], name, as_attachment=True)
return send_from_directory(app.config["DOWNLOAD_FOLDER"], name, as_attachment=False)

@app.route("/anonymise", methods=['GET', 'POST'])
def home():
Expand All @@ -41,19 +41,13 @@ def home():
if file and allowed_file(file.filename):
filename = secure_filename(file.filename) # type: ignore
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
'''file.save(filepath)
file.save(filepath)
ofilename = 'anonymized_cv.txt'
ofilepath = os.path.join(app.config['DOWNLOAD_FOLDER'], ofilename)
with open(ofilepath,'w', encoding='utf-8') as f:
f.write(anonymize(filepath))'''
response_body = {
'result': anonymize(filepath),
'message': 'Data processed successfully'
}
return response_body, 200

return render_template('index.html')

f.write(anonymize(filepath))

return send_from_directory(app.config["DOWNLOAD_FOLDER"], ofilename, as_attachment=True)

if __name__ == "__main__":
app.run()
app.run()
65 changes: 0 additions & 65 deletions cv_anonymisation/templates/index.html

This file was deleted.

4 changes: 2 additions & 2 deletions cv_anonymisation/utils/anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def final_filter(candidate_id: int, text: str) -> str:
personal_data = _get_personal_data(candidate_id)
keyword_list = _get_keywords(personal_data)
for keyword in keyword_list:
text = re.sub(str(keyword), "*****", text, flags=re.IGNORECASE)
escaped_keyword = re.escape(keyword)
text = re.sub(escaped_keyword, "*****", text, flags=re.IGNORECASE)
return text


def anonymize(filename):
text = extract_text(filename)
mock_candidate_id = filename.split('.pdf')[-2][-1] # Get mock candidate id from filename
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dei_hackathon",
"version": "0.1.0",
"homepage": "https://jincoreana.github.io/dei_hackathon_2023",
"proxy": "http://localhost:5000",
"proxy": "http://127.0.0.1:5000/",
"private": true,
"dependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
Expand Down
8 changes: 4 additions & 4 deletions src/pages/cvAnonymisation/CvAnonymisationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const CvAnonymisationPage = () => {
const handleButtonClick = (e: any) => {
e.preventDefault();
fileInputRef.current?.click();
setAnonymisedData("");
};

const handleFileChange = (event: any) => {
Expand All @@ -37,14 +38,13 @@ const CvAnonymisationPage = () => {
formData.append("file", submitData, selectedFile);
try {
//Used direct url for now. To be refactored with proxy server
const response = await fetch("http://localhost:5000/anonymise", {
const response = await fetch("/anonymise", {
method: "POST",
body: formData,
});

if (response.ok) {
console.log(response);
setAnonymisedData(response.body as unknown as string);
const parsedResponse = await response.text();
setAnonymisedData(parsedResponse);
} else {
console.error("Anonymisation Failed", response.status);
}
Expand Down

0 comments on commit 3acef02

Please sign in to comment.