Spaces:
Sleeping
Sleeping
File size: 1,376 Bytes
a06317b | 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 | 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}
|