Shiv22419 commited on
Commit
470d90e
Β·
verified Β·
1 Parent(s): ed08d37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -48
app.py CHANGED
@@ -3,6 +3,8 @@ import os
3
  import streamlit as st
4
  from reportlab.lib.pagesizes import letter
5
  from reportlab.pdfgen import canvas
 
 
6
  from langchain_community.vectorstores import FAISS
7
  from langchain_community.embeddings import HuggingFaceEmbeddings
8
  from langchain.prompts import PromptTemplate
@@ -11,6 +13,48 @@ from langchain.chains import ConversationalRetrievalChain
11
  from langchain_together import Together
12
 
13
  from footer import footer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # Set the Streamlit page configuration and theme
16
  st.set_page_config(page_title="In-Legal-IPC", layout="centered")
@@ -111,34 +155,21 @@ def create_pdf(content):
111
  c.save()
112
  return pdf_filename
113
 
114
-
115
  # Add links to multiple PDFs just above the chat input
116
- #st.markdown("""
117
- # ### Useful PDFs
118
- # - [πŸ“„ Commercial Court Rules and Forms](https://drive.google.com/file/d/1puzlPMT7fTt4utWJaGlFtOjW38CoFctc/view?usp=drive_link)
119
- # - [πŸ“„ Bail-Bond](https://drive.google.com/file/d/1uRlT7Yo_2jemxs5aRyvHzoLgeS7S81Vn/view?usp=drive_link)
120
- # - [πŸ“„ Inspection Form](https://drive.google.com/file/d/1Ib-RC4xPMqZVl7YgVES3Vb47Rb42W83s/view?usp=drive_link)
121
- # - [πŸ“„ Additional PDF](https://drive.google.com/file/d/1Xkq64r4Id8qSyb5woVzdvArhzUxvKJk8/view?usp=drive_link)
122
- # """, unsafe_allow_html=True)
123
-
124
-
125
-
126
- # Add the PDF buttons
127
  st.markdown("<h3 class='underline'>Useful PDFs</h3>", unsafe_allow_html=True)
128
 
129
  col1, col2 = st.columns(2) # Create two columns for better alignment
130
  with col1:
131
  if st.button("Commercial Court Rules and Forms πŸ“„", key="ccrf", help="Open PDF", use_container_width=True):
132
- st.markdown("[Open PDF](https://drive.google.com/file/d/1puzlPMT7fTt4utWJaGlFtOjW38CoFctc/view?usp=drive_link)", unsafe_allow_html=True)
133
  if st.button("Bail-Bond πŸ“„", key="bb", help="Open PDF", use_container_width=True):
134
- st.markdown("[Open PDF](https://drive.google.com/file/d/1uRlT7Yo_2jemxs5aRyvHzoLgeS7S81Vn/view?usp=drive_link)", unsafe_allow_html=True)
135
 
136
  with col2:
137
  if st.button("Inspection Form πŸ“„", key="if", help="Open PDF", use_container_width=True):
138
- st.markdown("[Open PDF](https://drive.google.com/file/d/1Ib-RC4xPMqZVl7YgVES3Vb47Rb42W83s/view?usp=drive_link)", unsafe_allow_html=True)
139
  if st.button("Additional PDF πŸ“„", key="apdf", help="Open PDF", use_container_width=True):
140
- st.markdown("[Open PDF](https://drive.google.com/file/d/1Xkq64r4Id8qSyb5woVzdvArhzUxvKJk8/view?usp=drive_link)", unsafe_allow_html=True)
141
-
142
 
143
  # Add CSS for the button styling
