Spaces:
Configuration error
Configuration error
Upload 3 files
Browse files- README_Synthsis.md +32 -0
- app_Synthsis.py +64 -0
- requirements_Synthsis.txt +7 -0
README_Synthsis.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
readme_content = """---
|
| 2 |
+
title: DataSynthis ML JobTask
|
| 3 |
+
colorFrom: blue
|
| 4 |
+
colorTo: green
|
| 5 |
+
sdk: gradio
|
| 6 |
+
sdk_version: 4.7.1
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Stock Price Forecasting
|
| 12 |
+
|
| 13 |
+
DataSynthis ML Engineer Intern Job Task SET-B
|
| 14 |
+
|
| 15 |
+
## Overview
|
| 16 |
+
Time series forecasting for stock prices using ARIMA and LSTM models.
|
| 17 |
+
|
| 18 |
+
## Models
|
| 19 |
+
- ARIMA: Statistical approach
|
| 20 |
+
- LSTM: Deep learning approach
|
| 21 |
+
|
| 22 |
+
## Dataset
|
| 23 |
+
Google (GOOGL) stock prices from 2018-2025 via Yahoo Finance
|
| 24 |
+
|
| 25 |
+
## Performance
|
| 26 |
+
ARIMA achieved RMSE of $3.48 vs LSTM's $30.88
|
| 27 |
+
|
| 28 |
+
## Usage
|
| 29 |
+
Enter stock ticker and select forecast horizon to generate predictions.
|
| 30 |
+
|
| 31 |
+
with open('README.md', 'w') as f:
|
| 32 |
+
f.write(readme_content)
|
app_Synthsis.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
import joblib
|
| 6 |
+
import yfinance as yf
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
+
import warnings
|
| 9 |
+
warnings.filterwarnings('ignore')
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
lstm_model = load_model('lstm_googl_stock_model.h5')
|
| 13 |
+
scaler = joblib.load('scaler.pkl')
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Error: {e}")
|
| 16 |
+
|
| 17 |
+
def predict_stock_price(ticker, days_ahead=30):
|
| 18 |
+
try:
|
| 19 |
+
end_date = datetime.now()
|
| 20 |
+
start_date = end_date - timedelta(days=365)
|
| 21 |
+
df = yf.download(ticker, start=start_date, end=end_date, progress=False)
|
| 22 |
+
|
| 23 |
+
if df.empty or len(df) < 60:
|
| 24 |
+
return "Error: Insufficient data", None
|
| 25 |
+
|
| 26 |
+
data = df[['Close']].values
|
| 27 |
+
scaled_data = scaler.transform(data)
|
| 28 |
+
lookback = 60
|
| 29 |
+
predictions = []
|
| 30 |
+
current_sequence = scaled_data[-lookback:].reshape(lookback, 1)
|
| 31 |
+
|
| 32 |
+
for _ in range(days_ahead):
|
| 33 |
+
pred_scaled = lstm_model.predict(current_sequence.reshape(1, lookback, 1), verbose=0)
|
| 34 |
+
predictions.append(pred_scaled[0, 0])
|
| 35 |
+
current_sequence = np.append(current_sequence[1:], pred_scaled).reshape(lookback, 1)
|
| 36 |
+
|
| 37 |
+
predictions_array = np.array(predictions).reshape(-1, 1)
|
| 38 |
+
predictions_actual = scaler.inverse_transform(predictions_array)
|
| 39 |
+
future_dates = pd.date_range(start=end_date + timedelta(days=1), periods=days_ahead)
|
| 40 |
+
results = pd.DataFrame({'Date': future_dates.strftime('%Y-%m-%d'), 'Predicted Price': predictions_actual.flatten().round(2)})
|
| 41 |
+
|
| 42 |
+
last_price = data[-1][0]
|
| 43 |
+
avg_prediction = predictions_actual.mean()
|
| 44 |
+
change_pct = ((avg_prediction - last_price) / last_price * 100)
|
| 45 |
+
|
| 46 |
+
summary = f"Stock: {ticker}\nCurrent: ${last_price:.2f}\nPredicted: ${avg_prediction:.2f}\nChange: {change_pct:+.2f}%"
|
| 47 |
+
return summary, results
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error: {e}", None
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("# Stock Price Forecasting")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
ticker_input = gr.Textbox(label="Stock Ticker", value="GOOGL")
|
| 56 |
+
days_slider = gr.Slider(1, 90, 30, label="Days")
|
| 57 |
+
predict_btn = gr.Button("Predict")
|
| 58 |
+
with gr.Column():
|
| 59 |
+
summary_output = gr.Textbox(label="Summary", lines=5)
|
| 60 |
+
table_output = gr.Dataframe(label="Predictions")
|
| 61 |
+
predict_btn.click(predict_stock_price, [ticker_input, days_slider], [summary_output, table_output])
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|
requirements_Synthsis.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow==2.15.0
|
| 2 |
+
numpy==1.24.3
|
| 3 |
+
pandas==2.0.3
|
| 4 |
+
scikit-learn==1.3.0
|
| 5 |
+
yfinance==0.2.38
|
| 6 |
+
joblib==1.3.2
|
| 7 |
+
gradio==4.7.1
|