|
|
from flask import Flask, request, jsonify, abort , render_template , send_file |
|
|
import tsadropboxretrieval |
|
|
import json |
|
|
import Find_Hyperlinking_text |
|
|
import findspecsv1 |
|
|
import requests |
|
|
from io import BytesIO |
|
|
import urllib |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
pdf_content = None |
|
|
pageNumTextFound = 0 |
|
|
BASE_URL = "https://marthee-nbslink.hf.space" |
|
|
|
|
|
@app.route("/", methods=["GET", "POST"]) |
|
|
def thismain(): |
|
|
print('ayhaga') |
|
|
return render_template("gui.html") |
|
|
|
|
|
@app.route('/view-pdf', methods=['GET']) |
|
|
def download_pdf(): |
|
|
|
|
|
|
|
|
full_query_string = request.query_string.decode() |
|
|
parsed_params = urllib.parse.parse_qs(full_query_string) |
|
|
|
|
|
|
|
|
pdf_link = parsed_params.get('pdfLink', [None])[0] |
|
|
keyword = parsed_params.get('keyword', [None])[0] |
|
|
|
|
|
if not pdf_link or not keyword: |
|
|
return "Missing required parameters.", 400 |
|
|
|
|
|
|
|
|
pdf_link = urllib.parse.unquote(pdf_link) |
|
|
keyword = urllib.parse.unquote(keyword) |
|
|
|
|
|
|
|
|
try: |
|
|
keyword = json.loads(keyword) |
|
|
except json.JSONDecodeError: |
|
|
keyword = [keyword] |
|
|
|
|
|
print("Extracted PDF Link:", pdf_link) |
|
|
print("Extracted Keywords:", keyword) |
|
|
createDF=False |
|
|
|
|
|
pdf_content = findspecsv1.extract_section_under_header(pdf_link, keyword)[0] |
|
|
if pdf_content is None: |
|
|
return "PDF content not found.", 404 |
|
|
|
|
|
pdf_bytes = BytesIO(pdf_content) |
|
|
return send_file( |
|
|
pdf_bytes, |
|
|
mimetype='application/pdf', |
|
|
as_attachment=False, |
|
|
download_name=f"annotated_page_{pageNumTextFound}.pdf" |
|
|
) |
|
|
|
|
|
@app.route('/api/process-data', methods=['POST']) |
|
|
def receive_pdf_data(): |
|
|
global pdf_content, pageNumTextFound |
|
|
|
|
|
|
|
|
pdfLink, keyword = finddata() |
|
|
|
|
|
if not pdfLink or not keyword: |
|
|
return jsonify({"error": "Both 'pdfLink' and 'keyword' must be provided."}), 400 |
|
|
|
|
|
try: |
|
|
print(pdfLink, keyword) |
|
|
|
|
|
|
|
|
pdfbytes, pdf_document , df ,tablepdfoutput= findspecsv1.extract_section_under_header(pdfLink, keyword) |
|
|
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user') |
|
|
|
|
|
|
|
|
metadata = dbxTeam.sharing_get_shared_link_metadata(pdfLink) |
|
|
dbPath='/TSA JOBS/ADR Test/FIND/' |
|
|
pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname=metadata.name) |
|
|
print('LINKS0',pdflink) |
|
|
|
|
|
dbPath='/TSA JOBS/ADR Test/FIND/' |
|
|
tablepdfLink=tsadropboxretrieval.uploadanyFile(doc=tablepdfoutput,path=dbPath,pdfname=metadata.name.rsplit(".pdf", 1)[0] +' Markup Summary'+'.pdf') |
|
|
print(f"PDF successfully uploaded to Dropbox at") |
|
|
print('LINKS1',tablepdfLink) |
|
|
return jsonify({ |
|
|
"message": "PDF processed successfully.", |
|
|
"PDF_MarkedUp": pdflink, |
|
|
'Table_PDF_Markup_Summary': tablepdfLink |
|
|
}) |
|
|
|
|
|
except Exception as e: |
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
def finddata(): |
|
|
pdfLink = 'https://www.dropbox.com/scl/fi/hnp4mqigb51a5kp89kgfa/00801-ARC-20-ZZ-S-A-0002.pdf?rlkey=45abeoebzqw4qwnslnei6dkd6&st=m4yrcjm2&dl=1' |
|
|
keyword = ['115 INTEGRATED MRI ROOM LININGS', '310 ACCURACY'] |
|
|
return pdfLink, keyword |
|
|
|
|
|
@app.route('/apiNBSData', methods=['POST']) |
|
|
def NBSData(): |
|
|
|
|
|
|
|
|
try: |
|
|
print('In process [Try]') |
|
|
data = request.get_json() |
|
|
|
|
|
pdfLink = data.get('filePath') |
|
|
keyword = data.get('NBS_List') |
|
|
|
|
|
|
|
|
if not pdfLink or not keyword: |
|
|
return jsonify({"error": "Both 'pdfLink' and 'keyword' must be provided."}), 400 |
|
|
|
|
|
keyword = eval(keyword) |
|
|
|
|
|
|
|
|
print(pdfLink, keyword) |
|
|
pdfbytes, pdf_document , df ,tablepdfoutput= findspecsv1.extract_section_under_header(pdfLink, keyword) |
|
|
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user') |
|
|
|
|
|
|
|
|
metadata = dbxTeam.sharing_get_shared_link_metadata(pdfLink) |
|
|
dbPath='/TSA JOBS/ADR Test/FIND/' |
|
|
pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname=metadata.name) |
|
|
print('LINKS0',pdflink) |
|
|
|
|
|
dbPath='/TSA JOBS/ADR Test/FIND/' |
|
|
tablepdfLink=tsadropboxretrieval.uploadanyFile(doc=tablepdfoutput,path=dbPath,pdfname=metadata.name.rsplit(".pdf", 1)[0] +' Markup Summary'+'.pdf') |
|
|
print(f"PDF successfully uploaded to Dropbox at") |
|
|
print('LINKS1',tablepdfLink) |
|
|
return jsonify(tablepdfoutput) |
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error: {e}") |
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(host='0.0.0.0', port=7860) |
|
|
|