Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +66 -127
- best_heartbeatsound_classification.h5 +3 -0
- requirements.txt +3 -8
app.py
CHANGED
|
@@ -1,143 +1,82 @@
|
|
| 1 |
-
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import yfinance as yf
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
from tensorflow.keras.models import load_model
|
| 7 |
-
from joblib import load
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
|
| 16 |
-
# Function to get the last row of stock data
|
| 17 |
-
def get_last_stock_data(ticker):
|
| 18 |
-
try:
|
| 19 |
-
start_date = '2010-01-01'
|
| 20 |
-
end_date = datetime.now().strftime('%Y-%m-%d')
|
| 21 |
-
data = yf.download(ticker, start=start_date, end=end_date)
|
| 22 |
-
last_row = data.iloc[-1]
|
| 23 |
-
return last_row.to_dict()
|
| 24 |
-
except Exception as e:
|
| 25 |
-
return str(e)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def predict_stock_price(ticker, open_price, close_price):
|
| 29 |
-
try:
|
| 30 |
-
start_date = '2010-01-01'
|
| 31 |
-
end_date = datetime.now().strftime('%Y-%m-%d')
|
| 32 |
-
data = yf.download(ticker, start=start_date, end=end_date)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
scaled_data = scaler.transform(dataset)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# LSTM Predictions
|
| 53 |
-
lstm_predictions = lstm_model.predict(x_test_lstm)
|
| 54 |
-
lstm_predictions = scaler.inverse_transform(lstm_predictions)
|
| 55 |
-
next_day_lstm_price = lstm_predictions[-1][0]
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return result
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return str(e)
|
| 62 |
-
|
| 63 |
-
# Function to predict next month's price
|
| 64 |
-
def predict_next_month_price(ticker, close_price):
|
| 65 |
-
try:
|
| 66 |
-
start_date = '2010-01-01'
|
| 67 |
-
end_date = datetime.now().strftime('%Y-%m-%d')
|
| 68 |
-
data = yf.download(ticker, start=start_date, end=end_date)
|
| 69 |
-
|
| 70 |
-
# Prepare the data
|
| 71 |
-
data = data[['Close']]
|
| 72 |
-
dataset = data.values
|
| 73 |
-
scaled_data = scaler.transform(dataset)
|
| 74 |
-
|
| 75 |
-
# Append the user inputs as the last row in the data
|
| 76 |
-
user_input = np.array([[close_price]])
|
| 77 |
-
user_input_scaled = scaler.transform(user_input)
|
| 78 |
-
scaled_data = np.vstack([scaled_data, user_input_scaled])
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
for i in range(60, len(scaled_data)):
|
| 83 |
-
x_test_lstm.append(scaled_data[i-60:i])
|
| 84 |
|
| 85 |
-
|
| 86 |
-
x_test_lstm = np.reshape(x_test_lstm, (x_test_lstm.shape[0], x_test_lstm.shape[1], 1))
|
| 87 |
|
| 88 |
-
# Predicting the next 30 days
|
| 89 |
-
predictions = []
|
| 90 |
-
for _ in range(30):
|
| 91 |
-
pred = lstm_model.predict(x_test_lstm[-1].reshape(1, 60, 1))
|
| 92 |
-
predictions.append(pred[0])
|
| 93 |
-
new_input = np.append(x_test_lstm[-1][1:], pred)
|
| 94 |
-
x_test_lstm = np.append(x_test_lstm, new_input.reshape(1, 60, 1), axis=0)
|
| 95 |
|
| 96 |
-
predictions = np.array(predictions)
|
| 97 |
-
next_month_predictions = scaler.inverse_transform(predictions)
|
| 98 |
-
next_month_price = next_month_predictions[-1][0]
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# Function to display historical data
|
| 107 |
-
def display_historical_data(ticker):
|
| 108 |
-
try:
|
| 109 |
-
start_date = '2010-01-01'
|
| 110 |
-
end_date = datetime.now().strftime('%Y-%m-%d')
|
| 111 |
-
data = yf.download(ticker, start=start_date, end=end_date)
|
| 112 |
-
return data.tail(30)
|
| 113 |
-
except Exception as e:
|
| 114 |
-
return str(e)
|
| 115 |
-
|
| 116 |
-
# Set up Gradio interface
|
| 117 |
-
with gr.Blocks() as app:
|
| 118 |
-
with gr.Tab("Predict Today's Price"):
|
| 119 |
-
gr.Markdown("## Predict Today's Price")
|
| 120 |
-
ticker_input = gr.Dropdown(choices=stock_list, label="Stock Ticker")
|
| 121 |
-
open_price = gr.Number(label="Open")
|
| 122 |
-
close_price = gr.Number(label="Close")
|
| 123 |
-
predict_button = gr.Button("Predict")
|
| 124 |
-
predict_output = gr.Textbox()
|
| 125 |
-
predict_button.click(predict_stock_price, inputs=[ticker_input, open_price, close_price], outputs=predict_output)
|
| 126 |
-
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## libraries for data preprocessing
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
## libraries for training dl models
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
from tensorflow import keras
|
| 8 |
|
| 9 |
+
## libraries for reading audio files
|
| 10 |
+
import librosa as lib
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
|
| 16 |
+
## lets load the model
|
| 17 |
+
model = keras.models.load_model('best_heartbeatsound_classification.h5')
|
|
|
|
| 18 |
|
| 19 |
+
def loading_sound_file(sound_file, sr=22050, duration=10):
|
| 20 |
+
input_length = sr * duration
|
| 21 |
+
X, sr = lib.load(sound_file, sr=sr, duration=duration)
|
| 22 |
+
dur = lib.get_duration(y=X, sr=sr)
|
| 23 |
+
|
| 24 |
+
# # pad audio file same duration
|
| 25 |
+
# if (round(dur) < duration):
|
| 26 |
+
# print ("fixing audio lenght :", file_name)
|
| 27 |
+
# y = lib.util.fix_length(X, input_length)
|
| 28 |
+
# extract normalized mfcc feature from data
|
| 29 |
+
|
| 30 |
+
# ## pad audio to same duration
|
| 31 |
+
# if round(dur) < duration:
|
| 32 |
+
# X = lib.util.fix_length(X, input_length)
|
| 33 |
|
| 34 |
+
# Pad or truncate audio file to the same duration
|
| 35 |
+
if round(dur) < duration:
|
| 36 |
+
pad_amount = input_length - len(X)
|
| 37 |
+
X = np.pad(X, (0, pad_amount), mode='constant')
|
| 38 |
+
elif round(dur) > duration:
|
| 39 |
+
X = X[:input_length]
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
mfccs = np.mean(lib.feature.mfcc(y=X, sr=sr, n_mfcc=25).T,axis=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
## Reshape to match the model's input shape
|
| 45 |
+
data = np.array(mfccs).reshape(1, -1, 1)
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
return data
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
def heart_signal_classification(data):
|
| 52 |
+
X = loading_sound_file(data)
|
| 53 |
+
pred = model.predict(X)
|
| 54 |
+
## Define the threshold
|
| 55 |
+
threshold = 0.6
|
| 56 |
+
max_prob = np.max(pred)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
## Create labels
|
| 59 |
+
labels = {
|
| 60 |
+
0: 'artifact',
|
| 61 |
+
1: 'unlabel',
|
| 62 |
+
2: 'extrastole',
|
| 63 |
+
3: 'extrahls',
|
| 64 |
+
4: 'normal',
|
| 65 |
+
5: 'murmur'
|
| 66 |
+
}
|
| 67 |
|
| 68 |
+
if max_prob < threshold:
|
| 69 |
+
label = 'unknown'
|
| 70 |
+
else:
|
| 71 |
+
result = pred[0].argmax()
|
| 72 |
+
label = labels[result]
|
| 73 |
+
|
| 74 |
+
return label
|
| 75 |
+
################### Gradio Web APP ################################
|
| 76 |
+
title = "Heart Signal Classification App"
|
| 77 |
+
Input = gr.Audio(sources=["upload"], type="filepath")
|
| 78 |
+
Output1 = gr.Textbox(label="Type Of Heart Signal")
|
| 79 |
+
description = "Type Of Signal: Artifact, Murmur, Normal, Extrastole, Extrahls"
|
| 80 |
+
iface = gr.Interface(fn=heart_signal_classification, inputs=Input, outputs=Output1, title=title, description=description)
|
| 81 |
+
|
| 82 |
+
iface.launch(inline=False)
|
best_heartbeatsound_classification.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b68fb24481fdecdc6c5594acf39fbdd79c0fe53b0d799d97239cfd7937c6be31
|
| 3 |
+
size 1150752
|
requirements.txt
CHANGED
|
@@ -1,9 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
gradio
|
| 3 |
-
yfinance
|
| 4 |
-
tensorflow
|
| 5 |
-
joblib
|
| 6 |
-
matplotlib
|
| 7 |
-
numpy
|
| 8 |
pandas
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
pandas
|
| 2 |
+
numpy
|
| 3 |
+
tensorflow
|
| 4 |
+
librosa
|