Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
# NX AI

> Production ready Python FastAPI/Postgres app for [NX](https://goldlabel.pro?s=nx-ai) AI services and more
> FastAPI/Python/Postgres/tsvector. Production ready Python FastAPI/Postgres app for [NX](https://goldlabel.pro?s=nx-ai) AI services and more

## Install & use

```bash
# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Start the development server
uvicorn app.main:app --reload
```

```sh
uvicorn app.main:app
pytest
```

[localhost](http://localhost:8000)
[Public RESTful API](https://nx-ai.onrender.com)
The API is at <http://localhost:8000>.

[localhost](http://localhost:8000) | [Public RESTful API](https://nx-ai.onrender.com)

- **Python 3.11+**
- **Postgres**
- **tsvector** - Superfast search
- **FastAPI** — RESTful API framework
- **Uvicorn** — ASGI server
- **Pytest** — testing framework
- **HTTPX / TestClient** — HTTP testing
- **HTTPX / TestClient**

## Docs

FastAPI automatically generates interactive documentation:

Expand All @@ -38,21 +54,6 @@ tests/
requirements.txt
```

## Install

```bash
# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Start the development server
uvicorn app.main:app --reload
```

The API will be available at <http://localhost:8000>.

## Endpoints

Expand All @@ -62,13 +63,4 @@ The API will be available at <http://localhost:8000>.
| GET | `/health` | Health check — returns `ok` |
| POST | `/echo` | Echoes the JSON `message` field |

### Example — Echo

```bash
curl -X POST http://localhost:8000/echo \
-H "Content-Type: application/json" \
-d '{"message": "hello"}'
# {"echo":"hello"}
```


6 changes: 3 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""NX AI - FastAPI"""
"""NX AI - FastAPI/Python/Postgres/tsvector"""

# Version tracking
__version__ = "1.0.3"
# Current Version
__version__ = "1.0.4"
56 changes: 56 additions & 0 deletions app/api/import_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from fastapi import APIRouter, UploadFile, File, HTTPException

import csv
import psycopg2
import os
import io
from dotenv import load_dotenv

router = APIRouter()

@router.post("/import-csv")
def import_csv(file: UploadFile = File(...)):
"""Import products from a CSV file into the database."""
load_dotenv()
conn = psycopg2.connect(
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT', '5432'),
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD')
)
cur = conn.cursor()
try:
reader = csv.DictReader(io.TextIOWrapper(file.file, encoding='utf-8'))
batch = []
batch_size = 100
inserted = 0
for row in reader:
batch.append((
row.get('name'),
row.get('description'),
float(row.get('price', 0)),
int(row.get('in_stock', 0)),
))
if len(batch) >= batch_size:
cur.executemany(
"INSERT INTO product (name, description, price, in_stock) VALUES (%s, %s, %s, %s)",
batch
)
conn.commit()
inserted += len(batch)
batch.clear()
if batch:
cur.executemany(
"INSERT INTO product (name, description, price, in_stock) VALUES (%s, %s, %s, %s)",
batch
)
conn.commit()
inserted += len(batch)
return {"inserted": inserted}
except Exception as e:
conn.rollback()
raise HTTPException(status_code=500, detail=str(e))
finally:
cur.close()
conn.close()
2 changes: 1 addition & 1 deletion app/api/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ def root() -> dict:
"time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
"epoch": epoch,
"severity": "success",
"message": f"NX AI says hello. Returned {len(products)} products."
"message": f"NX AI says hello & returned {len(products)} products"
}
return {"meta": meta, "data": products}
2 changes: 2 additions & 0 deletions app/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
from app.api.root import router as root_router
from app.api.health import router as health_router
from app.api.echo import router as echo_router
from app.api.import_csv import router as import_csv_router

router.include_router(root_router)
router.include_router(health_router)
router.include_router(echo_router)
router.include_router(import_csv_router)

Loading