Shakeel1979 commited on
Commit
17028d5
·
verified ·
1 Parent(s): 349809a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -42
app.py CHANGED
@@ -8,39 +8,29 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
8
  from langchain_community.vectorstores import FAISS
9
  from groq import Groq
10
  import streamlit as st
11
- from googleapiclient.discovery import build
12
- from googleapiclient.http import MediaIoBaseDownload
13
- from google.oauth2.service_account import Credentials
14
 
15
  # Initialize Groq client
16
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
17
 
18
- # Google Drive API authentication and setup
19
- SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
20
- SERVICE_ACCOUNT_FILE = 'tonal-vector-446915-a1-013785a5b8ac.json'
21
- credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
22
- service = build('drive', 'v3', credentials=credentials)
23
-
24
- # Predefined Google Drive folder link
25
- folder_id = "1lYWZwxr-4IBhxKV4siVO2nKCwbO-RMO7"
26
-
27
- # Function to list files in the folder
28
- def list_files_in_folder(folder_id):
29
- query = f"'{folder_id}' in parents"
30
- results = service.files().list(q=query).execute()
31
- files = results.get('files', [])
32
- return files
33
-
34
- # Function to download PDF from Google Drive by file ID
35
- def download_pdf_from_drive(file_id):
36
- request = service.files().get_media(fileId=file_id)
37
- fh = BytesIO()
38
- downloader = MediaIoBaseDownload(fh, request)
39
- done = False
40
- while done is False:
41
- status, done = downloader.next_chunk()
42
- fh.seek(0)
43
- return fh
44
 
45
  # Function to extract text from a PDF
46
  def extract_text_from_pdf(pdf_stream):
@@ -65,9 +55,11 @@ def create_embeddings_and_store(chunks):
65
 
66
  # Function to query the vector database and interact with Groq
67
  def query_vector_db(query, vector_db):
 
68
  docs = vector_db.similarity_search(query, k=3)
69
  context = "\n".join([doc.page_content for doc in docs])
70
 
 
71
  chat_completion = client.chat.completions.create(
72
  messages=[
73
  {"role": "system", "content": f"Use the following context:\n{context}"},
@@ -78,24 +70,30 @@ def query_vector_db(query, vector_db):
78
  return chat_completion.choices[0].message.content
79
 
80
  # Streamlit app
81
- st.title("RAG-based ChatBot (Advanced Triangle Solver)")
82
 
83
  st.write("Processing the Data links...")
84
 
85
  all_chunks = []
86
 
87
- # Process all files in the Google Drive folder
88
- try:
89
- files = list_files_in_folder(folder_id)
90
- for file in files:
91
- if file['mimeType'] == 'application/pdf': # Only process PDF files
92
- st.write(f"Processing file: {file['name']}")
93
- pdf_stream = download_pdf_from_drive(file['id'])
94
- text = extract_text_from_pdf(pdf_stream)
95
- chunks = chunk_text(text)
96
- all_chunks.extend(chunks)
97
- except Exception as e:
98
- st.write(f"Error processing folder: {e}")
 
 
 
 
 
 
99
 
100
  if all_chunks:
101
  # Generate embeddings and store in FAISS
 
8
  from langchain_community.vectorstores import FAISS
9
  from groq import Groq
10
  import streamlit as st
 
 
 
11
 
12
  # Initialize Groq client
13
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
14
 
15
+ # if client:
16
+ # st.write(f"API KEY FOUND!!!{client}")
17
+ # else:
18
+ # st.write("API KEY NOT FOUND")
19
+
20
+ # Predefined list of Google Drive links
21
+ drive_links = [
22
+ "https://drive.google.com/file/d/1xswnMcJH4KRWFICjUpszYBQCsPTykZp0/view?usp=sharing"
23
+ ]
24
+
25
+ # Function to download PDF from Google Drive
26
+ def download_pdf_from_drive(drive_link):
27
+ file_id = drive_link.split('/d/')[1].split('/')[0]
28
+ download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
29
+ response = requests.get(download_url)
30
+ if response.status_code == 200:
31
+ return BytesIO(response.content)
32
+ else:
33
+ raise Exception("Failed to download the PDF file from Google Drive.")
 
 
 
 
 
 
 
34
 
35
  # Function to extract text from a PDF
36
  def extract_text_from_pdf(pdf_stream):
 
55
 
56
  # Function to query the vector database and interact with Groq
57
  def query_vector_db(query, vector_db):
58
+ # Retrieve relevant documents
59
  docs = vector_db.similarity_search(query, k=3)
60
  context = "\n".join([doc.page_content for doc in docs])
61
 
62
+ # Interact with Groq API
63
  chat_completion = client.chat.completions.create(
64
  messages=[
65
  {"role": "system", "content": f"Use the following context:\n{context}"},
 
70
  return chat_completion.choices[0].message.content
71
 
72
  # Streamlit app
73
+ st.title("RAG-Based ChatBot (Already having Document)")
74
 
75
  st.write("Processing the Data links...")
76
 
77
  all_chunks = []
78
 
79
+ # Process each predefined Google Drive link
80
+ for link in drive_links:
81
+ try:
82
+ # st.write(f"Processing link: {link}")
83
+ # Download PDF
84
+ pdf_stream = download_pdf_from_drive(link)
85
+ # st.write("PDF Downloaded Successfully!")
86
+
87
+ # Extract text
88
+ text = extract_text_from_pdf(pdf_stream)
89
+ # st.write("PDF Text Extracted Successfully!")
90
+
91
+ # Chunk text
92
+ chunks = chunk_text(text)
93
+ # st.write(f"Created {len(chunks)} text chunks.")
94
+ all_chunks.extend(chunks)
95
+ except Exception as e:
96
+ st.write(f"Error processing link {link}: {e}")
97
 
98
  if all_chunks:
99
  # Generate embeddings and store in FAISS