Madhuri commited on
Commit
a7d489b
1 Parent(s): 16f792f

Move model initialization to separate thread.

Browse files

Initializing the model in main thread slows down the loading of
streamlit application, so initialize it from the thread.

Files changed (2) hide show
  1. app.py +9 -1
  2. chatbot.py +2 -2
app.py CHANGED
@@ -1,10 +1,15 @@
1
  import streamlit as st
2
 
3
  from model import predictor
 
4
  import audiobot
5
  import chatbot
6
  import os
 
7
 
 
 
 
8
 
9
  def run():
10
  st.set_page_config(
@@ -14,7 +19,6 @@ def run():
14
  )
15
 
16
  os.environ['TOKENIZERS_PARALLELISM'] = 'false'
17
- st.session_state['predictor'] = predictor.Predictor()
18
 
19
  st.sidebar.title('VQA Bot')
20
  st.sidebar.write('''
@@ -35,5 +39,9 @@ def run():
35
  '[Github](https://github.com/msak1612/vqa_chatbot)'
36
  )
37
 
 
 
 
 
38
 
39
  run()
 
1
  import streamlit as st
2
 
3
  from model import predictor
4
+ from streamlit.scriptrunner import add_script_run_ctx
5
  import audiobot
6
  import chatbot
7
  import os
8
+ import threading
9
 
10
+ def runInThread():
11
+ print('Initialize model in thread')
12
+ st.session_state['predictor'] = predictor.Predictor()
13
 
14
  def run():
15
  st.set_page_config(
 
19
  )
20
 
21
  os.environ['TOKENIZERS_PARALLELISM'] = 'false'
 
22
 
23
  st.sidebar.title('VQA Bot')
24
  st.sidebar.write('''
 
39
  '[Github](https://github.com/msak1612/vqa_chatbot)'
40
  )
41
 
42
+ if 'predictor' not in st.session_state:
43
+ thread = threading.Thread(target=runInThread)
44
+ add_script_run_ctx(thread)
45
+ thread.start()
46
 
47
  run()
chatbot.py CHANGED
@@ -47,8 +47,8 @@ def show():
47
  image = Image.open(upload_pic)
48
  st.image(upload_pic, use_column_width='auto')
49
  else:
50
- st.session_state.question.clear()
51
- st.session_state.answer.clear()
52
  st.session_state.input = ''
53
  with text_col:
54
  input = st.text_input('', '', key='input')
 
47
  image = Image.open(upload_pic)
48
  st.image(upload_pic, use_column_width='auto')
49
  else:
50
+ st.session_state.question=[]
51
+ st.session_state.answer=[]
52
  st.session_state.input = ''
53
  with text_col:
54
  input = st.text_input('', '', key='input')