Kurian07 commited on
Commit
963d17d
·
verified ·
1 Parent(s): 90fd58f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -55
app.py CHANGED
@@ -102,41 +102,30 @@ def cleanup_expired_files():
102
 
103
  ## Context Taking, PDF Upload, and Mode Selection
104
  with st.sidebar:
105
- st.title("Upload PDF:")
106
-
107
- research_field = st.text_input(
108
- "Research Field: ",
109
- key="research_field",
110
- placeholder="Enter research fields with commas"
111
  )
112
-
113
- if not research_field:
114
- st.info("Please enter a research field to proceed.")
115
- uploaded_file = st.file_uploader("", type=["pdf"], disabled=True)
 
 
 
 
 
116
  else:
117
- uploaded_file = st.file_uploader("", type=["pdf"], disabled=False)
118
-
119
- temperature = st.slider(
120
- "Select Temperature",
121
- min_value=0.0,
122
- max_value=1.0,
123
- value=0.05,
124
- step=0.01
125
- )
126
- selected_llm_model = st.selectbox(
127
- "Select LLM Model",
128
- options=list(llm_model.keys()),
129
- index=3
130
- )
131
- top_k = st.slider(
132
- "Select Top K Matches",
133
- min_value=1,
134
- max_value=20,
135
- value=5
136
- )
137
 
138
- ## Initialize unique ID, db_client, db_path, and timestamp if not already in session state
139
- if 'db_client' not in st.session_state:
140
  unique_id = str(uuid.uuid4())
141
  st.session_state['unique_id'] = unique_id
142
  db_path = os.path.join(VECTOR_DB_DIR, unique_id)
@@ -148,18 +137,19 @@ if 'db_client' not in st.session_state:
148
  log_upload_time(unique_id)
149
 
150
  # Access session-stored variables
151
- db_client = st.session_state['db_client']
152
- unique_id = st.session_state['unique_id']
153
- db_path = st.session_state['db_path']
 
154
 
155
- if 'document_text' not in st.session_state:
156
- st.session_state['document_text'] = None
157
 
158
- if 'text_embeddings' not in st.session_state:
159
- st.session_state['text_embeddings'] = None
160
 
161
  ## Handle PDF Upload and Processing
162
- if uploaded_file is not None and st.session_state['document_text'] is None:
163
  os.makedirs(UPLOAD_DIR, exist_ok=True)
164
  file_path = os.path.join(UPLOAD_DIR, f"{unique_id}_paper.pdf")
165
  with open(file_path, "wb") as file:
@@ -169,24 +159,21 @@ if uploaded_file is not None and st.session_state['document_text'] is None:
169
  st.session_state['document_text'] = document_text
170
 
171
  text_content_chunks = contextChunks(document_text, chunk_size, chunk_overlap)
172
- text_contents_embeddings = contextEmbeddingChroma(
173
- embeddModel,
174
- text_content_chunks,
175
- db_client,
176
- db_path=db_path
177
- )
178
  st.session_state['text_embeddings'] = text_contents_embeddings
179
 
180
- if st.session_state['document_text'] and st.session_state['text_embeddings']:
181
  document_text = st.session_state['document_text']
182
  text_contents_embeddings = st.session_state['text_embeddings']
183
  else:
184
- st.stop()
 
185
 
 
186
  q_input = st.chat_input(key="input", placeholder="Ask your question")
187
 
188
  if q_input:
189
- if option == "Chat":
190
  query_embedding = ragQuery(embeddModel, q_input)
191
  top_k_matches = similarityChroma(query_embedding, db_client, top_k)
192
 
@@ -195,16 +182,25 @@ if q_input:
195
  prompt_template = q_input
196
  user_content = top_k_matches
197
  max_tokens = max_tokens[selected_llm_model]
198
- print(max_tokens)
199
- top_p = 1
200
- stream = True
201
- stop = None
202
 
203
- groq_completion = GroqCompletion(groq_client, LLMmodel, domain, prompt_template, user_content, temperature, max_tokens, top_p, stream, stop)
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  result = groq_completion.create_completion()
205
 
206
  with st.spinner("Processing..."):
207
  chat_response(q_input, result)
208
 
209
- ## Call the cleanup function periodically
210
  cleanup_expired_files()
 
102
 
103
  ## Context Taking, PDF Upload, and Mode Selection
104
  with st.sidebar:
