harshvardhan96 commited on
Commit
cee3943
·
1 Parent(s): edf104b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -3,17 +3,19 @@ import numpy as np
3
  import pickle
4
  import gradio as gr
5
  from tensorflow.keras.preprocessing import sequence
 
 
6
 
7
  # Load the encoder model
8
- enc_model = tf.keras.models.load_model('./encoder_model.h5')
9
 
10
  # Load the decoder model
11
- dec_model = tf.keras.models.load_model('./decoder_model.h5')
12
 
13
- with open('./tokenizer.pkl', 'rb') as f:
14
  tokenizer = pickle.load(f)
15
 
16
- with open('./tokenizer_params (1).pkl', 'rb') as f:
17
  tokenizer_params = pickle.load(f)
18
 
19
  maxlen_questions = tokenizer_params["maxlen_questions"]
@@ -27,7 +29,7 @@ def str_to_tokens(sentence: str):
27
  tokens_list.append(tokenizer.word_index[word])
28
  return sequence.pad_sequences([tokens_list], maxlen=maxlen_questions, padding='post')
29
 
30
- def chatbot_response(question):
31
  states_values = enc_model.predict(str_to_tokens(question))
32
  empty_target_seq = np.zeros((1, 1))
33
  empty_target_seq[0, 0] = tokenizer.word_index['start']
@@ -52,16 +54,28 @@ def chatbot_response(question):
52
  states_values = [h, c]
53
 
54
  decoded_translation = decoded_translation.split(' end')[0]
55
- return decoded_translation
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- # Gradio Interface
 
 
58
  iface = gr.Interface(
59
- fn=chatbot_response,
60
- inputs=gr.inputs.Textbox(),
61
- outputs=gr.outputs.Textbox(),
62
  title="Chatbot",
63
  description="Talk to the chatbot and it will respond!"
64
  )
65
 
66
- # Launch the Gradio interface on Hugging Face Spaces
67
  iface.launch()
 
3
  import pickle
4
  import gradio as gr
5
  from tensorflow.keras.preprocessing import sequence
6
+ import random
7
+ import time
8
 
9
  # Load the encoder model
10
+ enc_model = tf.keras.models.load_model('/kaggle/input/model-1/encoder_model.h5')
11
 
12
  # Load the decoder model
13
+ dec_model = tf.keras.models.load_model('/kaggle/input/model-1/decoder_model.h5')
14
 
15
+ with open('/kaggle/input/tokenizer1/tokenizer.pkl', 'rb') as f:
16
  tokenizer = pickle.load(f)
17
 
18
+ with open('/kaggle/input/tokenizer-params/tokenizer_params (1).pkl', 'rb') as f:
19
  tokenizer_params = pickle.load(f)
20
 
21
  maxlen_questions = tokenizer_params["maxlen_questions"]
 
29
  tokens_list.append(tokenizer.word_index[word])
30
  return sequence.pad_sequences([tokens_list], maxlen=maxlen_questions, padding='post')
31
 
32
+ def chatbot_response(question, chat_history):
33
  states_values = enc_model.predict(str_to_tokens(question))
34
  empty_target_seq = np.zeros((1, 1))
35
  empty_target_seq[0, 0] = tokenizer.word_index['start']
 
54
  states_values = [h, c]
55
 
56
  decoded_translation = decoded_translation.split(' end')[0]
57
+ bot_message = decoded_translation
58
+
59
+ chat_history.append((question, bot_message))
60
+ time.sleep(2)
61
+ return "", chat_history
62
+
63
+ # Gradio Blocks Interface
64
+ with gr.Blocks() as demo:
65
+ chatbot = gr.Chatbot()
66
+ msg = gr.Textbox()
67
+ clear = gr.ClearButton([msg, chatbot])
68
+
69
+ def respond(message, chat_history):
70
+ return chatbot_response(message, chat_history)
71
 
72
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
73
+
74
+ # Launch the Gradio interface on Hugging Face Spaces
75
  iface = gr.Interface(
76
+ fn=demo,
 
 
77
  title="Chatbot",
78
  description="Talk to the chatbot and it will respond!"
79
  )
80
 
 
81
  iface.launch()