File size: 8,619 Bytes
47afc54
 
 
 
 
 
 
6e0f63d
47afc54
59f7d02
47afc54
 
 
6e0f63d
 
47afc54
6e0f63d
 
 
918044e
 
47afc54
6e0f63d
 
 
 
cc9c8d4
6e0f63d
47afc54
918044e
 
47afc54
 
918044e
 
47afc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f7d02
47afc54
 
 
 
 
 
 
 
 
 
 
 
 
1b9aa99
 
 
6fbaaf8
1b9aa99
6fbaaf8
 
 
 
 
 
 
 
 
 
 
1b9aa99
 
 
 
 
 
 
 
 
 
 
d2f8a91
47afc54
1b9aa99
 
47afc54
1b9aa99
 
 
d2f8a91
47afc54
 
 
d2f8a91
 
 
 
 
 
 
 
 
6fbaaf8
59f7d02
 
 
 
 
 
 
 
 
ca42ff6
 
 
 
 
59f7d02
 
ca42ff6
d2f8a91
 
59f7d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47afc54
 
d2f8a91
59f7d02
 
 
d2f8a91
47afc54
 
d2f8a91
59f7d02
 
 
 
 
47afc54
 
 
 
59f7d02
1b9aa99
 
 
59f7d02
 
 
47afc54
59f7d02
 
47afc54
 
 
 
918044e
 
47afc54
 
 
 
 
 
 
 
 
 
 
 
 
 
6ee1bbb
 
 
 
 
 
 
 
 
 
 
 
47afc54
6ee1bbb
47afc54
9aca198
 
 
 
 
 
47afc54
6ee1bbb
 
 
 
 
 
 
 
 
47afc54
 
6ee1bbb
47afc54
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#Import relevant modules
import os
import gradio as gr
import weaviate
from openai import OpenAI
from pypdf import PdfReader
from pathlib import Path
from weaviate.auth import AuthApiKey
from dotenv import load_dotenv
import re

#Setup
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
WEAVIATE_URL = os.getenv("WEAVIATE_URL")
WEAVIATE_API_KEY = os.getenv("WEAVIATE_API_KEY")

print("Testing Weaviate connection...")
print("URL:", WEAVIATE_URL)
print("API KEY:", "SET" if WEAVIATE_API_KEY else "MISSING")
print("OPENAI_API_KEY:", "SET" if OPENAI_API_KEY else "MISSING")


# Connect to Weaviate Cloud
client = weaviate.connect_to_weaviate_cloud(
    cluster_url=WEAVIATE_URL,
    auth_credentials=AuthApiKey(WEAVIATE_API_KEY),
    skip_init_checks=True
)

openai_client = OpenAI(api_key=OPENAI_API_KEY)

# Load and process PDF
def extract_text_from_pdf(pdf_path):
    if not pdf_path or not os.path.exists(pdf_path):
        raise ValueError(f"No PDF file provided")
    reader = PdfReader(pdf_path)
    text = ""
    for page in reader.pages:
        text += page.extract_text() + "\n"
    return text

#Chunk the text
def chunk_text(text, chunk_size = 1000, overlap = 200):
    chunks = []
    start = 0
    while start < len(text):
        end = start + chunk_size
        chunks.append(text[start:end])
        start += chunk_size - overlap
    return chunks

#Weaviate setup
from weaviate.classes.config import DataType
def setup_schema():
    #wipe old collections
    client.collections.delete_all()

    #create new collection
    client.collections.create(
        name="PDFChunk",
        vectorizer_config=None,
        properties=[
                {"name":"text", "data_type":DataType.TEXT},
                {"name":"page", "data_type":DataType.INT}
            ]
    )
            
#Create embeddings and Store in Vector DB
def embed(text):
    return openai_client.embeddings.create(
        model = "text-embedding-3-large",
        input=text
    ).data[0].embedding

def insert_chunks(chunks):
    pdf_chunks = client.collections.get("PDFChunk")
    for i, chunk in enumerate(chunks):
        vec = embed(chunk)
        pdf_chunks.data.insert(
            properties={"text":chunk, "page":i},
            vector=vec
        )

