Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app(2).py +73 -0
- requirements(2).txt +6 -0
- stock.pkl +3 -0
app(2).py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""stockpriceprediction_RNN.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1WNG8vH1hyyxmR3_BEtT9-c0Golei-f4d
|
8 |
+
"""
|
9 |
+
|
10 |
+
import numpy as np
|
11 |
+
import tensorflow as tf
|
12 |
+
from tensorflow.keras.models import Sequential
|
13 |
+
from tensorflow.keras.layers import SimpleRNN, Dense
|
14 |
+
import gradio as gr
|
15 |
+
|
16 |
+
# 1. Generate dummy data
|
17 |
+
def generate_dummy_data():
|
18 |
+
x = np.linspace(0, 100, 500)
|
19 |
+
y = np.sin(x / 5) + np.random.normal(scale=0.1, size=len(x))
|
20 |
+
return y
|
21 |
+
|
22 |
+
data = generate_dummy_data()
|
23 |
+
|
24 |
+
# 2. Prepare dataset (with time_steps = 5)
|
25 |
+
def create_dataset(data, time_steps=5): # π changed from 10 to 5
|
26 |
+
X, y = [], []
|
27 |
+
for i in range(len(data) - time_steps):
|
28 |
+
X.append(data[i:i + time_steps])
|
29 |
+
y.append(data[i + time_steps])
|
30 |
+
return np.array(X), np.array(y)
|
31 |
+
|
32 |
+
X, y = create_dataset(data)
|
33 |
+
X = X.reshape((X.shape[0], X.shape[1], 1))
|
34 |
+
|
35 |
+
# 3. Build model for input shape (5, 1)
|
36 |
+
model = Sequential([
|
37 |
+
SimpleRNN(50, activation='relu', input_shape=(5, 1)), # π changed from (10, 1) to (5, 1)
|
38 |
+
Dense(1)
|
39 |
+
])
|
40 |
+
model.compile(optimizer='adam', loss='mse')
|
41 |
+
model.fit(X, y, epochs=10, verbose=0)
|
42 |
+
|
43 |
+
import pickle
|
44 |
+
|
45 |
+
with open('stock.pkl', 'wb') as file:
|
46 |
+
pickle.dump(model,file)
|
47 |
+
|
48 |
+
with open('stock.pkl', 'rb') as f:
|
49 |
+
loaded_model = pickle.load(f)
|
50 |
+
|
51 |
+
# 4. Predict function
|
52 |
+
def predict_next_price(seq):
|
53 |
+
try:
|
54 |
+
seq = [float(i.strip()) for i in seq.split(',')]
|
55 |
+
if len(seq) != 5: # π only 5 numbers expected now
|
56 |
+
return "Please enter exactly 5 numbers."
|
57 |
+
|
58 |
+
input_seq = np.array(seq).reshape((1, 5, 1)) # π reshape accordingly
|
59 |
+
pred = model.predict(input_seq)
|
60 |
+
return f"π Predicted next price: {pred[0][0]:.4f}"
|
61 |
+
except Exception as e:
|
62 |
+
return f"Error: {str(e)}"
|
63 |
+
|
64 |
+
# 5. Gradio UI
|
65 |
+
iface = gr.Interface(
|
66 |
+
fn=predict_next_price,
|
67 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter 5 stock prices, comma-separated"),
|
68 |
+
outputs="text",
|
69 |
+
title="π Stock Price Predictor (RNN)",
|
70 |
+
description="Enter 5 stock prices to predict the next one."
|
71 |
+
)
|
72 |
+
|
73 |
+
iface.launch()
|
requirements(2).txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
pandas
|
3 |
+
scikit-learn
|
4 |
+
numpy
|
5 |
+
matplotlib
|
6 |
+
tensorflow
|
stock.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1cad596d25aaafe0ca1919dcd14ea30cd1cc00e8d80193c3f34720999832de91
|
3 |
+
size 56048
|