Spaces:
Sleeping
Sleeping
Download app/api.py from DarkLofi/construtech-api: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/DarkLofi/construtech-api/resolve/main/app/api.py
- Command line
-
hf download hf://spaces/DarkLofi/construtech-api/app/api.py
-
curl -L -o api.py https://huggingface.co/spaces/DarkLofi/construtech-api/resolve/main/app/api.py
1.38 kB
| 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 | |
| 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} | |
| 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} | |