Add application file
Browse files- .gitignore +1 -0
- main.py +16 -2
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
token.txt
|
main.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
|
|
@@ -7,6 +8,12 @@ from fastapi.responses import FileResponse
|
|
| 7 |
from model import extract_contact
|
| 8 |
from vcf import generate_vcf, VCF_DIR
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
app = FastAPI(title="Business Card Extractor", version="1.0")
|
| 11 |
|
| 12 |
|
|
@@ -21,12 +28,17 @@ async def extract(file: UploadFile = File(...)):
|
|
| 21 |
try:
|
| 22 |
suffix = os.path.splitext(file.filename)[1]
|
| 23 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp:
|
| 24 |
-
|
|
|
|
| 25 |
image_path = temp.name
|
|
|
|
| 26 |
|
| 27 |
data = extract_contact(image_path)
|
|
|
|
|
|
|
| 28 |
vcf_path = generate_vcf(data)
|
| 29 |
filename = os.path.basename(vcf_path)
|
|
|
|
| 30 |
|
| 31 |
return {
|
| 32 |
"success": True,
|
|
@@ -35,7 +47,9 @@ async def extract(file: UploadFile = File(...)):
|
|
| 35 |
"download": f"/download/{filename}",
|
| 36 |
}
|
| 37 |
except Exception as e:
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
finally:
|
| 40 |
if image_path and os.path.exists(image_path):
|
| 41 |
os.remove(image_path)
|
|
|
|
| 1 |
+
import logging
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
|
|
|
|
| 8 |
from model import extract_contact
|
| 9 |
from vcf import generate_vcf, VCF_DIR
|
| 10 |
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level=logging.INFO,
|
| 13 |
+
format="%(asctime)s %(levelname)s: %(message)s",
|
| 14 |
+
)
|
| 15 |
+
log = logging.getLogger("cardextractor")
|
| 16 |
+
|
| 17 |
app = FastAPI(title="Business Card Extractor", version="1.0")
|
| 18 |
|
| 19 |
|
|
|
|
| 28 |
try:
|
| 29 |
suffix = os.path.splitext(file.filename)[1]
|
| 30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp:
|
| 31 |
+
body = await file.read()
|
| 32 |
+
temp.write(body)
|
| 33 |
image_path = temp.name
|
| 34 |
+
log.info("received %s (%d bytes) -> %s", file.filename, len(body), image_path)
|
| 35 |
|
| 36 |
data = extract_contact(image_path)
|
| 37 |
+
log.info("extracted: %s", data)
|
| 38 |
+
|
| 39 |
vcf_path = generate_vcf(data)
|
| 40 |
filename = os.path.basename(vcf_path)
|
| 41 |
+
log.info("wrote vcf: %s", filename)
|
| 42 |
|
| 43 |
return {
|
| 44 |
"success": True,
|
|
|
|
| 47 |
"download": f"/download/{filename}",
|
| 48 |
}
|
| 49 |
except Exception as e:
|
| 50 |
+
# ponytail: exc_info dumps the full traceback to the container logs
|
| 51 |
+
log.exception("extract failed for %s", file.filename)
|
| 52 |
+
raise HTTPException(500, f"{type(e).__name__}: {e}")
|
| 53 |
finally:
|
| 54 |
if image_path and os.path.exists(image_path):
|
| 55 |
os.remove(image_path)
|