updated the changes
#2
by
himanishprak23
- opened
app.py
CHANGED
@@ -1,7 +1,112 @@
|
|
1 |
-
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import torch
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
trained_tokenizer = GPT2Tokenizer.from_pretrained("Kumarkishalaya/GPT-2-next-word-prediction")
|
6 |
trained_model = GPT2LMHeadModel.from_pretrained("Kumarkishalaya/GPT-2-next-word-prediction")
|
7 |
untrained_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
@@ -15,9 +120,9 @@ untrained_model.to(device)
|
|
15 |
trained_tokenizer.pad_token = trained_tokenizer.eos_token
|
16 |
untrained_tokenizer.pad_token = untrained_tokenizer.eos_token
|
17 |
|
18 |
-
|
19 |
-
def
|
20 |
-
# Generate text using the finetuned model
|
21 |
inputs = trained_tokenizer(commentary_text, return_tensors="pt", padding=True)
|
22 |
input_ids = inputs.input_ids.to(device)
|
23 |
attention_mask = inputs.attention_mask.to(device)
|
@@ -49,22 +154,32 @@ def generate(commentary_text, max_length, temperature):
|
|
49 |
|
50 |
return trained_text, untrained_text
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
inputs=[
|
56 |
-
gr.Textbox(lines=2, placeholder="Enter
|
57 |
-
gr.Slider(minimum=
|
58 |
-
gr.Slider(minimum=
|
59 |
-
|
|
|
60 |
outputs=[
|
61 |
-
gr.Textbox(label="
|
62 |
-
gr.Textbox(label="
|
|
|
|
|
63 |
],
|
64 |
-
title="GPT-2
|
65 |
-
description="
|
66 |
)
|
67 |
|
68 |
# Launch the app
|
69 |
if __name__ == "__main__":
|
70 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
from huggingface_hub import hf_hub_download
|
7 |
+
import numpy as np
|
8 |
+
import tensorflow as tf
|
9 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
10 |
import torch
|
11 |
+
|
12 |
+
# Paths to the uploaded models
|
13 |
+
repo_id = "himanishprak23/lstm_rnn"
|
14 |
+
lstm_filename = "model_lstm_4.keras"
|
15 |
+
rnn_filename = "model_rnn_1.keras"
|
16 |
+
lstm_model_path = hf_hub_download(repo_id=repo_id, filename=lstm_filename)
|
17 |
+
rnn_model_path = hf_hub_download(repo_id=repo_id, filename=rnn_filename)
|
18 |
+
|
19 |
+
data_text_path = "/Users/himanishprakash/NLP-Application/code/data_preprocess/df_commentary.csv"
|
20 |
+
|
21 |
+
# Load the LSTM model
|
22 |
+
lstm_model = load_model(lstm_model_path)
|
23 |
+
|
24 |
+
# Load the RNN model
|
25 |
+
rnn_model = load_model(rnn_model_path)
|
26 |
+
|
27 |
+
# Load the data for tokenizer fitting
|
28 |
+
data_text = pd.read_csv(data_text_path)
|
29 |
+
|
30 |
+
# Check the embedding layer's input dimension for LSTM
|
31 |
+
embedding_layer = lstm_model.layers[0]
|
32 |
+
vocab_size = embedding_layer.input_dim
|
33 |
+
|
34 |
+
# Initialize and fit the tokenizer with limited vocabulary size
|
35 |
+
tokenizer = Tokenizer(num_words=vocab_size)
|
36 |
+
tokenizer.fit_on_texts(data_text['Modified_Commentary'])
|
37 |
+
|
38 |
+
# Define the maximum sequence length (adjust based on your model training)
|
39 |
+
max_sequence_length = 153
|
40 |
+
|
41 |
+
# Define the text generation function for LSTM
|
42 |
+
def generate_with_lstm(commentary_text, num_words):
|
43 |
+
# Tokenize the input text
|
44 |
+
input_sequence = tokenizer.texts_to_sequences([commentary_text])
|
45 |
+
input_sequence = pad_sequences(input_sequence, maxlen=max_sequence_length)
|
46 |
+
|
47 |
+
# Convert to tensor
|
48 |
+
input_tensor = tf.convert_to_tensor(input_sequence)
|
49 |
+
|
50 |
+
# Generate the next words
|
51 |
+
generated_sequence = []
|
52 |
+
for _ in range(num_words):
|
53 |
+
# Get model predictions
|
54 |
+
output = lstm_model.predict(input_tensor)
|
55 |
+
|
56 |
+
# Get the index of the most probable next word
|
57 |
+
next_word_index = np.argmax(output[0], axis=-1)
|
58 |
+
|
59 |
+
# Add the predicted word to the sequence
|
60 |
+
generated_sequence.append(next_word_index)
|
61 |
+
|
62 |
+
# Append the predicted word to the input sequence
|
63 |
+
input_sequence = np.append(input_sequence[0][1:], next_word_index).reshape(1, -1)
|
64 |
+
input_tensor = tf.convert_to_tensor(input_sequence)
|
65 |
+
|
66 |
+
# Convert indices back to words
|
67 |
+
reverse_word_index = {value: key for key, value in tokenizer.word_index.items() if value < vocab_size}
|
68 |
+
generated_words = [reverse_word_index.get(i, '') for i in generated_sequence]
|
69 |
+
|
70 |
+
# Combine the input text with the generated words
|
71 |
+
generated_text = commentary_text + ' ' + ' '.join(generated_words)
|
72 |
+
|
73 |
+
return generated_text
|
74 |
+
|
75 |
+
# Define the text generation function for RNN
|
76 |
+
def generate_with_rnn(commentary_text, num_words):
|
77 |
+
# Tokenize the input text
|
78 |
+
input_sequence = tokenizer.texts_to_sequences([commentary_text])
|
79 |
+
input_sequence = pad_sequences(input_sequence, maxlen=max_sequence_length)
|
80 |
+
|
81 |
+
# Convert to tensor
|
82 |
+
input_tensor = tf.convert_to_tensor(input_sequence)
|
83 |
+
|
84 |
+
# Generate the next words
|
85 |
+
generated_sequence = []
|
86 |
+
for _ in range(num_words):
|
87 |
+
# Get model predictions
|
88 |
+
output = rnn_model.predict(input_tensor)
|
89 |
+
|
90 |
+
# Get the index of the most probable next word
|
91 |
+
next_word_index = np.argmax(output[0], axis=-1)
|
92 |
+
|
93 |
+
# Add the predicted word to the sequence
|
94 |
+
generated_sequence.append(next_word_index)
|
95 |
+
|
96 |
+
# Append the predicted word to the input sequence
|
97 |
+
input_sequence = np.append(input_sequence[0][1:], next_word_index).reshape(1, -1)
|
98 |
+
input_tensor = tf.convert_to_tensor(input_sequence)
|
99 |
+
|
100 |
+
# Convert indices back to words
|
101 |
+
reverse_word_index = {value: key for key, value in tokenizer.word_index.items() if value < vocab_size}
|
102 |
+
generated_words = [reverse_word_index.get(i, '') for i in generated_sequence]
|
103 |
+
|
104 |
+
# Combine the input text with the generated words
|
105 |
+
generated_text = commentary_text + ' ' + ' '.join(generated_words)
|
106 |
+
|
107 |
+
return generated_text
|
108 |
+
|
109 |
+
# Load GPT-2 models and tokenizers
|
110 |
trained_tokenizer = GPT2Tokenizer.from_pretrained("Kumarkishalaya/GPT-2-next-word-prediction")
|
111 |
trained_model = GPT2LMHeadModel.from_pretrained("Kumarkishalaya/GPT-2-next-word-prediction")
|
112 |
untrained_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
|
|
120 |
trained_tokenizer.pad_token = trained_tokenizer.eos_token
|
121 |
untrained_tokenizer.pad_token = untrained_tokenizer.eos_token
|
122 |
|
123 |
+
# Define the text generation function for GPT-2
|
124 |
+
def generate_with_gpt2(commentary_text, max_length, temperature):
|
125 |
+
# Generate text using the finetuned model
|
126 |
inputs = trained_tokenizer(commentary_text, return_tensors="pt", padding=True)
|
127 |
input_ids = inputs.input_ids.to(device)
|
128 |
attention_mask = inputs.attention_mask.to(device)
|
|
|
154 |
|
155 |
return trained_text, untrained_text
|
156 |
|
157 |
+
# Define the combined function for Gradio interface
|
158 |
+
def generate_with_all_models(commentary_text, num_words, max_length, temperature):
|
159 |
+
lstm_output = generate_with_lstm(commentary_text, num_words)
|
160 |
+
rnn_output = generate_with_rnn(commentary_text, num_words)
|
161 |
+
gpt2_finetuned_output, gpt2_base_output = generate_with_gpt2(commentary_text, max_length, temperature)
|
162 |
+
return lstm_output, rnn_output, gpt2_finetuned_output, gpt2_base_output
|
163 |
+
|
164 |
+
# Create the Gradio interface
|
165 |
+
commentrymodel = gr.Interface(
|
166 |
+
fn=generate_with_all_models,
|
167 |
inputs=[
|
168 |
+
gr.Textbox(lines=2, placeholder="Enter commentary text here...", label="Prompt"),
|
169 |
+
gr.Slider(minimum=1, maximum=50, step=1, value=10, label="Number of words to predict (LSTM/RNN)"),
|
170 |
+
gr.Slider(minimum=10, maximum=100, value=50, step=1, label="Max Length (GPT-2)"),
|
171 |
+
gr.Slider(minimum=0.01, maximum=1.99, value=0.7, label="Temperature (GPT-2)")
|
172 |
+
],
|
173 |
outputs=[
|
174 |
+
gr.Textbox(label="LSTM Model Output"),
|
175 |
+
gr.Textbox(label="RNN Model Output"),
|
176 |
+
gr.Textbox(label="GPT-2 Finetuned Model Output"),
|
177 |
+
gr.Textbox(label="GPT-2 Base Model Output")
|
178 |
],
|
179 |
+
title="Text Generation with LSTM, RNN, and GPT-2 Models",
|
180 |
+
description="Start writing a cricket commentary and the models will continue it. Compare outputs from LSTM, RNN, and GPT-2 (finetuned and base) models."
|
181 |
)
|
182 |
|
183 |
# Launch the app
|
184 |
if __name__ == "__main__":
|
185 |
+
commentrymodel.launch()
|