SohaAyub commited on
Commit
4b34e1e
Β·
verified Β·
1 Parent(s): c18ce9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import gradio as gr
3
  import faiss
4
  import numpy as np
@@ -8,7 +9,6 @@ from groq import Groq
8
 
9
  # ==============================
10
  # πŸ” SET YOUR GROQ API KEY
11
- # In HuggingFace Spaces, go to Settings β†’ Secrets β†’ Add "GROQ_RAG_API"
12
  # ==============================
13
  api_key = os.environ.get("GROQ_RAG_API")
14
  if not api_key:
@@ -16,16 +16,29 @@ if not api_key:
16
  client = Groq(api_key=api_key)
17
 
18
  # ==============================
19
- # πŸ“₯ PDF DOCUMENTS
20
- # Put your PDFs directly in a folder called "documents" in your repo
21
  # ==============================
 
 
 
 
22
  pdf_folder = "documents"
23
- if not os.path.exists(pdf_folder):
24
- os.makedirs(pdf_folder)
 
 
 
 
 
 
 
25
 
26
- # Load PDFs from the folder
27
- pdf_files = [os.path.join(pdf_folder, f) for f in os.listdir(pdf_folder) if f.endswith(".pdf")]
28
 
 
 
 
29
  documents = []
30
  for pdf in pdf_files:
31
  reader = PdfReader(pdf)
@@ -53,7 +66,7 @@ print("Total chunks:", len(chunks))
53
  # ==============================
54
  model = SentenceTransformer("all-MiniLM-L6-v2")
55
  embeddings = model.encode(chunks, convert_to_numpy=True)
56
- embeddings = embeddings.astype("float32") # important for FAISS
57
 
58
  # ==============================
59
  # πŸ—„οΈ FAISS VECTOR INDEX
@@ -105,7 +118,7 @@ def chat_fn(message, history):
105
 
106
  with gr.Blocks() as demo:
107
  gr.Markdown("# πŸ“š RAG Assistant")
108
- gr.Markdown("Ask questions from your uploaded PDFs.")
109
  chatbot = gr.Chatbot()
110
  msg = gr.Textbox(placeholder="Ask your question here...")
111
  btn = gr.Button("Send")
 
1
  import os
2
+ import gdown
3
  import gradio as gr
4
  import faiss
5
  import numpy as np
 
9
 
10
  # ==============================
11
  # πŸ” SET YOUR GROQ API KEY
 
12
  # ==============================
13
  api_key = os.environ.get("GROQ_RAG_API")
14
  if not api_key:
 
16
  client = Groq(api_key=api_key)
17
 
18
  # ==============================
19
+ # πŸ“₯ GOOGLE DRIVE LINKS
20
+ # Add as many as you want
21
  # ==============================
22
+ drive_links = [
23
+ "https://drive.google.com/file/d/1pKlbI7XbhlDc-hOBJEkLpYteg6mkeY0X/view?usp=sharing"
24
+ ]
25
+
26
  pdf_folder = "documents"
27
+ os.makedirs(pdf_folder, exist_ok=True)
28
+
29
+ def download_from_drive(link):
30
+ file_id = link.split("/d/")[1].split("/")[0]
31
+ output_path = os.path.join(pdf_folder, f"{file_id}.pdf")
32
+ if not os.path.exists(output_path):
33
+ download_url = f"https://drive.google.com/uc?id={file_id}"
34
+ gdown.download(download_url, output_path, quiet=False)
35
+ return output_path
36
 
37
+ pdf_files = [download_from_drive(link) for link in drive_links]
 
38
 
39
+ # ==============================
40
+ # πŸ“– LOAD PDF TEXT
41
+ # ==============================
42
  documents = []
43
  for pdf in pdf_files:
44
  reader = PdfReader(pdf)
 
66
  # ==============================
67
  model = SentenceTransformer("all-MiniLM-L6-v2")
68
  embeddings = model.encode(chunks, convert_to_numpy=True)
69
+ embeddings = embeddings.astype("float32") # for FAISS
70
 
71
  # ==============================
72
  # πŸ—„οΈ FAISS VECTOR INDEX
 
118
 
119
  with gr.Blocks() as demo:
120
  gr.Markdown("# πŸ“š RAG Assistant")
121
+ gr.Markdown("Ask questions from your Google Drive PDFs.")
122
  chatbot = gr.Chatbot()
123
  msg = gr.Textbox(placeholder="Ask your question here...")
124
  btn = gr.Button("Send")