Skip to content

Commit fc18009

Browse files
committed
delete commit history
0 parents  commit fc18009

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2933
-0
lines changed

.gitattributes

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
*.7z filter=lfs diff=lfs merge=lfs -text
2+
*.arrow filter=lfs diff=lfs merge=lfs -text
3+
*.bin filter=lfs diff=lfs merge=lfs -text
4+
*.bz2 filter=lfs diff=lfs merge=lfs -text
5+
*.ckpt filter=lfs diff=lfs merge=lfs -text
6+
*.ftz filter=lfs diff=lfs merge=lfs -text
7+
*.gz filter=lfs diff=lfs merge=lfs -text
8+
*.h5 filter=lfs diff=lfs merge=lfs -text
9+
*.joblib filter=lfs diff=lfs merge=lfs -text
10+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
11+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
12+
*.model filter=lfs diff=lfs merge=lfs -text
13+
*.msgpack filter=lfs diff=lfs merge=lfs -text
14+
*.npy filter=lfs diff=lfs merge=lfs -text
15+
*.npz filter=lfs diff=lfs merge=lfs -text
16+
*.onnx filter=lfs diff=lfs merge=lfs -text
17+
*.ot filter=lfs diff=lfs merge=lfs -text
18+
*.parquet filter=lfs diff=lfs merge=lfs -text
19+
*.pb filter=lfs diff=lfs merge=lfs -text
20+
*.pickle filter=lfs diff=lfs merge=lfs -text
21+
*.pkl filter=lfs diff=lfs merge=lfs -text
22+
*.pt filter=lfs diff=lfs merge=lfs -text
23+
*.pth filter=lfs diff=lfs merge=lfs -text
24+
*.rar filter=lfs diff=lfs merge=lfs -text
25+
*.safetensors filter=lfs diff=lfs merge=lfs -text
26+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27+
*.tar.* filter=lfs diff=lfs merge=lfs -text
28+
*.tar filter=lfs diff=lfs merge=lfs -text
29+
*.tflite filter=lfs diff=lfs merge=lfs -text
30+
*.tgz filter=lfs diff=lfs merge=lfs -text
31+
*.wasm filter=lfs diff=lfs merge=lfs -text
32+
*.xz filter=lfs diff=lfs merge=lfs -text
33+
*.zip filter=lfs diff=lfs merge=lfs -text
34+
*.zst filter=lfs diff=lfs merge=lfs -text
35+
*tfevents* filter=lfs diff=lfs merge=lfs -text
36+
*.csv filter=lfs diff=lfs merge=lfs -text
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Sync to Hugging Face hub
2+
on:
3+
push:
4+
branches: [main]
5+
6+
# to run this workflow manually from the Actions tab
7+
workflow_dispatch:
8+
9+
jobs:
10+
sync-to-hub:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
with:
15+
fetch-depth: 0
16+
lfs: true
17+
- name: Push to hub
18+
env:
19+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
20+
run: git push --force https://SwapCodes:[email protected]/spaces/SwapCodes/farmingo main

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.vscode/
2+
**/__pycache__/
3+
.testing/
4+
.env
5+
.env.*

