# --- Part 4.1: Project Structure ---
p41 = """# Project Structure (Create these files manually)

PRAC_4/
│── app/
│   ├── app.py
│   ├── train_model.py
│   └── model.pkl   # (generated after training)
│
│── requirements.txt
│── Dockerfile
│── README.md
"""

# --- Part 4.2: requirements.txt ---
p42 = """# File: requirements.txt

fastapi
uvicorn
scikit-learn
numpy
"""

# --- Part 4.3: app/app.py ---
p43 = """# File: app/app.py

from fastapi import FastAPI
import pickle
import os

app = FastAPI()

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "model.pkl")

try:
    with open(MODEL_PATH, "rb") as f:
        model = pickle.load(f)
except Exception as e:
    print("MODEL LOAD ERROR:", e)
    raise

@app.get("/")
def home():
    return {"message": "ML Model API is running"}

@app.post("/predict")
def predict(features: list[float]):
    prediction = model.predict([features])
    return {"prediction": prediction.tolist()}
"""

# --- Part 4.4: app/train_model.py ---
p44 = """# File: app/train_model.py

from sklearn.linear_model import LinearRegression
import numpy as np
import pickle
import os

# Dummy training data
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])

model = LinearRegression()
model.fit(X, y)

# Directory of this file
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "model.pkl")

# Save model inside app folder
with open(MODEL_PATH, "wb") as f:
    pickle.dump(model, f)

print(f"Model trained and saved at {MODEL_PATH}")
"""

# --- Part 4.5: Dockerfile ---
p45 = """# File: Dockerfile

FROM python:3.10

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

CMD ["uvicorn", "app.app:app", "--host", "0.0.0.0", "--port", "5000"]
"""

# --- Part 4.6: README.md ---
p46 = """# File: README.md

# MLOps Project – Practical 4

This project demonstrates model deployment using FastAPI and Docker.

## How to run

1. Train model:
python app/train_model.py

2. Run locally:
uvicorn app.app:app --reload

3. Build Docker image:
docker build -t ml-model .

4. Run container:
docker run -p 5000:5000 ml-model
"""