How minhaj-mhd/stock-prediction works
Project type and purpose
• This is a full-stack, real-time stock market price prediction system built with Python’s FastAPI.
• It fetches live data from Yahoo Finance, passes it through an LSTM model, and serves both a REST API and an interactive HTML frontend.
File structure
• Top-level files:
• README.md (project description)
• requirements.txt (dependencies)
• train_model.py (offline model training script)
• test/test.py (unit/integration tests)
• app/ directory:
• main.py (FastAPI entry point, defines routes)
• models/models.py (ML model loader, possibly defines how to load LSTM weights)
• schemas.py (Pydantic models for request/response validation)
• services/data_service.py (handles data fetching from Yahoo Finance)
• services/prediction_service.py (wraps the loaded LSTM model to generate forecasts)
• services/templates/index.html (Jinja2 template for the web UI)
README for additional insights
• Features emphasize:
• Real-time data fetching
• LSTM-based prediction
• RESTful endpoints
• Interactive visualization
Components and interactions
a. Main components:
1) Frontend (index.html + JS/CSS for visualization)
2) FastAPI Web Server (app/main.py)
3) Data Service (app/services/data_service.py)
4) Prediction Service (app/services/prediction_service.py + app/models/models.py)
5) Model Training Pipeline (train_model.py)
6) External Service: Yahoo Finance API
7) Consumer / Client (browser and programmatic API clients)
b. Relationships & data flow:
• The browser/client sends HTTP requests to FastAPI endpoints (e.g., GET /predict).
• FastAPI controllers validate input via Pydantic schemas (schemas.py).
• Controllers invoke Data Service to fetch live stock quotes from Yahoo Finance.
• Raw data flows into the Prediction Service, which uses the loaded LSTM model to generate forecasts.
• The result is returned as JSON or rendered via the Jinja2 HTML template.
• Separately, train_model.py loads historical data, trains or retrains the LSTM, and saves model artifacts for app/models/models.py to load at startup.
c. Architectural patterns and principles:
• Layered architecture:
• Controllers / Routes (main.py)
• Business logic / Services (data and prediction services)
• Models / Persistence (model loader, saved artifacts)
• Presentation (HTML templates)
• Separation of concerns: data fetching, prediction logic, API interface are decoupled.
• Use of Pydantic for input/output validation.
d. Key technologies and frameworks:
• FastAPI (web framework)
• Pydantic (data validation)
• Jinja2 (templating)
• LSTM model (TensorFlow or PyTorch)
• YahooFinance (external data source)