-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (77 loc) · 2.81 KB
/
main.py
File metadata and controls
102 lines (77 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import shutil
from urllib.parse import quote
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from pdf_parser import extract_text_and_images
from report_generator import create_report
app = FastAPI()
# Upload directory
UPLOAD_DIR = os.path.join(os.path.dirname(__file__), "uploads")
os.makedirs(UPLOAD_DIR, exist_ok=True)
# Serve uploaded images
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
# Allow Streamlit frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
BASE_URL = "https://ddr-ai-system.onrender.com"
@app.post("/generate-ddr")
async def generate_ddr(
inspection_report: UploadFile = File(...),
thermal_report: UploadFile = File(...)
):
try:
# Save inspection report
inspection_path = os.path.join(UPLOAD_DIR, inspection_report.filename)
with open(inspection_path, "wb") as f:
shutil.copyfileobj(inspection_report.file, f)
# Save thermal report
thermal_path = os.path.join(UPLOAD_DIR, thermal_report.filename)
with open(thermal_path, "wb") as f:
shutil.copyfileobj(thermal_report.file, f)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to save uploads: {e}")
# Extract text and images
ins_data = extract_text_and_images(inspection_path, UPLOAD_DIR)
ther_data = extract_text_and_images(thermal_path, UPLOAD_DIR)
# Combine images
images = ins_data.get("images", []) + ther_data.get("images", [])
# Remove duplicates while keeping order
seen = set()
unique_images = []
for img in images:
name = os.path.basename(img)
if name not in seen:
seen.add(name)
unique_images.append(img)
# Limit to maximum 500 images (performance)
unique_images = unique_images[:500]
extracted = {
"inspection_text": ins_data.get("text", "")[:3000],
"thermal_text": ther_data.get("text", "")[:3000],
"images": unique_images,
}
# Convert filesystem paths → public URLs
extracted["images"] = [
f"{BASE_URL}/uploads/{quote(os.path.basename(img))}"
for img in extracted["images"]
]
# Generate AI report
try:
report = create_report(extracted)
except Exception as e:
raise HTTPException(status_code=500, detail=f"AI processing failed: {e}")
return JSONResponse(content={"extracted": extracted, "report": report})
@app.get("/")
def root():
return {"message": "DDR generator API is running"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)