Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,15 @@ from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
| 8 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 9 |
from langchain_community.vectorstores import FAISS
|
| 10 |
|
| 11 |
-
# Suppress technical warnings
|
| 12 |
warnings.filterwarnings("ignore")
|
| 13 |
|
| 14 |
# --- 1. CONFIGURATION & SECRETS ---
|
| 15 |
-
#
|
| 16 |
GROQ_API_KEY = os.environ.get("MY_GROQ_SECRET")
|
| 17 |
client = Groq(api_key=GROQ_API_KEY)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
GDRIVE_LINKS = [
|
| 21 |
"https://drive.google.com/file/d/10D3uJqBYG9gMWsNHcpTW4I6BKmA2otfH/view?usp=sharing"
|
| 22 |
]
|
|
@@ -35,7 +35,6 @@ def download_gdrive_pdf(url, output_path):
|
|
| 35 |
return False
|
| 36 |
return False
|
| 37 |
|
| 38 |
-
# Initialize the vector database on startup
|
| 39 |
all_chunks = []
|
| 40 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
|
| 41 |
|
|
@@ -52,18 +51,17 @@ for i, link in enumerate(GDRIVE_LINKS):
|
|
| 52 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 53 |
vector_db = FAISS.from_documents(all_chunks, embeddings)
|
| 54 |
|
| 55 |
-
# --- 3.
|
| 56 |
def respond(message, history):
|
| 57 |
-
#
|
| 58 |
docs = vector_db.similarity_search(message, k=5)
|
| 59 |
context = "\n\n".join([doc.page_content for doc in docs])
|
| 60 |
|
| 61 |
-
# Strict instructions: No outside knowledge allowed
|
| 62 |
system_prompt = f"""
|
| 63 |
You are a professional Knowledge Assistant.
|
| 64 |
-
1. Answer ONLY using the context provided.
|
| 65 |
2. If the answer is NOT in the context, say: "Answer not found in provided documents."
|
| 66 |
-
3.
|
| 67 |
|
| 68 |
CONTEXT:
|
| 69 |
{context}
|
|
@@ -79,31 +77,30 @@ def respond(message, history):
|
|
| 79 |
)
|
| 80 |
return chat_completion.choices[0].message.content
|
| 81 |
|
| 82 |
-
# --- 4.
|
| 83 |
custom_css = """
|
| 84 |
body { background-color: #0f172a; }
|
| 85 |
-
.gradio-container { max-width: 850px !important; margin: auto; padding-top:
|
| 86 |
-
#title-text { text-align: center; color: #38bdf8; font-weight: 800;
|
| 87 |
-
#desc-text { text-align: center; color: #94a3b8; margin-bottom:
|
| 88 |
-
.chat-container { border-radius:
|
| 89 |
-
.primary-btn { background: linear-gradient(135deg, #38bdf8, #818cf8) !important; border: none !important; color: white !important; }
|
| 90 |
footer { display: none !important; }
|
| 91 |
"""
|
| 92 |
|
| 93 |
-
with gr.Blocks(theme=gr.themes.
|
| 94 |
gr.HTML("<h1 id='title-text'>🌀 DocuVortex</h1>")
|
| 95 |
-
|
| 96 |
-
gr.HTML("<p id='desc-text'>User's Research AI: Strict Document Knowledge Base</p>")
|
| 97 |
|
| 98 |
with gr.Column(elem_id="chat-container"):
|
| 99 |
gr.ChatInterface(
|
| 100 |
fn=respond,
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
retry_btn=None,
|
| 105 |
undo_btn=None,
|
| 106 |
-
clear_btn=gr.Button("
|
| 107 |
)
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
|
|
|
| 8 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 9 |
from langchain_community.vectorstores import FAISS
|
| 10 |
|
| 11 |
+
# Suppress technical warnings
|
| 12 |
warnings.filterwarnings("ignore")
|
| 13 |
|
| 14 |
# --- 1. CONFIGURATION & SECRETS ---
|
| 15 |
+
# Ensure 'MY_GROQ_SECRET' is added in Hugging Face Settings > Secrets
|
| 16 |
GROQ_API_KEY = os.environ.get("MY_GROQ_SECRET")
|
| 17 |
client = Groq(api_key=GROQ_API_KEY)
|
| 18 |
|
| 19 |
+
# HIDDEN DATA SOURCE
|
| 20 |
GDRIVE_LINKS = [
|
| 21 |
"https://drive.google.com/file/d/10D3uJqBYG9gMWsNHcpTW4I6BKmA2otfH/view?usp=sharing"
|
| 22 |
]
|
|
|
|
| 35 |
return False
|
| 36 |
return False
|
| 37 |
|
|
|
|
| 38 |
all_chunks = []
|
| 39 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
|
| 40 |
|
|
|
|
| 51 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 52 |
vector_db = FAISS.from_documents(all_chunks, embeddings)
|
| 53 |
|
| 54 |
+
# --- 3. RAG LOGIC ---
|
| 55 |
def respond(message, history):
|
| 56 |
+
# Find context snippets
|
| 57 |
docs = vector_db.similarity_search(message, k=5)
|
| 58 |
context = "\n\n".join([doc.page_content for doc in docs])
|
| 59 |
|
|
|
|
| 60 |
system_prompt = f"""
|
| 61 |
You are a professional Knowledge Assistant.
|
| 62 |
+
1. Answer ONLY using the context provided below.
|
| 63 |
2. If the answer is NOT in the context, say: "Answer not found in provided documents."
|
| 64 |
+
3. Keep answers direct and factual.
|
| 65 |
|
| 66 |
CONTEXT:
|
| 67 |
{context}
|
|
|
|
| 77 |
)
|
| 78 |
return chat_completion.choices[0].message.content
|
| 79 |
|
| 80 |
+
# --- 4. MODERN UI DESIGN ---
|
| 81 |
custom_css = """
|
| 82 |
body { background-color: #0f172a; }
|
| 83 |
+
.gradio-container { max-width: 850px !important; margin: auto; padding-top: 30px; }
|
| 84 |
+
#title-text { text-align: center; color: #38bdf8; font-weight: 800; }
|
| 85 |
+
#desc-text { text-align: center; color: #94a3b8; margin-bottom: 20px; }
|
| 86 |
+
.chat-container { border-radius: 15px !important; border: 1px solid #334155 !important; background: #1e293b !important; }
|
|
|
|
| 87 |
footer { display: none !important; }
|
| 88 |
"""
|
| 89 |
|
| 90 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky"), css=custom_css) as demo:
|
| 91 |
gr.HTML("<h1 id='title-text'>🌀 DocuVortex</h1>")
|
| 92 |
+
gr.HTML("<p id='desc-text'>Bilal's Research AI: Strict Document Knowledge Base</p>")
|
|
|
|
| 93 |
|
| 94 |
with gr.Column(elem_id="chat-container"):
|
| 95 |
gr.ChatInterface(
|
| 96 |
fn=respond,
|
| 97 |
+
# FIXED: Removed 'bubble_full_width' which caused the crash
|
| 98 |
+
chatbot=gr.Chatbot(height=550, show_label=False),
|
| 99 |
+
textbox=gr.Textbox(placeholder="Ask Bilal's AI a question...", container=False, scale=7),
|
| 100 |
+
submit_btn=gr.Button("Ask AI", variant="primary"),
|
| 101 |
retry_btn=None,
|
| 102 |
undo_btn=None,
|
| 103 |
+
clear_btn=gr.Button("Refresh Chat", variant="secondary")
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|