Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
# π₯
|
| 20 |
-
#
|
| 21 |
# ==============================
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
pdf_folder = "documents"
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 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") #
|
| 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
|
| 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")
|