Dockerfile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Hugging Face Spaces-friendly Dockerfile for your FastAPI app
2+
FROM python:3.9-slim
3+
4+
ENV DEBIAN_FRONTEND=noninteractive
5+
6+
# Install small set of system deps needed for wheels, TF and image libs
7+
RUN apt-get update && \
8+
apt-get install -y --no-install-recommends \
9+
build-essential \
10+
wget \
11+
ca-certificates \
12+
libjpeg-dev \
13+
zlib1g-dev \
14+
libgl1 \
15+
libglib2.0-0 && \
16+
rm -rf /var/lib/apt/lists/*
17+
18+
# Create non-root user
19+
RUN useradd -m -u 1000 appuser
20+
21+
WORKDIR /app
22+
23+
# Copy and install Python deps (as root so install can write to system)
24+
COPY requirements.txt /app/requirements.txt
25+
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
26+
pip install --no-cache-dir -r /app/requirements.txt
27+
28+
# Copy application code
29+
COPY . /app
30+
31+
# Ensure the non-root user owns the app files
32+
RUN chown -R appuser:appuser /app
33+
34+
# Run as non-root user
35+
USER appuser
36+
ENV PATH="/home/appuser/.local/bin:$PATH"
37+
38+
# Expose HF Spaces default port
39+
EXPOSE 7860
40+
41+
# Entrypoint: run the FastAPI app located at api/api.py
42+
# Note: uvicorn import path is "api.api:app"
43+
CMD ["uvicorn", "api.api:app", "--host", "0.0.0.0", "--port", "7860", "--proxy-headers"]

README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
## Files
2+
3+
* `api.py` — main FastAPI application. Must define `app = FastAPI()` at module level and expose the inference endpoint.
4+
* `schema.py` — pydantic models used for request validation and response serialization. Update these models to match the inputs/outputs your model expects.
5+
* `requirements.txt` — list of pip dependencies (FastAPI, uvicorn, pydantic, plus any ML libs like `torch`, `tensorflow`, `scikit-learn`).
6+
7+
---
8+
9+
## Design principles
10+
11+
1. **Keep schema simple and explicit**
12+
13+
* `schema.py` currently defines `CropPriceInput` and `CropPriceOutput`. If your model expects different fields, rename or create new pydantic models that reflect the exact inputs and outputs. Example:
14+
15+
```python
16+
from pydantic import BaseModel
17+
18+
class CropPriceInput(BaseModel):
19+
crop: str
20+
region: str
21+
date: str # ISO format
22+
23+
class CropPriceOutput(BaseModel):
24+
crop: str
25+
region: str
26+
date: str
27+
price: float
28+
```
29+
30+
* Pydantic will validate incoming JSON and produce useful 422 errors when the payload is malformed.
31+
32+
2. **Load the model once, at module import time**
33+
34+
* Load or initialize the model in `api.py` at module level so it is reused across requests instead of being loaded per-request. Use environment variables or configuration to point to model files or endpoints.
35+
36+
```python
37+
import os
38+
MODEL_PATH = os.environ.get("MODEL_PATH")
39+
model = load_model(MODEL_PATH) # implement load_model for your model type
40+
```
41+
42+
3. **Keep a small adapter function**
43+
44+
* Implement a single adapter function (already present as `predict_crop_price`) that accepts the validated fields from the pydantic model, transforms them as needed, runs model inference, and returns a primitive (number or dict) that the route can convert into the response model.
45+
46+
```python
47+
def predict_crop_price(crop: str, region: str, date: str = None):
48+
# transform inputs, call model, post-process output
49+
return 123.45 # numeric or convertible to float
50+
```
51+
52+
4. **Endpoint matches schema**
53+
54+
* The endpoint should accept the input model and return the output model using `response_model=...` so responses are validated and documented automatically by FastAPI.
55+
56+
```python
57+
@app.post("/crop_price", response_model=CropPriceOutput)
58+
async def predict(data: CropPriceInput):
59+
price = predict_crop_price(data.crop, data.region, data.date)
60+
return CropPriceOutput(...)
61+
```
62+
63+
64+
---
65+
66+
## Quick start (local development)
67+
68+
1. `(optional)` Create and activate a virtual environment:
69+
70+
```bash
71+
# Step 1: Create a new virtual environment named ".venv"
72+
python -m venv .venv
73+
74+
# Step 2: Activate the virtual environment (Windows PowerShell or Git Bash)
75+
./.venv/Scripts/activate
76+
77+
# Step 3: Export all installed libraries to a requirements file
78+
pip freeze > requirements.txt
79+
80+
```
81+
82+
2. Install dependencies (adjust for your model runtime):
83+
84+
```bash
85+
# Install all required dependencies
86+
pip install -r requirements.txt
87+
```
88+
89+
3. Run the app from the directory with `api.py`:
90+
91+
```bash
92+
uvicorn api:app --reload --port 8000
93+
```
94+
95+
4. Open interactive docs to test inputs and outputs:
96+
97+
* `http://127.0.0.1:8000/docs` (Swagger UI)
98+
* `http://127.0.0.1:8000/redoc` (ReDoc)
99+
100+
---
101+
102+
## Example request (JSON)
103+
104+
Use the input shape defined in `schema.py`. Example based on the current schema:
105+
106+
```json
107+
{
108+
"crop": "wheat",
109+
"region": "kolhapur",
110+
"date": "2025-11-13"
111+
}
112+
```
113+
114+
Example response:
115+
116+
```json
117+
{
118+
"crop": "wheat",
119+
"region": "kolhapur",
120+
"date": "2025-11-13",
121+
"price": 123.45
122+
}
123+
```
124+
125+
---
126+
127+
## Common pitfalls
128+
129+
* **Model reloaded on each request:** Load the model once at module import time.
130+
* **Non-serializable output:** Ensure outputs are primitives (float, int, str) or pydantic models.
131+
* **Missing `app` object:** `uvicorn api:app` requires `app = FastAPI()` in `api.py`.
132+
* **422 Unprocessable Entity:** Request JSON doesn't match the input pydantic model.
133+
* **Port in use:** Run on a different port with `--port`.
134+
135+
---
136+
137+
## For source code of Crop Prediction Model
138+
```
139+
https://github.com/SwapCodesDev/ML-Models---Farmingo
140+
```

0 commit comments

Comments
 (0)