144
  st.markdown("""
@@ -151,45 +182,79 @@ st.markdown("""
151
  </style>
152
  """, unsafe_allow_html=True)
153
 
154
-
155
-
156
  # Display previous messages
157
  for message in st.session_state.messages:
158
  with st.chat_message(message["role"]):
159
  st.write(message["content"])
160
 
 
 
 
 
161
  # Chat input area
162
  input_prompt = st.chat_input("Say something...")
163
  if input_prompt:
164
  with st.chat_message("user"):
165
  st.markdown(f"**You:** {input_prompt}")
166
 
167
- st.session_state.messages.append({"role": "user", "content": input_prompt})
168
- with st.chat_message("assistant"):
169
- with st.spinner("Thinking πŸ’‘..."):
170
- result = qa.invoke(input=input_prompt)
171
- message_placeholder = st.empty()
172
- answer = extract_answer(result["answer"])
173
-
174
- # Initialize the response message
175
- full_response = "⚠️ **_Gentle reminder: We generally ensure precise information, but do double-check._** \n\n\n"
176
- for chunk in answer:
177
- # Simulate typing by appending chunks of the response over time
178
- full_response += chunk
179
- time.sleep(0.02) # Adjust the sleep time to control the "typing" speed
180
- message_placeholder.markdown(full_response + " |", unsafe_allow_html=True)
181
-
182
- st.session_state.messages.append({"role": "assistant", "content": answer})
183
-
184
- # Generate PDF if the user indicates a personal legal problem
185
- if "personal legal problem" in input_prompt.lower():
186
- user_problem = input_prompt # Capture the user's legal problem
187
- pdf_content = f"Dear Client,\n\nYour legal problem is as follows:\n{user_problem}\n\nBest Regards,\nYour Legal Advisor"
188
- pdf_file = create_pdf(pdf_content)
189
- st.download_button(label="Download your legal letter", data=pdf_file, file_name="legal_letter.pdf")
190
-
191
- if st.button('πŸ—‘οΈ Reset All Chat', on_click=reset_conversation):
192
- st.experimental_rerun()
193
-
194
- # Define the CSS to style the footer
195
- footer()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import streamlit as st
4
  from reportlab.lib.pagesizes import letter
5
  from reportlab.pdfgen import canvas
6
+ from googleapiclient.discovery import build
7
+ from google.oauth2.service_account import Credentials
8
  from langchain_community.vectorstores import FAISS
9
  from langchain_community.embeddings import HuggingFaceEmbeddings
10
  from langchain.prompts import PromptTemplate
 
13
  from langchain_together import Together
14
 
15
  from footer import footer
16
+
17
+ # Google Drive API setup
18
+ SCOPES = ["https://www.googleapis.com/auth/drive.readonly"]
19
+ SERVICE_ACCOUNT_FILE = "data/credentials.json" # Path to your Google API credentials file
20
+ FOLDER_ID = "1LZIx-1tt_GormpU8nF_I2WL88Oxa9juU" # Replace with your Google Drive folder ID
21
+
22
+ def authenticate_drive():
23
+ """Authenticate and return the Google Drive API service."""
24
+ creds = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
25
+ return build("drive", "v3", credentials=creds)
26
+
27
+
28
+
29
+ from fuzzywuzzy import process
30
+
31
+ def search_drive_file(file_name):
32
+ """Search for a file by name in the specified Google Drive folder using fuzzy matching."""
33
+ service = authenticate_drive()
34
+ try:
35
+ # Get all files in the folder
36
+ query = f"'{FOLDER_ID}' in parents and trashed=false"
37
+ results = service.files().list(q=query, fields="files(id, name)").execute()
38
+ files = results.get("files", [])
39
+
40
+ # Debug: Print all file names for inspection
41
+ st.write("Available files:", [f['name'] for f in files])
42
+
43
+ # Perform fuzzy matching to find the best match
44
+ file_names = [f['name'] for f in files]
45
+ best_match, score = process.extractOne(file_name, file_names)
46
+
47
+ if score >= 75: # Threshold for a match
48
+ matched_file = next(f for f in files if f['name'] == best_match)
49
+ st.write(f"Match found: {matched_file['name']} (Score: {score})")
50
+ return [matched_file]
51
+ else:
52
+ st.warning(f"No close matches found for '{file_name}'. Try rephrasing or checking the folder manually.")
53
+ return []
54
+
55
+ except Exception as e:
56
+ st.error(f"An error occurred: {e}")
57
+ return []
58
 
59
  # Set the Streamlit page configuration and theme
60
  st.set_page_config(page_title="In-Legal-IPC", layout="centered")
 
155
  c.save()
156
  return pdf_filename
157
 
 
158
  # Add links to multiple PDFs just above the chat input
 
 
 
 
 
 
 
 
 
 
 
159
  st.markdown("<h3 class='underline'>Useful PDFs</h3>", unsafe_allow_html=True)
160
 
161
  col1, col2 = st.columns(2) # Create two columns for better alignment
162
  with col1:
163
  if st.button("Commercial Court Rules and Forms πŸ“„", key="ccrf", help="Open PDF", use_container_width=True):
164
+ st.markdown("[Open PDF](https://drive.google.com/file/d/198SC1mKipJ7WQXGN-5uc8qkNV5rLxVlT/view?usp=sharing)", unsafe_allow_html=True)
165
  if st.button("Bail-Bond πŸ“„", key="bb", help="Open PDF", use_container_width=True):
166
+ st.markdown("[Open PDF](https://drive.google.com/file/d/1Eju14MgFFME3nUknjwlbU8C9nrQoeM1v/view?usp=drive_link)", unsafe_allow_html=True)
167
 
168
  with col2:
169
  if st.button("Inspection Form πŸ“„", key="if", help="Open PDF", use_container_width=True):
170
+ st.markdown("[Open PDF](https://drive.google.com/file/d/17FT5Pmgp4bgf31tFyQRMNVnoRuVlQ2zi/view?usp=sharing)", unsafe_allow_html=True)
171
  if st.button("Additional PDF πŸ“„", key="apdf", help="Open PDF", use_container_width=True):
172
+ st.markdown("[Open PDF](https://drive.google.com/file/d/1LY1-R9chmd_I7Tf3iC4jNZ5dHRFFkjaV/view?usp=sharing)", unsafe_allow_html=True)
 
173
 
174
  # Add CSS for the button styling
175
  st.markdown("""
 
182
  </style>
183
  """, unsafe_allow_html=True)
184
 
 
 
185
  # Display previous messages
186
  for message in st.session_state.messages:
187
  with st.chat_message(message["role"]):
188
  st.write(message["content"])
189
 
190
+ # Initialize session state variables
191
+ if "show_reset" not in st.session_state:
192
+ st.session_state.show_reset = False
193
+
194
  # Chat input area
195
  input_prompt = st.chat_input("Say something...")
196
  if input_prompt:
197
  with st.chat_message("user"):
198
  st.markdown(f"**You:** {input_prompt}")
199
 
200
+ # Enable the reset button after receiving input
201
+ st.session_state.show_reset = True
202
+
203
+ if "form" in input_prompt.lower() or "document" in input_prompt.lower():
204
+ with st.spinner("Searching Google Drive..."):
205
+ # Call the updated search function
206
+ search_results = search_drive_file(input_prompt)
207
+
208
+ if search_results:
209
+ # Generate response for found files
210
+ response = "πŸ” Document(s) found! Click below to view:"
211
+ for file in search_results:
212
+ response += f"\n- [{file['name']}](https://drive.google.com/file/d/{file['id']}/view)"
213
+ st.session_state.messages.append({"role": "assistant", "content": response})
214
+ st.write(response)
215
+ else:
216
+ # If no results, provide an alternative message
217
+ response = (
218
+ "⚠️ No matching documents found. "
219
+ "Please check the spelling or explore the folder directly: "
220
+ f"[Google Drive Folder](https://drive.google.com/drive/folders/{FOLDER_ID})"
221
+ )
222
+ st.session_state.messages.append({"role": "assistant", "content": response})
223
+ st.write(response)
224
+
225
+ else:
226
+ # Handle general questions
227
+ with st.chat_message("assistant"):
228
+ with st.spinner("Thinking πŸ’‘..."):
229
+ try:
230
+ # Validate the input before invoking the QA chain
231
+ if not input_prompt.strip():
232
+ st.warning("⚠️ Input cannot be empty!")
233
+ else:
234
+ result = qa.invoke(input=input_prompt)
235
+ answer = result["answer"].strip()
236
+
237
+ # Simulate typing effect for the response
238
+ message_placeholder = st.empty()
239
+ full_response = (
240
+ "⚠️ **_Gentle reminder: We strive for precision, but please double-check._**\n\n"
241
+ )
242
+ for chunk in answer.split():
243
+ full_response += chunk + " "
244
+ time.sleep(0.02) # Simulating typing
245
+ message_placeholder.markdown(full_response + " |", unsafe_allow_html=True)
246
+
247
+ st.session_state.messages.append({"role": "assistant", "content": answer})
248
+
249
+ except Exception as e:
250
+ # Handle unexpected errors during QA invocation
251
+ error_message = f"⚠️ **_Error: An unexpected issue occurred: {str(e)}._**"
252
+ st.error(error_message)
253
+ st.session_state.messages.append({"role": "assistant", "content": error_message})
254
+
255
+ # Reset button
256
+ if st.session_state.show_reset:
257
+ if st.button('πŸ—‘οΈ Reset All Chat', on_click=reset_conversation):
258
+ st.rerun() # Updated from st.experimental_rerun
259
+
260
+ footer()