Kurian07 commited on
Commit
3f5717f
·
verified ·
1 Parent(s): 963d17d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -45
app.py CHANGED
@@ -102,30 +102,25 @@ def 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,19 +132,18 @@ if 'db_client' not in st.session_state and option == "Chat PDF":
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:
@@ -162,18 +156,16 @@ if option == "Chat PDF" and uploaded_file is not None and st.session_state['docu
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,25 +174,18 @@ if q_input:
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()
 
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("Research Field: ", key="research_field", placeholder="Enter research fields with commas")
108
+ option = ''
109
+
110
+ if not research_field:
111
+ st.info("Please enter a research field to proceed.")
112
+ option = st.selectbox('Select Mode', ('Chat', 'Code'), disabled=True)
113
+ uploaded_file = st.file_uploader("", type=["pdf"], disabled=True)
 
114
  else:
115
+ option = st.selectbox('Select Mode', ('Chat', 'Code'))
116
+ uploaded_file = st.file_uploader("", type=["pdf"], disabled=False)
117
 
118
  temperature = st.slider("Select Temperature", min_value=0.0, max_value=1.0, value=0.05, step=0.01)
119
  selected_llm_model = st.selectbox("Select LLM Model", options=list(llm_model.keys()), index=3)
120
  top_k = st.slider("Select Top K Matches", min_value=1, max_value=20, value=5)
121
 
122
+ ## Initialize unique ID, db_client, db_path, and timestamp if not already in session state
123
+ if 'db_client' not in st.session_state:
124
  unique_id = str(uuid.uuid4())
125
  st.session_state['unique_id'] = unique_id
126
  db_path = os.path.join(VECTOR_DB_DIR, unique_id)
 
132
  log_upload_time(unique_id)
133
 
134
  # Access session-stored variables
135
+ db_client = st.session_state['db_client']
136
+ unique_id = st.session_state['unique_id']
137
+ db_path = st.session_state['db_path']
 
138
 
139
+ if 'document_text' not in st.session_state:
140
+ st.session_state['document_text'] = None
141
 
142
+ if 'text_embeddings' not in st.session_state:
143
+ st.session_state['text_embeddings'] = None
144
 
145
  ## Handle PDF Upload and Processing
146
+ if uploaded_file is not None and st.session_state['document_text'] is None:
147
  os.makedirs(UPLOAD_DIR, exist_ok=True)
148
  file_path = os.path.join(UPLOAD_DIR, f"{unique_id}_paper.pdf")
149
  with open(file_path, "wb") as file:
 
156
  text_contents_embeddings = contextEmbeddingChroma(embeddModel, text_content_chunks, db_client, db_path=db_path)
157
  st.session_state['text_embeddings'] = text_contents_embeddings
158
 
159
+ if st.session_state['document_text'] and st.session_state['text_embeddings']:
160
  document_text = st.session_state['document_text']
161
  text_contents_embeddings = st.session_state['text_embeddings']
162
  else:
163
+ st.stop()
 
164
 
 
165
  q_input = st.chat_input(key="input", placeholder="Ask your question")
166
 
167
  if q_input:
168
+ if option == "Chat":
169
  query_embedding = ragQuery(embeddModel, q_input)
170
  top_k_matches = similarityChroma(query_embedding, db_client, top_k)
171
 
 
174
  prompt_template = q_input
175
  user_content = top_k_matches
176
  max_tokens = max_tokens[selected_llm_model]
177
+ print(max_tokens)
178
+ top_p = 1
179
+ stream = True
180
+ stop = None
181
 
182
+ groq_completion = GroqCompletion(groq_client, LLMmodel, domain, prompt_template, user_content, temperature, max_tokens, top_p, stream, stop)
183
  result = groq_completion.create_completion()
184
 
185
  with st.spinner("Processing..."):
186
  chat_response(q_input, result)
187
 
188
+ ## Call the cleanup function periodically
189
+ cleanup_expired_files()
 
 
 
 
190
 
 
 
 
 
 
191