Sukumar2005's picture
Update app.py
e324a3c verified
# πŸ”½ STEP 1: Install Required Packages
!pip install tensorflow huggingface_hub
# πŸ”½ STEP 2: Import Libraries
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
from tensorflow.keras.optimizers import Adam
from huggingface_hub import notebook_login, HfApi, create_repo
# πŸ”½ STEP 3: Prepare Dataset
def generate_sequence():
X, y = [], []
for i in range(1, 100):
X.append([i, i+1, i+2])
y.append(i+3)
X = np.array(X).reshape(-1, 3, 1)
y = np.array(y)
return X, y
X, y = generate_sequence()
# πŸ”½ STEP 4: Build the RNN Model
model = Sequential([
SimpleRNN(32, activation='relu', input_shape=(3, 1)),
Dense(1)
])
model.compile(optimizer=Adam(), loss='mse')
model.summary()
# πŸ”½ STEP 5: Train the Model
model.fit(X, y, epochs=200, verbose=0)
# πŸ”½ STEP 6: Save the Model
model.save("rnn_next_number.h5")
# πŸ”½ STEP 7: Login to Hugging Face
notebook_login() # Paste your token when asked
# πŸ”½ STEP 8: Create Repo on Hugging Face
repo_id = "your-username/predict-next-number-rnn" # Replace with your actual username
create_repo(name="predict-next-number-rnn", repo_type="model", private=False)
# πŸ”½ STEP 9: Upload Model to Hugging Face
api = HfApi()
api.upload_file(
path_or_fileobj="rnn_next_number.h5",
path_in_repo="rnn_next_number.h5",
repo_id=repo_id,
repo_type="model"
)
# πŸ”’ Predict Next Number - RNN Model
This is a simple RNN model trained using Keras to predict the next number in a sequence.
## Example
Input: [4, 5, 6] β†’ Output: ~7.0
### Usage
```python
from tensorflow.keras.models import load_model
import numpy as np
model = load_model("rnn_next_number.h5")
x_input = np.array([4, 5, 6]).reshape(1, 3, 1)
prediction = model.predict(x_input)
print(f"Predicted next number: {prediction[0][0]:.2f}")