from fastapi import FastAPI, UploadFile, File, Form from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel import os import json import shutil import sys import tempfile sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from ai_engine.analyzer import estimate_materials_from_text, analyze_construction_photo app = FastAPI(title="Barueltech ConstruTech API") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) class EstimateRequest(BaseModel): description: str @app.post("/api/estimate") async def estimate_api(req: EstimateRequest): result_str = estimate_materials_from_text(req.description) try: return json.loads(result_str) except: return {"error": result_str} @app.post("/api/analyze") async def analyze_api(file: UploadFile = File(...)): temp_path = os.path.join(tempfile.gettempdir(), f"temp_{file.filename}") with open(temp_path, "wb") as buffer: shutil.copyfileobj(file.file, buffer) result_str = analyze_construction_photo(temp_path) if os.path.exists(temp_path): os.remove(temp_path) try: return json.loads(result_str) except: return {"error": result_str}