105
+ st.title("Select Mode:")
106
+ option = st.selectbox(
107
+ 'Choose your interaction mode',
108
+ ('Chat PDF', 'Chat LLM')
 
 
109
  )
110
+
111
+ if option == "Chat PDF":
112
+ st.title("Upload PDF:")
113
+ research_field = st.text_input("Research Field: ", key="research_field", placeholder="Enter research fields with commas")
114
+ if not research_field:
115
+ st.info("Please enter a research field to proceed.")
116
+ uploaded_file = st.file_uploader("", type=["pdf"], disabled=True)
117
+ else:
118
+ uploaded_file = st.file_uploader("", type=["pdf"], disabled=False)
119
  else:
120
+ research_field = None
121
+ uploaded_file = None
122
+
123
+ temperature = st.slider("Select Temperature", min_value=0.0, max_value=1.0, value=0.05, step=0.01)
124
+ selected_llm_model = st.selectbox("Select LLM Model", options=list(llm_model.keys()), index=3)
125
+ top_k = st.slider("Select Top K Matches", min_value=1, max_value=20, value=5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
+ ## Initialize unique ID, db_client, db_path, and timestamp if needed
128
+ if 'db_client' not in st.session_state and option == "Chat PDF":
129
  unique_id = str(uuid.uuid4())
130
  st.session_state['unique_id'] = unique_id
131
  db_path = os.path.join(VECTOR_DB_DIR, unique_id)
 
137
  log_upload_time(unique_id)
138
 
139
  # Access session-stored variables
140
+ if option == "Chat PDF":
141
+ db_client = st.session_state['db_client']
142
+ unique_id = st.session_state['unique_id']
143
+ db_path = st.session_state['db_path']
144
 
145
+ if 'document_text' not in st.session_state:
146
+ st.session_state['document_text'] = None
147
 
148
+ if 'text_embeddings' not in st.session_state:
149
+ st.session_state['text_embeddings'] = None
150
 
151
  ## Handle PDF Upload and Processing
152
+ if option == "Chat PDF" and uploaded_file is not None and st.session_state['document_text'] is None:
153
  os.makedirs(UPLOAD_DIR, exist_ok=True)
154
  file_path = os.path.join(UPLOAD_DIR, f"{unique_id}_paper.pdf")
155
  with open(file_path, "wb") as file:
 
159
  st.session_state['document_text'] = document_text
160
 
161
  text_content_chunks = contextChunks(document_text, chunk_size, chunk_overlap)
162
+ text_contents_embeddings = contextEmbeddingChroma(embeddModel, text_content_chunks, db_client, db_path=db_path)
 
 
 
 
 
163
  st.session_state['text_embeddings'] = text_contents_embeddings
164
 
165
+ if option == "Chat PDF" and st.session_state['document_text'] and st.session_state['text_embeddings']:
166
  document_text = st.session_state['document_text']
167
  text_contents_embeddings = st.session_state['text_embeddings']
168
  else:
169
+ if option == "Chat PDF":
170
+ st.stop()
171
 
172
+ ## Chat Input for Both Modes
173
  q_input = st.chat_input(key="input", placeholder="Ask your question")
174
 
175
  if q_input:
176
+ if option == "Chat PDF":
177
  query_embedding = ragQuery(embeddModel, q_input)
178
  top_k_matches = similarityChroma(query_embedding, db_client, top_k)
179
 
 
182
  prompt_template = q_input
183
  user_content = top_k_matches
184
  max_tokens = max_tokens[selected_llm_model]
 
 
 
 
185
 
186
+ groq_completion = GroqCompletion(groq_client, LLMmodel, domain, prompt_template, user_content, temperature, max_tokens, top_p=1, stream=True, stop=None)
187
+ result = groq_completion.create_completion()
188
+
189
+ with st.spinner("Processing..."):
190
+ chat_response(q_input, result)
191
+
192
+ elif option == "Chat LLM":
193
+ LLMmodel = llm_model[selected_llm_model]
194
+ domain = "General"
195
+ prompt_template = q_input
196
+ user_content = ""
197
+ max_tokens = max_tokens[selected_llm_model]
198
+
199
+ groq_completion = GroqCompletion(groq_client, LLMmodel, domain, prompt_template, user_content, temperature, max_tokens, top_p=1, stream=True, stop=None)
200
  result = groq_completion.create_completion()
201
 
202
  with st.spinner("Processing..."):
203
  chat_response(q_input, result)
204
 
205
+ ## Periodic Cleanup
206
  cleanup_expired_files()