Ahmad-01 commited on
Commit
ba765de
Β·
verified Β·
1 Parent(s): df3b430

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import faiss
4
+ import numpy as np
5
+ import gdown
6
+
7
+ from groq import Groq
8
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
9
+ from langchain_community.document_loaders import PyPDFLoader
10
+ from langchain_huggingface import HuggingFaceEmbeddings
11
+
12
+
13
+ # ==============================
14
+ # πŸ” Load Groq API Key Securely
15
+ # ==============================
16
+ groq_api_key = os.environ.get("GROQ_API_KEY")
17
+ client = Groq(api_key=groq_api_key)
18
+
19
+
20
+ # ==============================
21
+ # πŸ“₯ Download Knowledge Base
22
+ # ==============================
23
+ FILE_ID = "1ppfRoaQik3h1Gr9A15xSOLGVpNQtm8eH"
24
+ DOWNLOAD_URL = f"https://drive.google.com/uc?id={FILE_ID}"
25
+ PDF_PATH = "knowledge_base.pdf"
26
+
27
+ if not os.path.exists(PDF_PATH):
28
+ gdown.download(DOWNLOAD_URL, PDF_PATH, quiet=False)
29
+
30
+
31
+ # ==============================
32
+ # πŸ“š Create Vector Database
33
+ # ==============================
34
+ embedding_model = HuggingFaceEmbeddings(
35
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
36
+ )
37
+
38
+ loader = PyPDFLoader(PDF_PATH)
39
+ documents = loader.load()
40
+
41
+ text_splitter = RecursiveCharacterTextSplitter(
42
+ chunk_size=600,
43
+ chunk_overlap=150
44
+ )
45
+
46
+ chunks = text_splitter.split_documents(documents)
47
+ texts = [chunk.page_content for chunk in chunks]
48
+
49
+ embeddings = embedding_model.embed_documents(texts)
50
+ embeddings = np.array(embeddings).astype("float32")
51
+
52
+ dimension = embeddings.shape[1]
53
+ vector_store = faiss.IndexFlatL2(dimension)
54
+ vector_store.add(embeddings)
55
+
56
+ print("βœ… Knowledge Base Loaded Successfully")
57
+
58
+
59
+ # ==============================
60
+ # πŸ€– RAG Function
61
+ # ==============================
62
+ def ask_question(question):
63
+ question_embedding = embedding_model.embed_query(question)
64
+ question_embedding = np.array([question_embedding]).astype("float32")
65
+
66
+ distances, indices = vector_store.search(question_embedding, k=4)
67
+
68
+ retrieved_texts = [texts[i] for i in indices[0]]
69
+ context = "\n\n".join(retrieved_texts)
70
+
71
+ prompt = f"""
72
+ You are an expert assistant.
73
+
74
+ Use ONLY the context below to answer clearly.
75
+ Format with headings and bullet points if needed.
76
+
77
+ CONTEXT:
78
+ {context}
79
+
80
+ QUESTION:
81
+ {question}
82
+ """
83
+
84
+ chat_completion = client.chat.completions.create(
85
+ messages=[{"role": "user", "content": prompt}],
86
+ model="llama-3.3-70b-versatile",
87
+ )
88
+
89
+ answer = chat_completion.choices[0].message.content
90
+
91
+ return f"""
92
+ ## πŸ“Œ Answer
93
+
94
+ {answer}
95
+ """
96
+
97
+
98
+ # ==============================
99
+ # 🎨 Professional Yellow UI
100
+ # ==============================
101
+ custom_css = """
102
+ body {
103
+ background-color: #ffffff;
104
+ font-family: Arial, sans-serif;
105
+ }
106
+
107
+ .gradio-container {
108
+ background-color: #fffbea;
109
+ border-radius: 15px;
110
+ padding: 25px;
111
+ }
112
+
113
+ button {
114
+ background-color: #ffc107 !important;
115
+ color: black !important;
116
+ font-weight: bold !important;
117
+ border-radius: 10px !important;
118
+ }
119
+
120
+ textarea {
121
+ border-radius: 10px !important;
122
+ }
123
+
124
+ .answer-box {
125
+ background-color: white;
126
+ border: 2px solid #ffc107;
127
+ padding: 20px;
128
+ border-radius: 12px;
129
+ min-height: 250px;
130
+ }
131
+ """
132
+
133
+
134
+ with gr.Blocks(css=custom_css) as app:
135
+
136
+ gr.Markdown(
137
+ """
138
+ # 🟑 KnowledgeBase AI Assistant
139
+ ### Ask questions from my curated knowledge base
140
+ """
141
+ )
142
+
143
+ question_input = gr.Textbox(
144
+ label="Enter Your Question",
145
+ placeholder="Ask something from the knowledge base..."
146
+ )
147
+
148
+ ask_button = gr.Button("Get Answer")
149
+
150
+ answer_output = gr.Markdown(elem_classes="answer-box")
151
+
152
+ ask_button.click(ask_question, inputs=question_input, outputs=answer_output)
153
+
154
+ app.launch()