Talha812 commited on
Commit
c025d72
·
verified ·
1 Parent(s): ed65326

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+ from groq import Groq
5
+
6
+ # LangChain components
7
+ from langchain_community.document_loaders import PyPDFLoader
8
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
9
+ from langchain_community.embeddings import HuggingFaceEmbeddings
10
+ from langchain_community.vectorstores import FAISS
11
+
12
+ # --- 1. SETUP & API KEYS ---
13
+ # Hugging Face uses os.getenv to read secrets
14
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
15
+ client = Groq(api_key=GROQ_API_KEY)
16
+
17
+ # Add your links here
18
+ GDRIVE_LINKS = [
19
+ "https://drive.google.com/file/d/12bS7b-Q3qdbnwCRcTynXjMj1IyKFzBJl/view?usp=sharing"
20
+ ]
21
+
22
+ def download_gdrive_pdf(url, output_path):
23
+ try:
24
+ file_id = url.split('/')[-2]
25
+ download_url = f'https://drive.google.com/uc?export=download&id={file_id}'
26
+ response = requests.get(download_url)
27
+ if response.status_code == 200:
28
+ with open(output_path, 'wb') as f:
29
+ f.write(response.content)
30
+ return True
31
+ except:
32
+ return False
33
+ return False
34
+
35
+ # --- 2. KNOWLEDGE BASE INITIALIZATION ---
36
+ print("Initializing Knowledge Base...")
37
+ all_chunks = []
38
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=150)
39
+
40
+ for i, link in enumerate(GDRIVE_LINKS):
41
+ filename = f"doc_{i}.pdf"
42
+ if download_gdrive_pdf(link, filename):
43
+ loader = PyPDFLoader(filename)
44
+ all_chunks.extend(text_splitter.split_documents(loader.load()))
45
+
46
+ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
47
+ vector_db = FAISS.from_documents(all_chunks, embeddings)
48
+ print("System Ready.")
49
+
50
+ # --- 3. RAG LOGIC ---
51
+ def respond(message, history):
52
+ docs = vector_db.similarity_search(message, k=5)
53
+ context = "\n\n".join([doc.page_content for doc in docs])
54
+
55
+ system_prompt = f"""
56
+ You are a professional Knowledge Assistant.
57
+ 1. Answer the question using ONLY the provided context.
58
+ 2. If the answer is not in the context, say: "I'm sorry, I couldn't find that in the documents."
59
+ 3. Be concise and factual.
60
+
61
+ CONTEXT:
62
+ {context}
63
+ """
64
+
65
+ chat_completion = client.chat.completions.create(
66
+ messages=[
67
+ {"role": "system", "content": system_prompt},
68
+ {"role": "user", "content": message},
69
+ ],
70
+ model="llama-3.3-70b-versatile",
71
+ temperature=0.1,
72
+ )
73
+ return chat_completion.choices[0].message.content
74
+
75
+ # --- 4. MODERN UI ---
76
+ custom_css = """
77
+ footer {visibility: hidden}
78
+ .gradio-container { background-color: #fcfcfc; }
79
+ #chatbot-container { border-radius: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
80
+ """
81
+
82
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), css=custom_css) as demo:
83
+ gr.Markdown("# 📑 Enterprise Knowledge Chat")
84
+ gr.Markdown("Ask questions based on your specialized document library.")
85
+
86
+ chatbot = gr.ChatInterface(
87
+ fn=respond,
88
+ chatbot=gr.Chatbot(height=550, elem_id="chatbot-container"),
89
+ textbox=gr.Textbox(placeholder="Ask me anything...", container=False, scale=7),
90
+ type="messages"
91
+ )
92
+
93
+ if __name__ == "__main__":
94
+ demo.launch()