Skip to content

Commit

Permalink
feat: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
freddysongg committed Jan 14, 2025
1 parent 0c2e5ae commit 1d6f0e7
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
Binary file added .DS_Store
Binary file not shown.
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CaféCast ☕📊

**CaféCast** is an AI-powered sales forecasting and product analysis application. This project focuses on testing and enhancing my knowledge of machine learning models, hyperparameter fine-tuning, and model interpretability. It serves as a learning platform to explore advanced techniques, refine my skills, and deepen my understanding of how to apply machine learning to complex real-world problems. The main goal is to practice and improve while exploring the capabilities of various machine learning models and methodologies.
**CaféCast** is a data-focused platform designed to help users explore and analyze datasets using AI-driven insights. This project currently focuses on manual data cleaning, shaping, and model training workflows, with plans to evolve into a fully interactive and user-friendly platform.

---

Expand All @@ -10,17 +10,15 @@
- Supports **TensorFlow (CPU)** for macOS users to ensure compatibility and efficient local execution.
- Utilizes **PyTorch (CUDA)** for Windows users with GPU support for accelerated training and predictions.
- Automatic detection of the operating system to ensure the correct model implementation is selected.
- **Broad Forecasting Capabilities:**
- Handles **multiple concurrent predictions**, enabling comprehensive sales and demand insights.
- Adaptable to a wide range of café datasets and forecasting requirements.
- **Sales Forecasting Models:**
- **Data Cleaning and Shaping:**
- Manual workflows for preparing datasets for analysis.
- **Model Training:**
- Advanced LSTM-based models with Bayesian optimization and iterative tuning for precise forecasts.
- Time Series Transformers for capturing complex temporal dependencies.
- Classical ARIMA modeling for interpretable, short-term linear trend forecasts.
- **Enhanced Model Flexibility:**
- Configurable hyperparameters to suit the unique characteristics of each dataset.
- Dynamic support for both platform-optimized and manually fine-tuned workflows.
- **Explainable AI:** SHAP values and attention mechanisms provide transparency into model decisions.
- **Data Visualization:** Visual insights into sales trends, seasonality, and demand patterns.

---
Expand Down Expand Up @@ -83,6 +81,40 @@ This project is a testament to continuous learning and experimentation in the fi

---

## Future Expansions 🚀

CafeCast is set to evolve into a more interactive and user-friendly platform with the following key features:

### 1. **User-Focused Frontend Deployment**

- A dedicated web-based interface for seamless interaction.
- Users can upload datasets directly through the frontend, removing the need for manual backend processes.
- A clean and intuitive design that prioritizes simplicity and accessibility.
- Technologies considered include **React**, **Next.js**, or other modern web frameworks for dynamic and responsive user experiences.

### 2. **AI-Driven Q&A Functionality**

- Users will be able to ask questions about their uploaded data through the frontend.
- The platform will leverage pre-trained models saved on the backend to provide accurate and meaningful answers.
- Natural language processing (NLP) capabilities will interpret user queries and deliver real-time responses.
- Support for follow-up queries to explore datasets more deeply.

### 3. **Automated Data Cleaning and Preprocessing**

- Automate the current manual data cleaning workflow.
- The platform will:
- Validate datasets upon upload.
- Detect and resolve missing values, outliers, and inconsistencies automatically.
- Present a preview of cleaned data for user confirmation.

### 4. **Model Training and Customization**

- Allow users to train or fine-tune models directly through the platform using their uploaded datasets.
- Include visualization tools for monitoring training progress, accuracy, and performance metrics.
- Ensure that users can easily customize their models to suit specific use cases.

---

## Tech Stack 🛠️

- **Programming Language:** Python
Expand Down
98 changes: 98 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import numpy as np
import joblib
import tensorflow as tf
import torch

app = FastAPI()

# Paths for models and scaler
SCALER_PATH = "models/scaler.pkl"
LSTM_MODEL_PATH = "models/scaler_lstm.pkl"
LSTM_BAYESIAN_MODEL_PATH = "models/scaler_lstm_bayesian.pkl"
TRANSFORMER_MODEL_PATH = "models/scaler_ts_transformer.pkl"

# Globals for models and scaler
scaler = None
lstm_model = None
lstm_bayesian_model = None
transformer_model = None

@app.on_event("startup")
def load_models_and_scaler():
global scaler, lstm_model, lstm_bayesian_model, transformer_model

# Load scaler
try:
with open(SCALER_PATH, "rb") as f:
scaler = joblib.load(f)
print("Scaler loaded successfully.")
except FileNotFoundError:
raise HTTPException(status_code=500, detail="Scaler file not found.")

# Load LSTM model
try:
with open(LSTM_MODEL_PATH, "rb") as f:
lstm_model = joblib.load(f)
print("LSTM model loaded successfully.")
except Exception as e:
print(f"Error loading LSTM model: {e}")

# Load Bayesian LSTM model
try:
with open(LSTM_BAYESIAN_MODEL_PATH, "rb") as f:
lstm_bayesian_model = joblib.load(f)
print("Bayesian LSTM model loaded successfully.")
except Exception as e:
print(f"Error loading Bayesian LSTM model: {e}")

# Load Transformer model
try:
with open(TRANSFORMER_MODEL_PATH, "rb") as f:
transformer_model = torch.load(f, map_location="cpu")
print(transformer_model.state_dict().keys())
transformer_model.eval()
print("Transformer model loaded successfully.")
except Exception as e:
print(f"Error loading Transformer model: {e}")

# Request model
class PredictionRequest(BaseModel):
model: str # 'lstm', 'lstm_bayesian', 'transformer'
input_values: list # Raw input values

@app.post("/predict")
def predict(data: PredictionRequest):
if scaler is None:
raise HTTPException(status_code=500, detail="Scaler not loaded.")

# Preprocess input
try:
input_array = np.array(data.input_values).reshape(1, -1)
scaled_input = scaler.transform(input_array)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Invalid input: {e}")

# Predict based on model
if data.model == "lstm":
if lstm_model:
prediction = lstm_model.predict(scaled_input)
else:
raise HTTPException(status_code=500, detail="LSTM model not loaded.")
elif data.model == "lstm_bayesian":
if lstm_bayesian_model:
prediction = lstm_bayesian_model.predict(scaled_input)
else:
raise HTTPException(status_code=500, detail="Bayesian LSTM model not loaded.")
elif data.model == "transformer":
if transformer_model:
with torch.no_grad():
tensor_input = torch.tensor(scaled_input, dtype=torch.float32)
prediction = transformer_model(tensor_input).numpy()
else:
raise HTTPException(status_code=500, detail="Transformer model not loaded.")
else:
raise HTTPException(status_code=400, detail="Invalid model type.")

return {"model": data.model, "prediction": prediction.tolist()}
Binary file added output/c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1d6f0e7

Please sign in to comment.