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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -51
app.py CHANGED
@@ -5,12 +5,8 @@ from llama_index.core import Settings
5
  import os
6
  import pdfplumber
7
  from docx import Document as DocxDocument
8
- from dotenv import load_dotenv
9
  import json
10
 
11
- # Load environment variables from .env file
12
- load_dotenv(".env")
13
-
14
  st.header("Chat with the Streamlit docs πŸ’¬ πŸ“š")
15
 
16
  # Sidebar for OpenAI API Key
@@ -46,10 +42,6 @@ def read_docx(file):
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,11 +52,9 @@ def load_data(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)
69
  return index
70
 
@@ -96,44 +86,43 @@ uploaded_files = st.file_uploader("Upload PDF or DOCX files", type=["pdf", "docx
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,4 +158,4 @@ if st.session_state.show_conversations:
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.")
 
5
  import os
6
  import pdfplumber
7
  from docx import Document as DocxDocument
 
8
  import json
9
 
 
 
 
10
  st.header("Chat with the Streamlit docs πŸ’¬ πŸ“š")
11
 
12
  # Sidebar for OpenAI API Key
 
42
 
43
  @st.cache_resource(show_spinner=False)
44
  def load_data(uploaded_files):
 
 
 
 
45
  with st.spinner("Loading and indexing the documents – hang tight! This should take 1-2 minutes."):
46
  docs = []
47
  for uploaded_file in uploaded_files:
 
52
  text = read_docx(uploaded_file)
53
  docs.append(Document(text=text))
54
 
55
+ Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
 
 
56
  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.")
57
+
58
  index = VectorStoreIndex.from_documents(docs, settings=Settings.llm)
59
  return index
60
 
 
86
 
87
  if uploaded_files and st.session_state.openai_api_key:
88
  index = load_data(uploaded_files)
89
+ chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
90
+
91
+ # User input for questions
92
+ if prompt := st.chat_input("Your question"):
93
+ st.session_state.messages.append({"role": "user", "content": prompt})
94
+
95
+ for message in st.session_state.messages:
96
+ with st.chat_message(message["role"]):
97
+ st.write(message["content"])
98
+
99
+ if len(st.session_state.messages) > 0 and st.session_state.messages[-1]["role"] != "assistant":
100
+ with st.chat_message("assistant"):
101
+ with st.spinner("Thinking..."):
102
+ response = chat_engine.chat(prompt)
103
+ st.write(response.response)
104
+ message = {"role": "assistant", "content": response.response}
105
+ st.session_state.messages.append(message)
106
+
107
+ if st.button("Save Conversation"):
108
+ if st.session_state.messages:
109
+ st.session_state.confirm_save = True
110
+
111
+ if st.session_state.get('confirm_save', False):
112
+ st.warning("Do you want to save the conversation?")
113
+ col1, col2 = st.columns(2)
114
+ with col1:
115
+ if st.button("Yes"):
116
+ save_conversation()
117
+ st.success("Conversation saved!")
118
+ st.session_state.confirm_save = False
119
+ with col2:
120
+ if st.button("No"):
121
+ st.session_state.confirm_save = False
122
+
123
+ if st.button("End Conversation"):
124
+ st.session_state.messages = []
125
+ st.success("Conversation ended. You can start a new one!")
 
126
 
127
  else:
128
  st.sidebar.warning("Please enter your OpenAI API key and upload PDF or DOCX files to proceed.")
 
158
  else:
159
  st.sidebar.write("No previous conversations found.")
160
  else:
161
+ st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")