File size: 5,475 Bytes
dac3297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# FastAPI Setup - What You Need to Do

## Summary: 3 Steps

1. **Refactor `analyze_blueprint.py`** β€” Extract core logic into a reusable function (you do this)
2. **Push to GitHub** β€” Create a repo and push your code (you do this)
3. **Deploy on Railway** β€” Click a button and it's live (Railway does this)

---

## Step-by-Step

### STEP 1: Refactor Your Analyzer (30 mins)

Your `analyze_blueprint.py` has a `main()` function that's tightly coupled to CLI arguments. The FastAPI needs a cleaner interface.

**What to do:**

Open `python_files/analyze_blueprint.py` and find where `main()` starts (around line 2242).

Add this new function BEFORE `main()`:

```python
def analyze_pdf(pdf_path: str, out_dir: str, dpi: int = 150, min_conf: int = 40):
    """
    Analyze a PDF blueprint and return results (non-CLI version).
    
    Args:
        pdf_path: Path to the blueprint PDF
        out_dir: Directory to save output files
        dpi: Resolution for rendering (150 = fast, 300 = accurate)
        min_conf: YOLO confidence threshold (0-100)
    
    Returns:
        tuple: (floor_results, schedule_tags)
    """
    
    # 1. Load PDF and get manifest
    pages_manifest = _load_and_classify_pages(Path(pdf_path), dpi=dpi)
    
    # 2. Extract schedule from schedule pages
    schedule_tags = _extract_all_schedules(Path(pdf_path), pages_manifest)
    
    # 3. Run YOLO detection on plan pages
    floor_results = _detect_units_on_plans(Path(pdf_path), pages_manifest, min_conf=min_conf, dpi=dpi)
    
    # 4. Reconcile detections with schedule
    floor_results = _reconcile_detections(floor_results, schedule_tags)
    
    # 5. Save output files (CSVs, images) to out_dir
    Path(out_dir).mkdir(parents=True, exist_ok=True)
    _save_results_to_disk(floor_results, schedule_tags, Path(out_dir))
    
    return floor_results, schedule_tags
```

The functions like `_load_and_classify_pages()` already exist in your code β€” they're just currently called from within `main()`. You're just reorganizing them.

**Easiest approach:**
- Copy the core logic from `main()` into `analyze_pdf()`
- Keep all the helper functions as-is
- Update the import in `app.py` to call `analyze_pdf` instead of the stub

Don't worry about perfection β€” just make it work.

---

### STEP 2: Test Locally (15 mins)

```bash
cd /path/to/hvac_project

# Install FastAPI dependencies
pip install fastapi uvicorn python-multipart

# Test the API
python -m uvicorn app:app --reload
```

Visit `http://localhost:8000/docs` in your browser. You'll see an interactive API explorer.

Click "Try it out" on `/analyze`, upload a test PDF, and see if it works.

**If it works:** You're good for deployment.
**If it fails:** Check the error message, fix `analyze_blueprint.py`, and retry.

---

### STEP 3: Push to GitHub (10 mins)

```bash
# Initialize git repo (if not already done)
cd /path/to/hvac_project
git init

# Add all files
git add .

# Commit
git commit -m "Add FastAPI backend"

# Create repo on GitHub at https://github.com/new
# Then push (replace YOUR_USERNAME and hvac-analyzer with your details)
git remote add origin https://github.com/YOUR_USERNAME/hvac-analyzer.git
git branch -M main
git push -u origin main
```

---

### STEP 4: Deploy on Railway (5 mins)

1. Go to https://railway.app
2. Click "New Project"
3. Select "Deploy from GitHub repo"
4. Authorize Railway to access your GitHub
5. Select your `hvac-analyzer` repo
6. Railway auto-deploys (takes ~2 minutes)
7. You get a URL: `https://hvac-analyzer-prod-abc123.railway.app`

Done!

---

## What Happens When You Update Code

### Workflow:

```bash
# Make a change to app.py or analyze_blueprint.py
vim app.py

# Test locally
python -m uvicorn app:app --reload
# Visit http://localhost:8000/docs and test

# Push to GitHub
git add .
git commit -m "Improve YOLO confidence handling"
git push origin main
```

### Railway watches and auto-deploys:
1. You push to `main`
2. Railway gets notified (via webhook)
3. Railway pulls your code
4. Reinstalls dependencies from `requirements.txt`
5. Starts the app using `Procfile`
6. Your URL gets the new code (usually within 2-3 minutes)

You can watch the deployment in the Railway dashboard:
- Dashboard β†’ Your Project β†’ Deployments β†’ click the latest one β†’ View Logs

---

## What If Something Breaks?

**Scenario 1: Code has a bug**
- Railway will show a failed deployment
- Your old version keeps running (no downtime)
- Fix the code locally, test, push again
- Railway auto-deploys the fix

**Scenario 2: Missing dependency**
- Error: `ModuleNotFoundError: No module named 'foo'`
- Add to `requirements.txt`: `foo==1.0.0`
- Commit and push
- Railway reinstalls and redeploys

**Scenario 3: App runs but is slow**
- Check Railway logs to see response times
- Reduce DPI in `app.py` (use 150 instead of 300)
- Or upgrade Railway plan ($5 β†’ $7/mo)

---

## Testing Your Live API

Once deployed to Railway, test it:

```bash
# Replace with your actual Railway URL
URL="https://hvac-analyzer-prod-abc123.railway.app"

# Health check
curl $URL/

# Upload a test PDF
curl -X POST $URL/analyze \
  -F "file=@/path/to/test_blueprint.pdf"
```

---

## Next Steps

1. βœ… Refactor `analyze_blueprint.py` β†’ `analyze_pdf()`
2. βœ… Test locally with FastAPI
3. βœ… Push to GitHub
4. βœ… Deploy on Railway
5. ⬜ Build a frontend (HTML/Canvas) that talks to the API
6. ⬜ Deploy frontend to Netlify
7. ⬜ Wire them together

Want help with the frontend next?