Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π½ STEP 1: Install Required Packages
|
| 2 |
+
!pip install tensorflow huggingface_hub
|
| 3 |
+
|
| 4 |
+
# π½ STEP 2: Import Libraries
|
| 5 |
+
import numpy as np
|
| 6 |
+
from tensorflow.keras.models import Sequential
|
| 7 |
+
from tensorflow.keras.layers import SimpleRNN, Dense
|
| 8 |
+
from tensorflow.keras.optimizers import Adam
|
| 9 |
+
from huggingface_hub import notebook_login, HfApi, create_repo
|
| 10 |
+
|
| 11 |
+
# π½ STEP 3: Prepare Dataset
|
| 12 |
+
def generate_sequence():
|
| 13 |
+
X, y = [], []
|
| 14 |
+
for i in range(1, 100):
|
| 15 |
+
X.append([i, i+1, i+2])
|
| 16 |
+
y.append(i+3)
|
| 17 |
+
X = np.array(X).reshape(-1, 3, 1)
|
| 18 |
+
y = np.array(y)
|
| 19 |
+
return X, y
|
| 20 |
+
|
| 21 |
+
X, y = generate_sequence()
|
| 22 |
+
|
| 23 |
+
# π½ STEP 4: Build the RNN Model
|
| 24 |
+
model = Sequential([
|
| 25 |
+
SimpleRNN(32, activation='relu', input_shape=(3, 1)),
|
| 26 |
+
Dense(1)
|
| 27 |
+
])
|
| 28 |
+
model.compile(optimizer=Adam(), loss='mse')
|
| 29 |
+
model.summary()
|
| 30 |
+
|
| 31 |
+
# π½ STEP 5: Train the Model
|
| 32 |
+
model.fit(X, y, epochs=200, verbose=0)
|
| 33 |
+
|
| 34 |
+
# π½ STEP 6: Save the Model
|
| 35 |
+
model.save("rnn_next_number.h5")
|
| 36 |
+
|
| 37 |
+
# π½ STEP 7: Login to Hugging Face
|
| 38 |
+
notebook_login() # Paste your token when asked
|
| 39 |
+
|
| 40 |
+
# π½ STEP 8: Create Repo on Hugging Face
|
| 41 |
+
repo_id = "your-username/predict-next-number-rnn" # Replace with your actual username
|
| 42 |
+
create_repo(name="predict-next-number-rnn", repo_type="model", private=False)
|
| 43 |
+
|
| 44 |
+
# π½ STEP 9: Upload Model to Hugging Face
|
| 45 |
+
api = HfApi()
|
| 46 |
+
api.upload_file(
|
| 47 |
+
path_or_fileobj="rnn_next_number.h5",
|
| 48 |
+
path_in_repo="rnn_next_number.h5",
|
| 49 |
+
repo_id=repo_id,
|
| 50 |
+
repo_type="model"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# π½ STEP 10: Create README.md
|
| 54 |
+
readme = """
|
| 55 |
+
# π’ Predict Next Number - RNN Model
|
| 56 |
+
|
| 57 |
+
This is a simple RNN model trained using Keras to predict the next number in a sequence.
|
| 58 |
+
|
| 59 |
+
## Example
|
| 60 |
+
|
| 61 |
+
Input: [4, 5, 6] β Output: ~7.0
|
| 62 |
+
|
| 63 |
+
### Usage
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
from tensorflow.keras.models import load_model
|
| 67 |
+
import numpy as np
|
| 68 |
+
|
| 69 |
+
model = load_model("rnn_next_number.h5")
|
| 70 |
+
x_input = np.array([4, 5, 6]).reshape(1, 3, 1)
|
| 71 |
+
prediction = model.predict(x_input)
|
| 72 |
+
|
| 73 |
+
print(f"Predicted next number: {prediction[0][0]:.2f}")
|