Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import gdown
|
| 3 |
import time
|
| 4 |
import gradio as gr
|
| 5 |
-
|
| 6 |
|
| 7 |
# Modern Imports
|
| 8 |
from langchain_community.document_loaders import PyPDFLoader
|
|
@@ -17,15 +17,13 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 17 |
# ==========================================
|
| 18 |
# 1. SETUP & KEYS
|
| 19 |
# ==========================================
|
| 20 |
-
|
| 21 |
-
os.environ["GROQ_API_KEY"] = os.getenv('GROQ_API_KEY')
|
| 22 |
-
|
| 23 |
|
| 24 |
# --- UPDATE THIS LIST WITH ALL YOUR LINKS ---
|
| 25 |
links_to_process = [
|
| 26 |
"https://drive.google.com/file/d/1rb7AeJZrDNR-bq8Q9V4IvtzYZsDOvDH0/view?usp=sharing",
|
| 27 |
"https://drive.google.com/file/d/16PcJo_JaQHh1bx01lCAkc4QwQ6YnLb-K/view?usp=sharing"
|
| 28 |
-
#"https://drive.google.com/drive/folders/ANOTHER_FOLDER_ID"
|
| 29 |
]
|
| 30 |
|
| 31 |
output_dir = 'knowledge_base'
|
|
@@ -37,7 +35,7 @@ if not os.path.exists(output_dir):
|
|
| 37 |
# ==========================================
|
| 38 |
def build_vector_db(links):
|
| 39 |
print(f"π₯ Starting synchronization for {len(links)} sources...")
|
| 40 |
-
|
| 41 |
for link in links:
|
| 42 |
try:
|
| 43 |
if "/folders/" in link:
|
|
@@ -47,7 +45,7 @@ def build_vector_db(links):
|
|
| 47 |
print(f"π Syncing Individual File: {link}")
|
| 48 |
# Use output_dir + "/" to ensure it saves into the folder
|
| 49 |
gdown.download(url=link, output=output_dir + "/", quiet=True)
|
| 50 |
-
|
| 51 |
time.sleep(1) # Small pause to respect Drive rate limits
|
| 52 |
except Exception as e:
|
| 53 |
print(f"β οΈ Skip Link: Could not download {link}. Error: {e}")
|
|
@@ -63,22 +61,22 @@ def build_vector_db(links):
|
|
| 63 |
all_docs.extend(loader.load())
|
| 64 |
except Exception as e:
|
| 65 |
print(f"β Error loading {filename}: {e}")
|
| 66 |
-
|
| 67 |
if not all_docs:
|
| 68 |
raise ValueError("No PDF documents found! Ensure links are set to 'Anyone with the link'.")
|
| 69 |
|
| 70 |
# Chunking & Embeddings
|
| 71 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
| 72 |
chunks = text_splitter.split_documents(all_docs)
|
| 73 |
-
|
| 74 |
print(f"π§ Creating embeddings for {len(chunks)} text chunks...")
|
| 75 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 76 |
-
|
| 77 |
vector_db = FAISS.from_documents(chunks, embeddings)
|
| 78 |
print("β
Multi-Source Vector Database Created Successfully!")
|
| 79 |
return vector_db
|
| 80 |
|
| 81 |
-
# Initialize
|
| 82 |
vector_store = build_vector_db(links_to_process)
|
| 83 |
retriever = vector_store.as_retriever(search_kwargs={"k": 3})
|
| 84 |
|
|
@@ -106,68 +104,37 @@ rag_chain = (
|
|
| 106 |
# ==========================================
|
| 107 |
# 4. PROFESSIONAL FRONTEND (GRADIO BLOCKS)
|
| 108 |
# ==========================================
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# ==========================================
|
| 112 |
-
# 4. PROFESSIONAL FRONTEND (STYLING FIXED)
|
| 113 |
-
# ==========================================
|
| 114 |
-
|
| 115 |
-
# Improved CSS for high-contrast visibility
|
| 116 |
custom_css = """
|
| 117 |
#main-container { max-width: 900px; margin: auto; padding: 20px; }
|
| 118 |
.header-text { text-align: center; color: #1e293b; margin-bottom: 2px; }
|
| 119 |
-
|
| 120 |
-
/* The result box styling */
|
| 121 |
-
.report-box {
|
| 122 |
-
background-color: #ffffff !important;
|
| 123 |
-
border-radius: 12px !important;
|
| 124 |
-
border: 2px solid #4f46e5 !important;
|
| 125 |
-
padding: 20px !important;
|
| 126 |
-
min-height: 250px !important;
|
| 127 |
-
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1) !important;
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
/* Force text inside the result box to be Bold Black */
|
| 131 |
-
.report-box p, .report-box span, .report-box div {
|
| 132 |
-
color: #000000 !important;
|
| 133 |
-
font-weight: 500 !important;
|
| 134 |
-
font-size: 1.1rem !important;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
/* Ensure Markdown headers (# Header) are extra bold and dark */
|
| 138 |
-
.report-box h1, .report-box h2, .report-box h3 {
|
| 139 |
-
color: #1e1b4b !important;
|
| 140 |
-
font-weight: 800 !important;
|
| 141 |
-
border-bottom: 1px solid #e2e8f0;
|
| 142 |
-
}
|
| 143 |
"""
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), css=custom_css) as demo:
|
| 146 |
with gr.Column(elem_id="main-container"):
|
| 147 |
-
gr.Markdown("# ποΈ
|
| 148 |
-
gr.Markdown("<p style='text-align: center;'>
|
| 149 |
gr.HTML("<hr>")
|
| 150 |
-
|
| 151 |
-
user_input = gr.Textbox(
|
| 152 |
-
|
| 153 |
-
placeholder="Ask a question about Assembly or Data Structures...",
|
| 154 |
-
lines=3
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
with gr.Row():
|
| 158 |
submit_btn = gr.Button("ANALYZE DATA", variant="primary", scale=2)
|
| 159 |
-
clear_btn = gr.ClearButton([user_input], value="RESET", scale=1)
|
| 160 |
|
| 161 |
gr.Markdown("### π Intelligence Report")
|
| 162 |
-
|
| 163 |
-
# We apply 'report-box' here to ensure the Markdown inherits the high-contrast CSS
|
| 164 |
with gr.Column(elem_classes="report-box"):
|
| 165 |
-
output_display = gr.Markdown(
|
| 166 |
-
value="_Awaiting input... Results will appear here in high-contrast black text._"
|
| 167 |
-
)
|
| 168 |
|
| 169 |
submit_btn.click(fn=process_query, inputs=user_input, outputs=output_display)
|
| 170 |
user_input.submit(fn=process_query, inputs=user_input, outputs=output_display)
|
| 171 |
|
| 172 |
-
# Launch
|
| 173 |
demo.launch(share=True)
|
|
|
|
| 2 |
import gdown
|
| 3 |
import time
|
| 4 |
import gradio as gr
|
| 5 |
+
from google.colab import userdata
|
| 6 |
|
| 7 |
# Modern Imports
|
| 8 |
from langchain_community.document_loaders import PyPDFLoader
|
|
|
|
| 17 |
# ==========================================
|
| 18 |
# 1. SETUP & KEYS
|
| 19 |
# ==========================================
|
| 20 |
+
os.environ["GROQ_API_KEY"] = userdata.get('ragapikey')
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# --- UPDATE THIS LIST WITH ALL YOUR LINKS ---
|
| 23 |
links_to_process = [
|
| 24 |
"https://drive.google.com/file/d/1rb7AeJZrDNR-bq8Q9V4IvtzYZsDOvDH0/view?usp=sharing",
|
| 25 |
"https://drive.google.com/file/d/16PcJo_JaQHh1bx01lCAkc4QwQ6YnLb-K/view?usp=sharing"
|
| 26 |
+
#"https://drive.google.com/drive/folders/ANOTHER_FOLDER_ID"
|
| 27 |
]
|
| 28 |
|
| 29 |
output_dir = 'knowledge_base'
|
|
|
|
| 35 |
# ==========================================
|
| 36 |
def build_vector_db(links):
|
| 37 |
print(f"π₯ Starting synchronization for {len(links)} sources...")
|
| 38 |
+
|
| 39 |
for link in links:
|
| 40 |
try:
|
| 41 |
if "/folders/" in link:
|
|
|
|
| 45 |
print(f"π Syncing Individual File: {link}")
|
| 46 |
# Use output_dir + "/" to ensure it saves into the folder
|
| 47 |
gdown.download(url=link, output=output_dir + "/", quiet=True)
|
| 48 |
+
|
| 49 |
time.sleep(1) # Small pause to respect Drive rate limits
|
| 50 |
except Exception as e:
|
| 51 |
print(f"β οΈ Skip Link: Could not download {link}. Error: {e}")
|
|
|
|
| 61 |
all_docs.extend(loader.load())
|
| 62 |
except Exception as e:
|
| 63 |
print(f"β Error loading {filename}: {e}")
|
| 64 |
+
|
| 65 |
if not all_docs:
|
| 66 |
raise ValueError("No PDF documents found! Ensure links are set to 'Anyone with the link'.")
|
| 67 |
|
| 68 |
# Chunking & Embeddings
|
| 69 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
| 70 |
chunks = text_splitter.split_documents(all_docs)
|
| 71 |
+
|
| 72 |
print(f"π§ Creating embeddings for {len(chunks)} text chunks...")
|
| 73 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 74 |
+
|
| 75 |
vector_db = FAISS.from_documents(chunks, embeddings)
|
| 76 |
print("β
Multi-Source Vector Database Created Successfully!")
|
| 77 |
return vector_db
|
| 78 |
|
| 79 |
+
# Initialize
|
| 80 |
vector_store = build_vector_db(links_to_process)
|
| 81 |
retriever = vector_store.as_retriever(search_kwargs={"k": 3})
|
| 82 |
|
|
|
|
| 104 |
# ==========================================
|
| 105 |
# 4. PROFESSIONAL FRONTEND (GRADIO BLOCKS)
|
| 106 |
# ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
custom_css = """
|
| 108 |
#main-container { max-width: 900px; margin: auto; padding: 20px; }
|
| 109 |
.header-text { text-align: center; color: #1e293b; margin-bottom: 2px; }
|
| 110 |
+
.report-box { background-color: #ffffff; border-radius: 8px; border: 1px solid #e2e8f0; padding: 15px; min-height: 200px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
"""
|
| 112 |
|
| 113 |
+
def process_query(query):
|
| 114 |
+
if not query.strip():
|
| 115 |
+
return "### β οΈ System Note\n*Please enter a strategic inquiry to begin analysis.*"
|
| 116 |
+
try:
|
| 117 |
+
return rag_chain.invoke(query)
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"### β Error\nAn error occurred: {str(e)}"
|
| 120 |
+
|
| 121 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), css=custom_css) as demo:
|
| 122 |
with gr.Column(elem_id="main-container"):
|
| 123 |
+
gr.Markdown("# ποΈ Enterprise Knowledge Engine", elem_classes="header-text")
|
| 124 |
+
gr.Markdown("<p style='text-align: center;'>Multi-Source Document Synthesis via Groq & FAISS</p>")
|
| 125 |
gr.HTML("<hr>")
|
| 126 |
+
|
| 127 |
+
user_input = gr.Textbox(label="Strategic Inquiry", placeholder="Ask a question about the collected knowledge base...", lines=3)
|
| 128 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
with gr.Row():
|
| 130 |
submit_btn = gr.Button("ANALYZE DATA", variant="primary", scale=2)
|
| 131 |
+
clear_btn = gr.ClearButton([user_input], value="RESET DASHBOARD", scale=1)
|
| 132 |
|
| 133 |
gr.Markdown("### π Intelligence Report")
|
|
|
|
|
|
|
| 134 |
with gr.Column(elem_classes="report-box"):
|
| 135 |
+
output_display = gr.Markdown(value="_Awaiting input..._")
|
|
|
|
|
|
|
| 136 |
|
| 137 |
submit_btn.click(fn=process_query, inputs=user_input, outputs=output_display)
|
| 138 |
user_input.submit(fn=process_query, inputs=user_input, outputs=output_display)
|
| 139 |
|
|
|
|
| 140 |
demo.launch(share=True)
|