# Querying
def expand_query(query):
    try:
        prompt = f"""Expand the following short questions into a more detailed search query
that includes synonyms and related HR terms, but also restate the keywords clearly.

Examples:

Q: Who should I contact if I am sick?
Expanded: Who should I notify or contact if I am ill, unwell, or absent due to sickness β€” such as my Deputy Head or line manager.

Q: What do I do if I am late?
Expanded: What procedure should I follow if I expect to be late, delayed, or absent for work β€” who must I contact, for example my Deputy Head or line manager?

Now expand this query in the same way:
Q: {query}
Expanded:
"""
        response = openai_client.chat.completions.create(
            model = "gpt-4.1-mini",
            messages = [{"role": "user", "content": prompt}],
            temperature=0
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        print("⚠️ Query expansion failed:", e)
        return query

def search_weaviate(query, k=12):
    pdf_chunks = client.collections.get("PDFChunk")
    expanded_query = expand_query(query)
    query_vec = embed(expanded_query)
    
    result = pdf_chunks.query.hybrid(  #both lexical and semantic
            query=expanded_query,
            vector=query_vec,
            alpha=0.3,
            limit=k,
            return_properties=["text", "page"]
    )

    filtered_objects = []
    for o in result.objects:
        distance = getattr(o.metadata, "distance", None)
        certainty = getattr(o.metadata, "certainty", None)

        # Keep results above a relevance threshold
        if (distance is None or distance < 1.2) or (certainty and certainty >0.3):
            filtered_objects.append(o)

    return [(o.properties["text"], o.metadata.distance)for o in result.objects] 

def rerank_chunks_with_llm(query, chunks):
    """
    Rerank retrieved chunks using GPT reasoning.
    Returns a list of chunks ordered in descending order
    """

    #Build a short reranking prompt
    chunk_list_parts = []
    for i, (text, _) in enumerate(chunks):
        clean_text = text[:400].strip().replace("\n", " ")
        chunk_list_parts.append(f"[{i+1}] {clean_text}...")
    chunk_list = "\n\n".join(chunk_list_parts)

    rerank_prompt = f"""
You are a precise HR assistant that ranks excerpts 
from a staff handbook by how relevant they are to the user's question.
You must rank excerpts that directly answer the user's question higher than those that merely discuss related topics.

    Question: {query}

    Excerpts:
    {chunk_list}

    Return only the list of excerpt numbers, separated by commas, in descending order of relevance.
    Example: 3, 1, 2
    """

    #Run LLM model
    response = openai_client.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[
            {"role": "system", "content": "You are a factual and consistent reranker."},
            {"role": "user", "content": rerank_prompt}
        ],
        temperature = 0
    )
    
    text_output = response.choices[0].message.content.strip()
    print(f"πŸ”Ž Reranker raw output: {text_output}")  # optional

    # extract numbers safely
    order = [int(x) for x in re.findall(r'\d+', text_output )]
    order = [i for i in order if 1 <= i <= len(chunks)] #ensure valid range

    # fallback: if model fails to output indices, return original order
    if not order:
        order = list(range(1, len(chunks) + 1))
    
    # Return reordered text chunks
    ordered_chunks = [chunks[i-1][0] for i in order]
    return ordered_chunks

def ask_question(query):
    chunks = search_weaviate(query, k=12)
    reranked_chunks = rerank_chunks_with_llm(query, chunks)

    # Use top three after reranking
    context = "\n\n---\n\n".join(reranked_chunks[:4])

    prompt = f"""
You are an HR assistant answering questions from the staff handbook.
Use only the following content to answer accurately and concisely:
    {context}

    Question: {query}
    Answer:
    """

    response = openai_client.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[
            {"role": "system", "content": 
             "You are a helpful HR assistant. Base your answer only on the handbook excerpts provided. \
            If the information is unclear, infer carefully using HR policies but prefer quoting exact text."},
            {"role": "user", "content": prompt}
    ],
    temperature=0
    )

    return response.choices[0].message.content.strip() 

#Gradio App
def process_pdf(pdf_file):
    try:
        if not pdf_file:
            return "❌ No file uploaded"
        setup_schema()
        # pdf_file is already a string path because of type="filepath"
        text = extract_text_from_pdf(pdf_file)
        chunks = chunk_text(text)
        insert_chunks(chunks)
        return "βœ… PDF uploaded and indexed! You can now ask questions."
    except Exception as e:
        import traceback
        return f"❌ Error: {str(e)}\n{traceback.format_exc()}"


def qa_pipeline(question):
    return ask_question(question)

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    # Global CSS injected explicitly
    gr.HTML("""
    <style>
      /* widen overall container */
      .gradio-container { max-width: 1100px !important; margin: auto; }

      /* make textareas bigger & full width */
      #qbox textarea { width: 100% !important; min-height: 110px !important; font-size: 16px !important; }
      #abox textarea { width: 100% !important; min-height: 220px !important; font-size: 16px !important; }
    </style>
    """)

    gr.Markdown("## πŸ“„ PDF Q&A Bot with Weaviate + OpenAI")

    with gr.Tab("Upload PDF"):
        pdf_input = gr.File(label="Upload PDF", type="filepath")
        upload_btn = gr.Button("Process PDF")
        status = gr.Textbox(label="Status")
        upload_btn.click(process_pdf, inputs=pdf_input, outputs=status)

    with gr.Tab("Ask Questions"):
        question = gr.Textbox(
            label="Your Question",
            elem_id="qbox"              # πŸ‘ˆ ID we target in CSS
        )
        answer = gr.Textbox(
            label="Answer",
            elem_id="abox"              # πŸ‘ˆ ID we target in CSS
        )
        ask_btn = gr.Button("Ask", size="lg")
        ask_btn.click(qa_pipeline, inputs=question, outputs=answer)

demo.launch()
client.close()