Ade1ola commited on
Commit
8532693
·
verified ·
1 Parent(s): 83d915d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -65
app.py CHANGED
@@ -1,79 +1,93 @@
1
  import gradio as gr
2
- import numpy as np
3
- import pandas as pd
4
- import tensorflow as tf
5
- import joblib
6
- from tensorflow.keras.models import load_model
7
 
8
- # Load the trained LSTM model
9
- model = load_model("lstm_gru_model5.h5")
10
 
11
- # Load the MinMaxScaler
12
- scaler = joblib.load("forex_scaler222.pkl")
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- def preprocess_input(data):
15
- """Preprocess input data for LSTM model."""
16
- if len(data) < 60:
17
- mean_value = np.mean(data) # Compute mean of given prices
18
- data = data + [mean_value] * (60 - len(data)) # Fill missing spots
19
 
20
- data = np.array(data).reshape(1, -1) # Reshape for MinMaxScaler
21
- scaled_data = scaler.transform(data) # Scale the input
22
- return scaled_data.reshape(1, 60, 1) # Reshape for LSTM
23
 
 
 
 
 
 
24
 
 
 
 
25
 
26
- def predict_forex(prices):
27
- """Predict the next forex price based on the input sequence."""
28
- try:
29
- input_data = [float(price) for price in prices.split(",")]
 
 
30
 
31
- # Ensure enough input data
32
- if len(input_data) < 60:
33
- return "Please provide at least 60 previous forex prices."
34
 
35
- # Use last 60 prices
36
- preprocessed_data = preprocess_input(input_data[-60:])
37
- prediction = model.predict(preprocessed_data)
38
-
39
- # Convert back to original scale
40
- predicted_price = scaler.inverse_transform(prediction)[0][0]
41
- return f"Predicted Next Price: {predicted_price:.5f}"
42
- except Exception as e:
43
- return f"Error: {str(e)}"
44
-
45
-
46
- def batch_predict(file):
47
- """Batch prediction for CSV files."""
48
- try:
49
- df = pd.read_csv(file)
50
- if "prices" not in df.columns:
51
- return "CSV must have a 'prices' column with historical data."
52
 
53
- df["predictions"] = df["prices"].rolling(window=10).apply(lambda x: predict_forex(",".join(map(str, x))) if len(x) == 10 else None)
54
- return df.dropna()
55
- except Exception as e:
56
- return f"Error: {str(e)}"
57
 
58
- # Gradio UI
59
- demo = gr.Interface(
60
- fn=predict_forex,
61
- inputs=gr.Textbox(label="Enter last 10 forex prices (comma-separated)"),
62
- outputs=gr.Textbox(label="Predicted Next Price"),
63
- title="Forex Price Predictor",
64
- description="Enter the last 10 forex prices to predict the next price. Upload CSV for batch predictions.",
65
- examples=[
66
- ["1.2345,1.2350,1.2360,1.2370,1.2380,1.2390,1.2400,1.2410,1.2420,1.2430"]
67
- ],
68
- allow_flagging="never"
69
- )
70
 
71
- batch_demo = gr.Interface(
72
- fn=batch_predict,
73
- inputs=gr.File(label="Upload CSV"),
74
- outputs=gr.Dataframe(label="Predictions"),
75
- title="Batch Prediction",
76
- description="Upload a CSV with a 'prices' column for batch predictions."
77
- )
78
 
79
- gr.TabbedInterface([demo, batch_demo], ["Single Prediction", "Batch Prediction"]).launch()
 
1
  import gradio as gr
 
 
 
 
 
2
 
3
+ def message():
4
+ return "🚧 Under Construction 🚧\n\nCheck back soon!"
5
 
6
+ demo = gr.Interface(
7
+ fn=message,
8
+ inputs=[],
9
+ outputs=gr.Textbox(label="Status"),
10
+ title="Site Under Construction",
11
+ description="This application is currently under development. Please check back later!"
12
+ )
13
+
14
+ demo.launch()
15
+ # import gradio as gr
16
+ # import numpy as np
17
+ # import pandas as pd
18
+ # import tensorflow as tf
19
+ # import joblib
20
+ # from tensorflow.keras.models import load_model
21
 
22
+ # # Load the trained LSTM model
23
+ # model = load_model("lstm_gru_model5.h5")
 
 
 
24
 
25
+ # # Load the MinMaxScaler
26
+ # scaler = joblib.load("forex_scaler222.pkl")
 
27
 
28
+ # def preprocess_input(data):
29
+ # """Preprocess input data for LSTM model."""
30
+ # if len(data) < 60:
31
+ # mean_value = np.mean(data) # Compute mean of given prices
32
+ # data = data + [mean_value] * (60 - len(data)) # Fill missing spots
33
 
34
+ # data = np.array(data).reshape(1, -1) # Reshape for MinMaxScaler
35
+ # scaled_data = scaler.transform(data) # Scale the input
36
+ # return scaled_data.reshape(1, 60, 1) # Reshape for LSTM
37
 
38
+
39
+
40
+ # def predict_forex(prices):
41
+ # """Predict the next forex price based on the input sequence."""
42
+ # try:
43
+ # input_data = [float(price) for price in prices.split(",")]
44
 
45
+ # # Ensure enough input data
46
+ # if len(input_data) < 60:
47
+ # return "Please provide at least 60 previous forex prices."
48
 
49
+ # # Use last 60 prices
50
+ # preprocessed_data = preprocess_input(input_data[-60:])
51
+ # prediction = model.predict(preprocessed_data)
52
+
53
+ # # Convert back to original scale
54
+ # predicted_price = scaler.inverse_transform(prediction)[0][0]
55
+ # return f"Predicted Next Price: {predicted_price:.5f}"
56
+ # except Exception as e:
57
+ # return f"Error: {str(e)}"
58
+
59
+
60
+ # def batch_predict(file):
61
+ # """Batch prediction for CSV files."""
62
+ # try:
63
+ # df = pd.read_csv(file)
64
+ # if "prices" not in df.columns:
65
+ # return "CSV must have a 'prices' column with historical data."
66
 
67
+ # df["predictions"] = df["prices"].rolling(window=10).apply(lambda x: predict_forex(",".join(map(str, x))) if len(x) == 10 else None)
68
+ # return df.dropna()
69
+ # except Exception as e:
70
+ # return f"Error: {str(e)}"
71
 
72
+ # # Gradio UI
73
+ # demo = gr.Interface(
74
+ # fn=predict_forex,
75
+ # inputs=gr.Textbox(label="Enter last 10 forex prices (comma-separated)"),
76
+ # outputs=gr.Textbox(label="Predicted Next Price"),
77
+ # title="Forex Price Predictor",
78
+ # description="Enter the last 10 forex prices to predict the next price. Upload CSV for batch predictions.",
79
+ # examples=[
80
+ # ["1.2345,1.2350,1.2360,1.2370,1.2380,1.2390,1.2400,1.2410,1.2420,1.2430"]
81
+ # ],
82
+ # allow_flagging="never"
83
+ # )
84
 
85
+ # batch_demo = gr.Interface(
86
+ # fn=batch_predict,
87
+ # inputs=gr.File(label="Upload CSV"),
88
+ # outputs=gr.Dataframe(label="Predictions"),
89
+ # title="Batch Prediction",
90
+ # description="Upload a CSV with a 'prices' column for batch predictions."
91
+ # )
92
 
93
+ # gr.TabbedInterface([demo, batch_demo], ["Single Prediction", "Batch Prediction"]).launch()