Spaces:
Running
Running
Commit ·
c70dbbf
1
Parent(s): ec3f76a
[Cursor] Improve API with Mangum and better error handling
Browse files- index.py +188 -0
- requirements.txt +5 -13
- vercel.json +26 -0
index.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
from mangum import Mangum
|
| 6 |
+
import magic
|
| 7 |
+
import requests
|
| 8 |
+
from werkzeug.utils import secure_filename
|
| 9 |
+
import datetime
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # 10MB max file size
|
| 13 |
+
|
| 14 |
+
def is_valid_file(file_obj):
|
| 15 |
+
"""Check if file type is allowed using python-magic"""
|
| 16 |
+
try:
|
| 17 |
+
mime = magic.from_buffer(file_obj.read(2048), mime=True)
|
| 18 |
+
file_obj.seek(0) # Reset file pointer
|
| 19 |
+
allowed_mimes = [
|
| 20 |
+
'application/pdf',
|
| 21 |
+
'text/plain',
|
| 22 |
+
'text/html',
|
| 23 |
+
'text/markdown',
|
| 24 |
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
| 25 |
+
]
|
| 26 |
+
return mime in allowed_mimes
|
| 27 |
+
except Exception:
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
def download_file(url):
|
| 31 |
+
"""Download file from URL and save to temp file"""
|
| 32 |
+
try:
|
| 33 |
+
response = requests.get(url, stream=True, timeout=10)
|
| 34 |
+
response.raise_for_status()
|
| 35 |
+
|
| 36 |
+
# Get content type
|
| 37 |
+
content_type = response.headers.get('content-type', '').split(';')[0]
|
| 38 |
+
if content_type not in [
|
| 39 |
+
'application/pdf',
|
| 40 |
+
'text/plain',
|
| 41 |
+
'text/html',
|
| 42 |
+
'text/markdown',
|
| 43 |
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
| 44 |
+
]:
|
| 45 |
+
raise ValueError(f"Unsupported content type: {content_type}")
|
| 46 |
+
|
| 47 |
+
# Create temp file with proper extension
|
| 48 |
+
ext = {
|
| 49 |
+
'application/pdf': '.pdf',
|
| 50 |
+
'text/plain': '.txt',
|
| 51 |
+
'text/html': '.html',
|
| 52 |
+
'text/markdown': '.md',
|
| 53 |
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': '.docx'
|
| 54 |
+
}.get(content_type, '')
|
| 55 |
+
|
| 56 |
+
fd, temp_path = tempfile.mkstemp(suffix=ext)
|
| 57 |
+
os.close(fd)
|
| 58 |
+
|
| 59 |
+
# Download file
|
| 60 |
+
with open(temp_path, 'wb') as f:
|
| 61 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 62 |
+
f.write(chunk)
|
| 63 |
+
|
| 64 |
+
return temp_path, content_type
|
| 65 |
+
except Exception as e:
|
| 66 |
+
raise ValueError(f"Failed to download file: {str(e)}")
|
| 67 |
+
|
| 68 |
+
@app.route('/')
|
| 69 |
+
def home():
|
| 70 |
+
return jsonify({
|
| 71 |
+
"status": "ok",
|
| 72 |
+
"message": "Document Processing API",
|
| 73 |
+
"version": "1.0.0"
|
| 74 |
+
})
|
| 75 |
+
|
| 76 |
+
@app.route('/health')
|
| 77 |
+
def health():
|
| 78 |
+
return jsonify({
|
| 79 |
+
"status": "healthy",
|
| 80 |
+
"timestamp": str(datetime.datetime.now(datetime.UTC))
|
| 81 |
+
})
|
| 82 |
+
|
| 83 |
+
@app.route('/parse/file', methods=['POST'])
|
| 84 |
+
def parse_file():
|
| 85 |
+
try:
|
| 86 |
+
if 'file' not in request.files:
|
| 87 |
+
return jsonify({
|
| 88 |
+
"error": "No file provided",
|
| 89 |
+
"details": "Please provide a file in the 'file' field"
|
| 90 |
+
}), 400
|
| 91 |
+
|
| 92 |
+
file = request.files['file']
|
| 93 |
+
if not file.filename:
|
| 94 |
+
return jsonify({
|
| 95 |
+
"error": "No file selected",
|
| 96 |
+
"details": "Please select a file to upload"
|
| 97 |
+
}), 400
|
| 98 |
+
|
| 99 |
+
if not is_valid_file(file):
|
| 100 |
+
return jsonify({
|
| 101 |
+
"error": "Invalid file type",
|
| 102 |
+
"details": "Supported formats: PDF, TXT, HTML, MD, DOCX"
|
| 103 |
+
}), 400
|
| 104 |
+
|
| 105 |
+
# Save to temp file
|
| 106 |
+
filename = secure_filename(file.filename)
|
| 107 |
+
fd, temp_path = tempfile.mkstemp(suffix=os.path.splitext(filename)[1])
|
| 108 |
+
os.close(fd)
|
| 109 |
+
file.save(temp_path)
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
# Process file here
|
| 113 |
+
return jsonify({
|
| 114 |
+
"status": "success",
|
| 115 |
+
"message": "File processed successfully",
|
| 116 |
+
"metadata": {
|
| 117 |
+
"filename": filename,
|
| 118 |
+
"size": os.path.getsize(temp_path),
|
| 119 |
+
"mime_type": magic.from_file(temp_path, mime=True)
|
| 120 |
+
}
|
| 121 |
+
})
|
| 122 |
+
finally:
|
| 123 |
+
# Clean up temp file
|
| 124 |
+
try:
|
| 125 |
+
os.unlink(temp_path)
|
| 126 |
+
except:
|
| 127 |
+
pass
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
return jsonify({
|
| 131 |
+
"error": "Processing failed",
|
| 132 |
+
"details": str(e)
|
| 133 |
+
}), 500
|
| 134 |
+
|
| 135 |
+
@app.route('/parse/url', methods=['POST'])
|
| 136 |
+
def parse_url():
|
| 137 |
+
try:
|
| 138 |
+
data = request.get_json()
|
| 139 |
+
if not data or 'url' not in data:
|
| 140 |
+
return jsonify({
|
| 141 |
+
"error": "No URL provided",
|
| 142 |
+
"details": "Please provide a URL in the request body"
|
| 143 |
+
}), 400
|
| 144 |
+
|
| 145 |
+
url = data['url']
|
| 146 |
+
if not url.startswith(('http://', 'https://')):
|
| 147 |
+
return jsonify({
|
| 148 |
+
"error": "Invalid URL",
|
| 149 |
+
"details": "URL must start with http:// or https://"
|
| 150 |
+
}), 400
|
| 151 |
+
|
| 152 |
+
# Download and process file
|
| 153 |
+
temp_path, content_type = download_file(url)
|
| 154 |
+
try:
|
| 155 |
+
# Process file here
|
| 156 |
+
return jsonify({
|
| 157 |
+
"status": "success",
|
| 158 |
+
"message": "URL processed successfully",
|
| 159 |
+
"metadata": {
|
| 160 |
+
"url": url,
|
| 161 |
+
"content_type": content_type,
|
| 162 |
+
"size": os.path.getsize(temp_path)
|
| 163 |
+
}
|
| 164 |
+
})
|
| 165 |
+
finally:
|
| 166 |
+
# Clean up temp file
|
| 167 |
+
try:
|
| 168 |
+
os.unlink(temp_path)
|
| 169 |
+
except:
|
| 170 |
+
pass
|
| 171 |
+
|
| 172 |
+
except ValueError as e:
|
| 173 |
+
return jsonify({
|
| 174 |
+
"error": "Invalid request",
|
| 175 |
+
"details": str(e)
|
| 176 |
+
}), 400
|
| 177 |
+
except Exception as e:
|
| 178 |
+
return jsonify({
|
| 179 |
+
"error": "Processing failed",
|
| 180 |
+
"details": str(e)
|
| 181 |
+
}), 500
|
| 182 |
+
|
| 183 |
+
# Create Mangum handler
|
| 184 |
+
handler = Mangum(app, lifespan="off")
|
| 185 |
+
|
| 186 |
+
# For local development
|
| 187 |
+
if __name__ == '__main__':
|
| 188 |
+
app.run(debug=True)
|
requirements.txt
CHANGED
|
@@ -1,13 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
python-
|
| 5 |
-
|
| 6 |
-
beautifulsoup4>=4.12.0
|
| 7 |
-
lxml>=4.9.0
|
| 8 |
-
gradio>=4.44.1
|
| 9 |
-
pandas>=1.5.0
|
| 10 |
-
huggingface-hub>=0.19.0
|
| 11 |
-
python-magic-bin>=0.4.14; platform_system == "Windows"
|
| 12 |
-
libmagic; platform_system == "Linux"
|
| 13 |
-
requests>=2.31.0
|
|
|
|
| 1 |
+
flask==2.0.1
|
| 2 |
+
werkzeug==2.0.1
|
| 3 |
+
mangum==0.17.0
|
| 4 |
+
python-magic==0.4.27
|
| 5 |
+
requests==2.31.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vercel.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"version": 2,
|
| 3 |
+
"builds": [
|
| 4 |
+
{
|
| 5 |
+
"src": "index.py",
|
| 6 |
+
"use": "@vercel/python",
|
| 7 |
+
"config": {
|
| 8 |
+
"maxLambdaSize": "10mb",
|
| 9 |
+
"runtime": "python3.9"
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
],
|
| 13 |
+
"routes": [
|
| 14 |
+
{
|
| 15 |
+
"src": "/(.*)",
|
| 16 |
+
"dest": "index.py",
|
| 17 |
+
"headers": {
|
| 18 |
+
"Access-Control-Allow-Origin": "*",
|
| 19 |
+
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With",
|
| 20 |
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
| 21 |
+
"Access-Control-Max-Age": "86400",
|
| 22 |
+
"Cache-Control": "public, max-age=0, must-revalidate"
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
]
|
| 26 |
+
}
|