File size: 4,060 Bytes
0392514
 
 
 
0ec4d3d
0392514
 
 
 
 
 
3013d37
0392514
 
551b2f7
f2e81fb
0392514
 
 
 
 
 
 
 
 
 
 
 
 
 
3013d37
0392514
 
 
 
 
 
 
3013d37
0392514
 
 
 
 
3013d37
 
 
 
0392514
 
 
3013d37
0392514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3013d37
0392514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3013d37
0392514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3013d37
0392514
 
 
 
 
 
 
 
 
 
 
 
 
3013d37
0392514
 
 
 
 
 
3013d37
0392514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3013d37
0392514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os
import gradio as gr
from groq import Groq

from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings


# ------------------------------
# API KEY
# ------------------------------

# client = Groq(api_key=os.environ.get("GROQ_API"))
client = Groq(api_key=os.environ.get("GROQ_API"))

vector_db = None


# ------------------------------
# EMBEDDING MODEL
# ------------------------------

embedding_model = HuggingFaceEmbeddings(
    model_name="sentence-transformers/all-MiniLM-L6-v2"
)


# ------------------------------
# BUILD KNOWLEDGE BASE
# ------------------------------

def build_knowledge_base(files):

    global vector_db

    if not files:
        return "Please upload at least one PDF."

    all_docs = []

    for file in files:

        file_path = file.name

        loader = PyPDFLoader(file_path)

        pages = loader.load()

        for page in pages:
            page.metadata["source"] = os.path.basename(file_path)
            page.metadata["page"] = page.metadata.get("page", 0)

        all_docs.extend(pages)

    splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,
        chunk_overlap=200
    )

    chunks = splitter.split_documents(all_docs)

    vector_db = FAISS.from_documents(
        chunks,
        embedding_model
    )

    return f"Knowledge base created with {len(chunks)} chunks."


# ------------------------------
# CONTEXT BUILDER
# ------------------------------

def build_context(docs):

    context = ""
    sources = []

    for d in docs:

        context += d.page_content + "\n\n"

        src = f"{d.metadata['source']} (Page {d.metadata['page']})"

        if src not in sources:
            sources.append(src)

    return context, sources


# ------------------------------
# QUESTION ANSWERING
# ------------------------------

def ask_question(question):

    global vector_db

    if vector_db is None:
        yield "Please upload and build the knowledge base first."
        return

    docs = vector_db.similarity_search(question, k=5)

    context, sources = build_context(docs)

    prompt = f"""
You are an expert document assistant.

Answer ONLY using the context below.

If the answer is not present, say:
"I could not find the answer in the documents."

Context:
{context}

Question:
{question}

Answer:
"""

    stream = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=[{"role": "user", "content": prompt}],
        stream=True
    )

    response = ""

    for chunk in stream:

        if chunk.choices[0].delta.content:

            token = chunk.choices[0].delta.content

            response += token

            yield response

    source_text = "\n\nSources:\n"

    for s in sources:
        source_text += f"- {s}\n"

    yield response + source_text


# ------------------------------
# UI
# ------------------------------

with gr.Blocks(theme=gr.themes.Soft()) as app:

    gr.Markdown("# 📚 AI Knowledge Base Assistant")

    gr.Markdown("Upload PDFs and ask questions about them.")

    with gr.Row():

        file_input = gr.File(
            file_count="multiple",
            label="Upload PDF Files"
        )

        build_btn = gr.Button("Build Knowledge Base")

    status = gr.Textbox(label="System Status")

    build_btn.click(
        build_knowledge_base,
        inputs=file_input,
        outputs=status
    )

    gr.Markdown("## Ask Questions")

    question = gr.Textbox(
        placeholder="Ask something about the documents..."
    )

    ask_btn = gr.Button("Ask AI")

    answer = gr.Textbox(
        label="AI Response",
        lines=15
    )

    ask_btn.click(
        ask_question,
        inputs=question,
        outputs=answer
    )

    gr.Markdown(
        """
        ---
        © 2026 AI Document Assistant  
        Developed by **Asif Jamal**
        """
    )


app.launch()