shukdevdatta123 commited on
Commit
32702f7
Β·
verified Β·
1 Parent(s): 07d2db3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -38
app.py CHANGED
@@ -46,6 +46,10 @@ def read_docx(file):
46
 
47
  @st.cache_resource(show_spinner=False)
48
  def load_data(uploaded_files):
 
 
 
 
49
  with st.spinner("Loading and indexing the documents – hang tight! This should take 1-2 minutes."):
50
  docs = []
51
  for uploaded_file in uploaded_files:
@@ -56,7 +60,9 @@ def load_data(uploaded_files):
56
  text = read_docx(uploaded_file)
57
  docs.append(Document(text=text))
58
 
 
59
  Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
 
60
  system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
61
 
62
  index = VectorStoreIndex.from_documents(docs, settings=Settings.llm)
@@ -90,43 +96,44 @@ uploaded_files = st.file_uploader("Upload PDF or DOCX files", type=["pdf", "docx
90
 
91
  if uploaded_files and st.session_state.openai_api_key:
92
  index = load_data(uploaded_files)
93
- chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
94
-
95
- # User input for questions
96
- if prompt := st.chat_input("Your question"):
97
- st.session_state.messages.append({"role": "user", "content": prompt})
98
-
99
- for message in st.session_state.messages:
100
- with st.chat_message(message["role"]):
101
- st.write(message["content"])
102
-
103
- if len(st.session_state.messages) > 0 and st.session_state.messages[-1]["role"] != "assistant":
104
- with st.chat_message("assistant"):
105
- with st.spinner("Thinking..."):
106
- response = chat_engine.chat(prompt)
107
- st.write(response.response)
108
- message = {"role": "assistant", "content": response.response}
109
- st.session_state.messages.append(message)
110
-
111
- if st.button("Save Conversation"):
112
- if st.session_state.messages:
113
- st.session_state.confirm_save = True
114
-
115
- if st.session_state.get('confirm_save', False):
116
- st.warning("Do you want to save the conversation?")
117
- col1, col2 = st.columns(2)
118
- with col1:
119
- if st.button("Yes"):
120
- save_conversation()
121
- st.success("Conversation saved!")
122
- st.session_state.confirm_save = False
123
- with col2:
124
- if st.button("No"):
125
- st.session_state.confirm_save = False
126
-
127
- if st.button("End Conversation"):
128
- st.session_state.messages = []
129
- st.success("Conversation ended. You can start a new one!")
 
130
 
131
  else:
132
  st.sidebar.warning("Please enter your OpenAI API key and upload PDF or DOCX files to proceed.")
@@ -162,4 +169,4 @@ if st.session_state.show_conversations:
162
  else:
163
  st.sidebar.write("No previous conversations found.")
164
  else:
165
- st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")
 
46
 
47
  @st.cache_resource(show_spinner=False)
48
  def load_data(uploaded_files):
49
+ if not st.session_state.openai_api_key:
50
+ st.error("No OpenAI API key provided. Please enter a valid API key.")
51
+ return None
52
+
53
  with st.spinner("Loading and indexing the documents – hang tight! This should take 1-2 minutes."):
54
  docs = []
55
  for uploaded_file in uploaded_files:
 
60
  text = read_docx(uploaded_file)
61
  docs.append(Document(text=text))
62
 
63
+ # Set the OpenAI API key
64
  Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
65
+ api_key=st.session_state.openai_api_key,
66
  system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
67
 
68
  index = VectorStoreIndex.from_documents(docs, settings=Settings.llm)
 
96
 
97
  if uploaded_files and st.session_state.openai_api_key:
98
  index = load_data(uploaded_files)
99
+ if index: # Ensure index is not None
100
+ chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
101
+
102
+ # User input for questions
103
+ if prompt := st.chat_input("Your question"):
104
+ st.session_state.messages.append({"role": "user", "content": prompt})
105
+
106
+ for message in st.session_state.messages:
107
+ with st.chat_message(message["role"]):
108
+ st.write(message["content"])
109
+
110
+ if len(st.session_state.messages) > 0 and st.session_state.messages[-1]["role"] != "assistant":
111
+ with st.chat_message("assistant"):
112
+ with st.spinner("Thinking..."):
113
+ response = chat_engine.chat(prompt)
114
+ st.write(response.response)
115
+ message = {"role": "assistant", "content": response.response}
116
+ st.session_state.messages.append(message)
117
+
118
+ if st.button("Save Conversation"):
119
+ if st.session_state.messages:
120
+ st.session_state.confirm_save = True
121
+
122
+ if st.session_state.get('confirm_save', False):
123
+ st.warning("Do you want to save the conversation?")
124
+ col1, col2 = st.columns(2)
125
+ with col1:
126
+ if st.button("Yes"):
127
+ save_conversation()
128
+ st.success("Conversation saved!")
129
+ st.session_state.confirm_save = False
130
+ with col2:
131
+ if st.button("No"):
132
+ st.session_state.confirm_save = False
133
+
134
+ if st.button("End Conversation"):
135
+ st.session_state.messages = []
136
+ st.success("Conversation ended. You can start a new one!")
137
 
138
  else:
139
  st.sidebar.warning("Please enter your OpenAI API key and upload PDF or DOCX files to proceed.")
 
169
  else:
170
  st.sidebar.write("No previous conversations found.")
171
  else:
172
+ st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")