bkbilal09 commited on
Commit
01f6ace
·
verified ·
1 Parent(s): 09b9ebf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import warnings
4
+ import gradio as gr
5
+ from groq import Groq
6
+ from langchain_community.document_loaders import PyPDFLoader
7
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
8
+ from langchain_huggingface import HuggingFaceEmbeddings
9
+ from langchain_community.vectorstores import FAISS
10
+
11
+ # Suppress technical warnings for a clean logs
12
+ warnings.filterwarnings("ignore")
13
+
14
+ # --- 1. CONFIGURATION & SECRETS ---
15
+ # On Hugging Face, set 'MY_GROQ_SECRET' in Settings > Variables and Secrets
16
+ GROQ_API_KEY = os.environ.get("MY_GROQ_SECRET")
17
+ client = Groq(api_key=GROQ_API_KEY)
18
+
19
+ # YOUR HIDDEN LINKS (Never shown in UI)
20
+ GDRIVE_LINKS = [
21
+ "https://drive.google.com/file/d/10D3uJqBYG9gMWsNHcpTW4I6BKmA2otfH/view?usp=sharing"
22
+ ]
23
+
24
+ # --- 2. KNOWLEDGE BASE INITIALIZATION ---
25
+ def download_gdrive_pdf(url, output_path):
26
+ try:
27
+ file_id = url.split('/')[-2]
28
+ download_url = f'https://drive.google.com/uc?export=download&id={file_id}'
29
+ response = requests.get(download_url)
30
+ if response.status_code == 200:
31
+ with open(output_path, 'wb') as f:
32
+ f.write(response.content)
33
+ return True
34
+ except:
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
+
42
+ for i, link in enumerate(GDRIVE_LINKS):
43
+ filename = f"doc_{i}.pdf"
44
+ if download_gdrive_pdf(link, filename):
45
+ try:
46
+ loader = PyPDFLoader(filename)
47
+ all_chunks.extend(text_splitter.split_documents(loader.load()))
48
+ finally:
49
+ if os.path.exists(filename):
50
+ os.remove(filename)
51
+
52
+ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
53
+ vector_db = FAISS.from_documents(all_chunks, embeddings)
54
+
55
+ # --- 3. STRICT RAG LOGIC ---
56
+ def respond(message, history):
57
+ # Retrieve relevant snippets
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. Do not mention the context or the technical nature of the search.
67
+
68
+ CONTEXT:
69
+ {context}
70
+ """
71
+
72
+ chat_completion = client.chat.completions.create(
73
+ messages=[
74
+ {"role": "system", "content": system_prompt},
75
+ {"role": "user", "content": message}
76
+ ],
77
+ model="llama-3.3-70b-versatile",
78
+ temperature=0.1,
79
+ )
80
+ return chat_completion.choices[0].message.content
81
+
82
+ # --- 4. ATTRACTIVE MODERN UI ---
83
+ custom_css = """
84
+ body { background-color: #0f172a; }
85
+ .gradio-container { max-width: 850px !important; margin: auto; padding-top: 50px; }
86
+ #title-text { text-align: center; color: #38bdf8; font-weight: 800; margin-bottom: 5px; }
87
+ #desc-text { text-align: center; color: #94a3b8; margin-bottom: 25px; }
88
+ .chat-container { border-radius: 20px !important; border: 1px solid #334155 !important; background: #1e293b !important; box-shadow: 0 20px 50px rgba(0,0,0,0.4); }
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.Default(primary_hue="sky"), css=custom_css) as demo:
94
+ gr.HTML("<h1 id='title-text'>🌀 DocuVortex</h1>")
95
+ # REPLACE "User's Research AI" with your own name here!
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
+ chatbot=gr.Chatbot(height=550, bubble_full_width=False, show_label=False),
102
+ textbox=gr.Textbox(placeholder="Ask a question about the document...", container=False, scale=7),
103
+ submit_btn=gr.Button("Ask AI", variant="primary", elem_classes="primary-btn"),
104
+ retry_btn=None,
105
+ undo_btn=None,
106
+ clear_btn=gr.Button("New Chat", variant="secondary")
107
+ )
108
+
109
+ if __name__ == "__main__":
110
+ demo.launch()