number_sequence / app.py
Harshini16's picture
Upload 2 files
ab8047f verified
Raw
History Blame Contribute Delete
1.51 kB
# -*- coding: utf-8 -*-
"""app.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Fs9_sYM9yLWes2K3kTowoXNMcVqfteCH
"""
import numpy as np
import gradio as gr
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
# Parameters
window_size = 3
# Generate and prepare the sequence data
sequence = np.array([i for i in range(1, 101)])
x = []
y = []
for i in range(len(sequence) - window_size):
x.append(sequence[i:i + window_size])
y.append(sequence[i + window_size])
x = np.array(x)
y = np.array(y)
x = x.reshape(x.shape[0], x.shape[1], 1)
# Build and train the RNN model
model = Sequential()
model.add(SimpleRNN(50, activation='relu', input_shape=(window_size, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(x, y, epochs=500, verbose=0)
# Prediction function for Gradio
def predict_next_number(a, b, c):
input_sequence = np.array([a, b, c]).reshape((1, window_size, 1))
prediction = model.predict(input_sequence, verbose=0)
return float(prediction[0][0])
# Gradio UI
iface = gr.Interface(
fn=predict_next_number,
inputs=[gr.Number(label="Number 1"), gr.Number(label="Number 2"), gr.Number(label="Number 3")],
outputs=gr.Number(label="Predicted Next Number"),
title="RNN Sequence Prediction",
description="Enter 3 consecutive numbers to predict the next number in the sequence."
)
if __name__ == "__main__":
iface.launch()