Marthee commited on
Commit
d19d78e
·
verified ·
1 Parent(s): 9e6686c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -49
app.py CHANGED
@@ -8,65 +8,58 @@ app = Flask(__name__)
8
  def thismain():
9
  print('ayhaga')
10
  return jsonify('done')
11
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  @app.route('/api/process-data', methods=['POST'])
13
- def process():
14
- # check_api_key()
15
- try:
16
-
17
- print('In process [Try]')
18
- data = request.form
19
- # Extracting values
20
- filePath = data.get('filePath')
21
- groupName = data.get('groupName')
22
 
23
- pdftext = pdftotext.texts_from_pdf(filePath,groupName)
24
- return jsonify(pdftext)
25
-
26
- except Exception as e:
27
- print(f"Error: {e}")
28
- return jsonify({"error": str(e)}), 500
29
 
 
 
30
 
31
- @app.route("/apiMC",methods=["GET", "POST"])
32
- def apifunction():
33
  try:
34
- print('In process [Try]')
35
- # data = request.form
36
- data = request.get_json()
37
- print('here',data)
38
- # Extracting values
39
- if data:
40
- # Pass the raw JSON data to the filtering function
41
- jsonOutput = pdftotext.apiFiltering(data)
42
- return jsonify(jsonOutput)
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
-
46
  except Exception as e:
47
- print(f"Error: {e}")
48
  return jsonify({"error": str(e)}), 500
49
 
50
 
51
  if __name__ == '__main__':
52
  app.run(host='0.0.0.0', port=7860)
53
- # from flask import Flask, request, jsonify
54
-
55
- # app = Flask(__name__)
56
-
57
- # @app.route('/api/process-data', methods=['POST'])
58
- # def process_data():
59
- # # Get JSON data from the request
60
- # data = request.get_json()
61
-
62
- # # Perform some processing on the data
63
- # # For example, let's just return the data back with a message
64
- # processed_data = {
65
- # 'message': 'Data received and processed successfully!',
66
- # 'received_data': data
67
- # }
68
-
69
- # return jsonify(processed_data)
70
-
71
- # if __name__ == '__main__':
72
- # app.run(host='0.0.0.0', port=7860, debug=True)
 
8
  def thismain():
9
  print('ayhaga')
10
  return jsonify('done')
11
+
12
+ pdf_content = None
13
+ pageNumTextFound = 0
14
+ BASE_URL = "hhttps://huggingface.co/spaces/Marthee/NBSLink" # Localhost URL for testing
15
+
16
+ @app.route('/view-pdf', methods=['GET'])
17
+ def download_pdf():
18
+ global pdf_content, pageNumTextFound
19
+
20
+ if pdf_content is None:
21
+ return "PDF content not found.", 404
22
+
23
+ pdf_bytes = BytesIO(pdf_content)
24
+ return send_file(
25
+ pdf_bytes,
26
+ mimetype='application/pdf',
27
+ as_attachment=False,
28
+ download_name=f"annotated_page_{pageNumTextFound}.pdf"
29
+ )
30
+
31
  @app.route('/api/process-data', methods=['POST'])
32
+ def receive_pdf_data():
33
+ global pdf_content, pageNumTextFound
 
 
 
 
 
 
 
34
 
35
+ # Get PDF link and keyword from the request body
36
+ pdf_link = request.form.get('pdf_link')
37
+ keyword = request.form.get('keyword')
 
 
 
38
 
39
+ if not pdf_link or not keyword:
40
+ return jsonify({"error": "Both 'pdf_link' and 'keyword' must be provided."}), 400
41
 
 
 
42
  try:
43
+ # Call the function to process the PDF
44
+ keyword = json.loads(keyword)
 
 
 
 
 
 
 
45
 
46
+ global pdf_content
47
+ pdf_content, pageNumTextFound,highlight_rect = Find_Hyperlinking_text.annotate_text_from_pdf([pdf_link], keyword)
48
+
49
+ if pdf_content is None:
50
+ return jsonify({"error": "No valid PDF content found."}), 404
51
+
52
+ # Construct the URL in the desired format with the rectangle coordinates
53
+ download_link = f"{BASE_URL}/view-pdf#page={pageNumTextFound}&zoom={highlight_rect}"
54
+
55
+ return jsonify({
56
+ "message": "PDF processed successfully.",
57
+ "download_link": download_link # Return the formatted URL
58
+ })
59
 
 
60
  except Exception as e:
 
61
  return jsonify({"error": str(e)}), 500
62
 
63
 
64
  if __name__ == '__main__':
65
  app.run(host='0.0.0.0', port=7860)