From 29e8ba2e3a0d432cf456818472e2654cca82f1f8 Mon Sep 17 00:00:00 2001 From: Ashad Qureshi Date: Sat, 19 Oct 2024 18:38:18 +0500 Subject: [PATCH] ci/cd --- .github/workflows/sync2hf.yaml | 25 ++++++++++++ DOCKERFILE | 13 +++++++ app.py | 70 ++++++++++++++++++++++++++++++++++ plan/agents.py | 1 - requirements.txt | 2 + 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/sync2hf.yaml create mode 100644 DOCKERFILE create mode 100644 app.py diff --git a/.github/workflows/sync2hf.yaml b/.github/workflows/sync2hf.yaml new file mode 100644 index 0000000..9d321ac --- /dev/null +++ b/.github/workflows/sync2hf.yaml @@ -0,0 +1,25 @@ +name: Sync to Hugging Face hub + +on: + push: + branches: [main] + workflow_dispatch: + +jobs: + sync-to-hub: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Push to Hugging Face hub + env: + HF_TOKEN: ${{ secrets.HF_SECRET }} + run: | + git remote remove origin || true + git remote add origin https://Ashad001:${{ secrets.HF_SECRET }}@huggingface.co/spaces/Ashad001/roomaligner + git add . + git commit -m "Sync code to Hugging Face Hub" || true + git push --force origin main diff --git a/DOCKERFILE b/DOCKERFILE new file mode 100644 index 0000000..7d59e53 --- /dev/null +++ b/DOCKERFILE @@ -0,0 +1,13 @@ +FROM python:3.12 + +RUN useradd -m -u 1000 user +USER user +ENV PATH="/home/user/.local/bin:$PATH" + +WORKDIR /app + +COPY --chown=user ./requirements.txt requirements.txt +RUN pip install --no-cache-dir --upgrade -r requirements.txt + +COPY --chown=user . /app +CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..a2d59fb --- /dev/null +++ b/app.py @@ -0,0 +1,70 @@ +import os +import base64 +import json +from io import BytesIO +from fastapi import FastAPI, UploadFile, File, HTTPException +from fastapi.responses import JSONResponse +from fastapi.middleware.cors import CORSMiddleware +from dotenv import load_dotenv +from plan.agents import GeneratePlan, GenerateSuggestions +from plan.image_analyzer import ImageAnalyzer +from raster.raster import InferenceClient +from utils.preprocess_data import preprocess_data +from PIL import Image + +load_dotenv() + +app = FastAPI(title="Interior Designer API") + +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# required classes +inference_client = InferenceClient() +plan_generator = GeneratePlan() +image_analyzer = ImageAnalyzer() +suggestions_generator = GenerateSuggestions() + +@app.post("/upload-room-image/") +async def upload_image(file: UploadFile = File(...)): + """ + Upload room image and get natural language description and suggested room layout. + """ + try: + contents = await file.read() + img = Image.open(BytesIO(contents)) + + # Convert image to base64 + buffered = BytesIO() + img.save(buffered, format="JPEG") + image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8') + + # Step 1: Classify objects in the image + result = inference_client.infer_image(image_base64) + data = preprocess_data(result) + + # Step 2: Generate a natural language description + nl_description = plan_generator.forward(data) + + # Step 3: Analyze room structure and generate suggestions + room_structure = image_analyzer.generate_floor_plan_details(image_base64, nl_description) + suggested_structure = suggestions_generator.forward(room_structure.content) + + # Return response as JSON + return JSONResponse(content={ + "natural_language_description": nl_description, + "room_structure": room_structure.content, + "suggested_structure": suggested_structure + }) + + except Exception as e: + raise HTTPException(status_code=500, detail=f"An error occurred: {e}") + +@app.get("/") +def read_root(): + return {"message": "Welcome to the Interior Designer API!"} diff --git a/plan/agents.py b/plan/agents.py index 227dd28..a6646a8 100644 --- a/plan/agents.py +++ b/plan/agents.py @@ -5,7 +5,6 @@ from typing import List, Dict, Any load_dotenv(".env") -print(os.getenv("OPENAI_API_KEY")) lm = dspy.OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"), max_tokens=4096) dspy.settings.configure(lm=lm) diff --git a/requirements.txt b/requirements.txt index d39b363..fdde1bc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,6 +24,7 @@ dill==0.3.8 diskcache==5.6.3 distro==1.9.0 dspy==2.5.3 +fastapi==0.115.2 filelock==3.16.1 fonttools==4.54.1 frozenlist==1.4.1 @@ -87,6 +88,7 @@ six==1.16.0 smmap==5.0.1 sniffio==1.3.1 SQLAlchemy==2.0.35 +starlette==0.40.0 streamlit==1.39.0 structlog==24.4.0 supervision==0.23.0