Juan Palomino commited on
Commit
225be25
·
1 Parent(s): 6a58fd5

Deploy Document Format Converter

Browse files
Files changed (3) hide show
  1. README.md +30 -3
  2. app.py +20 -0
  3. samples/sample.json +1 -0
README.md CHANGED
@@ -1,3 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Document Format Converter
2
 
3
  Upload document and select target format for conversion.
@@ -8,7 +21,11 @@ This application provides a web interface for document conversion. Simply upload
8
 
9
  ## API Endpoints
10
 
11
- The application also provides API endpoints for programmatic access:
 
 
 
 
12
 
13
  ### 1. DOCX to JSON Conversion
14
 
@@ -18,13 +35,18 @@ import requests
18
  # URL of the API endpoint
19
  url = "https://huggingface.co/spaces/ObiJuanCodenobi/docgen/api/docx-to-json"
20
 
 
 
 
 
 
21
  # Prepare the file for upload
22
  files = {
23
  'file': ('document.docx', open('path/to/your/document.docx', 'rb'), 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
24
  }
25
 
26
  # Send the request
27
- response = requests.post(url, files=files)
28
 
29
  # Get the JSON result
30
  if response.status_code == 200:
@@ -42,13 +64,18 @@ import requests
42
  # URL of the API endpoint
43
  url = "https://huggingface.co/spaces/ObiJuanCodenobi/docgen/api/json-to-docx"
44
 
 
 
 
 
 
 
45
  # Your JSON document data
46
  json_data = {
47
  # Your document structure here
48
  }
49
 
50
  # Send the request
51
- headers = {'Content-Type': 'application/json'}
52
  response = requests.post(url, json=json_data, headers=headers)
53
 
54
  # Save the DOCX file
 
1
+ ---
2
+ title: Document Format Converter
3
+ emoji: 🙊
4
+ colorFrom: green
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 5.13.2
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ short_description: Upload document and select target format for conversion.
12
+ ---
13
+
14
  # Document Format Converter
15
 
16
  Upload document and select target format for conversion.
 
21
 
22
  ## API Endpoints
23
 
24
+ The application provides API endpoints for programmatic access. **API key authentication is required.**
25
+
26
+ ### Authentication
27
+
28
+ All API requests require an API key to be sent in the `X-API-Key` header.
29
 
30
  ### 1. DOCX to JSON Conversion
31
 
 
35
  # URL of the API endpoint
36
  url = "https://huggingface.co/spaces/ObiJuanCodenobi/docgen/api/docx-to-json"
37
 
38
+ # API key for authentication
39
+ headers = {
40
+ 'X-API-Key': 'YOUR_API_KEY'
41
+ }
42
+
43
  # Prepare the file for upload
44
  files = {
45
  'file': ('document.docx', open('path/to/your/document.docx', 'rb'), 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
46
  }
47
 
48
  # Send the request
49
+ response = requests.post(url, files=files, headers=headers)
50
 
51
  # Get the JSON result
52
  if response.status_code == 200:
 
64
  # URL of the API endpoint
65
  url = "https://huggingface.co/spaces/ObiJuanCodenobi/docgen/api/json-to-docx"
66
 
67
+ # API key for authentication
68
+ headers = {
69
+ 'X-API-Key': 'YOUR_API_KEY',
70
+ 'Content-Type': 'application/json'
71
+ }
72
+
73
  # Your JSON document data
74
  json_data = {
75
  # Your document structure here
76
  }
77
 
78
  # Send the request
 
79
  response = requests.post(url, json=json_data, headers=headers)
80
 
81
  # Save the DOCX file
app.py CHANGED
@@ -14,6 +14,7 @@ import sys
14
  import tempfile
15
  from flask import Flask, request, jsonify, send_file
16
  import threading
 
17
 
18
  os.system('sudo apt-get install texlive')
19
 
@@ -521,11 +522,26 @@ if __name__ == "__main__":
521
  parity_check(sys.argv[2])
522
  sys.exit(0)
523
 
 
 
 
 
524
  # Create Flask app for API endpoints
525
  app = Flask(__name__)
526
 
 
 
 
 
 
 
 
527
  @app.route('/api/docx-to-json', methods=['POST'])
528
  def api_docx_to_json():
 
 
 
 
529
  if 'file' not in request.files:
530
  return jsonify({"error": "No file part"}), 400
531
 
@@ -558,6 +574,10 @@ if __name__ == "__main__":
558
 
559
  @app.route('/api/json-to-docx', methods=['POST'])
560
  def api_json_to_docx():
 
 
 
 
561
  if not request.is_json:
562
  return jsonify({"error": "Request must be JSON"}), 400
563
 
 
14
  import tempfile
15
  from flask import Flask, request, jsonify, send_file
16
  import threading
17
+ import secrets
18
 
19
  os.system('sudo apt-get install texlive')
20
 
 
522
  parity_check(sys.argv[2])
523
  sys.exit(0)
524
 
525
+ # Generate a random API key if one doesn't exist in environment variables
526
+ API_KEY = os.environ.get('API_KEY', secrets.token_urlsafe(32))
527
+ print(f"API Key: {API_KEY}") # Print the API key when the app starts
528
+
529
  # Create Flask app for API endpoints
530
  app = Flask(__name__)
531
 
532
+ def check_api_key():
533
+ """Check if the API key is valid."""
534
+ provided_key = request.headers.get('X-API-Key')
535
+ if not provided_key or provided_key != API_KEY:
536
+ return False
537
+ return True
538
+
539
  @app.route('/api/docx-to-json', methods=['POST'])
540
  def api_docx_to_json():
541
+ # Check API key
542
+ if not check_api_key():
543
+ return jsonify({"error": "Invalid or missing API key"}), 401
544
+
545
  if 'file' not in request.files:
546
  return jsonify({"error": "No file part"}), 400
547
 
 
574
 
575
  @app.route('/api/json-to-docx', methods=['POST'])
576
  def api_json_to_docx():
577
+ # Check API key
578
+ if not check_api_key():
579
+ return jsonify({"error": "Invalid or missing API key"}), 401
580
+
581
  if not request.is_json:
582
  return jsonify({"error": "Request must be JSON"}), 400
583
 
samples/sample.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"title": "Sample Document", "content": "This is a sample JSON document for testing the converter."}