Spaces:
Sleeping
Sleeping
Commit
·
59edf2a
0
Parent(s):
Task6: FastAPI + Streamlit deployment
Browse files- app.py +23 -0
- dockerfile +20 -0
- requirements.txt +7 -0
- streamlit_app.py +18 -0
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load your trained model
|
| 7 |
+
model = joblib.load("model.joblib")
|
| 8 |
+
|
| 9 |
+
# Initialize FastAPI app
|
| 10 |
+
app = FastAPI(title="Supermarket Sales Forecast API")
|
| 11 |
+
|
| 12 |
+
# Define input schema (replace with actual model features)
|
| 13 |
+
class SalesInput(BaseModel):
|
| 14 |
+
feature1: float
|
| 15 |
+
feature2: float
|
| 16 |
+
feature3: float
|
| 17 |
+
|
| 18 |
+
@app.post("/predict")
|
| 19 |
+
def predict(data: SalesInput):
|
| 20 |
+
# Convert input to array for prediction
|
| 21 |
+
input_data = np.array([[data.feature1, data.feature2, data.feature3]])
|
| 22 |
+
prediction = model.predict(input_data)
|
| 23 |
+
return {"prediction": prediction.tolist()}
|
dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python base image
|
| 2 |
+
FROM python:3.12-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy project files
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
COPY app.py .
|
| 10 |
+
COPY model.joblib .
|
| 11 |
+
COPY streamlit_app.py .
|
| 12 |
+
|
| 13 |
+
# Install dependencies
|
| 14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
# Expose FastAPI port
|
| 17 |
+
EXPOSE 8000
|
| 18 |
+
|
| 19 |
+
# Command to run FastAPI
|
| 20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
pydantic
|
| 4 |
+
joblib
|
| 5 |
+
numpy
|
| 6 |
+
requests
|
| 7 |
+
streamlit
|
streamlit_app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
st.title("Supermarket Sales Forecast")
|
| 5 |
+
|
| 6 |
+
# Input sliders or number inputs
|
| 7 |
+
feature1 = st.number_input("Feature 1")
|
| 8 |
+
feature2 = st.number_input("Feature 2")
|
| 9 |
+
feature3 = st.number_input("Feature 3")
|
| 10 |
+
|
| 11 |
+
if st.button("Predict"):
|
| 12 |
+
data = {
|
| 13 |
+
"feature1": feature1,
|
| 14 |
+
"feature2": feature2,
|
| 15 |
+
"feature3": feature3
|
| 16 |
+
}
|
| 17 |
+
response = requests.post("https://YOUR_DEPLOYED_API_URL/predict", json=data)
|
| 18 |
+
st.write("Prediction:", response.json()["prediction"])
|