Tesneem commited on
Commit
f478447
·
verified ·
1 Parent(s): 6d7e01a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -463
app.py CHANGED
@@ -16,6 +16,8 @@ from pymongo import MongoClient
16
  from PyPDF2 import PdfReader
17
 
18
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
 
 
19
  from langchain_community.vectorstores import MongoDBAtlasVectorSearch
20
  from langchain.prompts import PromptTemplate
21
  from langchain.schema import Document
@@ -52,21 +54,26 @@ Respond truthfully. If the answer is not available, say "This information is not
52
  )
53
 
54
  # =================== Vector Search Setup ===================
 
 
 
 
55
  @st.cache_resource
56
  def init_vector_search() -> MongoDBAtlasVectorSearch:
57
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
58
  from langchain_community.vectorstores import MongoDBAtlasVectorSearch
59
 
60
  HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
61
- model_name = "thenlper/gte-small"
62
 
63
  try:
64
  st.write(f"🔌 Connecting to Hugging Face model: `{model_name}`")
65
- embedding_model = HuggingFaceInferenceAPIEmbeddings(
66
- api_key=HF_TOKEN,
67
- model_name=model_name
68
- )
69
 
 
70
  # Test if embedding works
71
  test_vector = embedding_model.embed_query("Test query for Grant Buddy")
72
  st.success(f"✅ HF embedding model connected. Vector length: {len(test_vector)}")
@@ -174,462 +181,4 @@ def main():
174
  if __name__ == "__main__":
175
  main()
176
 
177
- # # Import libraries.
178
- # import os
179
- # import streamlit as st
180
-
181
- # from dotenv import load_dotenv, find_dotenv
182
- # from huggingface_hub import InferenceClient
183
- # from langchain.prompts import PromptTemplate
184
- # from langchain.schema import Document
185
- # from langchain.schema.runnable import RunnablePassthrough, RunnableLambda
186
- # # from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
187
- # from langchain.embeddings import OpenAIEmbeddings
188
- # from langchain_community.vectorstores import MongoDBAtlasVectorSearch
189
- # from pymongo import MongoClient
190
- # from pymongo.collection import Collection
191
- # from typing import Dict, Any
192
- # from langchain.chat_models import ChatOpenAI
193
-
194
-
195
-
196
- # #############################################################################################################################
197
-
198
-
199
- # class RAGQuestionAnswering:
200
- # def __init__(self):
201
- # """
202
- # Parameters
203
- # ----------
204
- # None
205
-
206
- # Output
207
- # ------
208
- # None
209
-
210
- # Purpose
211
- # -------
212
- # Initializes the RAG Question Answering system by setting up configuration
213
- # and loading environment variables.
214
-
215
- # Assumptions
216
- # -----------
217
- # - Expects .env file with MONGO_URI and HF_TOKEN
218
- # - Requires proper MongoDB setup with vector search index
219
- # - Needs connection to Hugging Face API
220
-
221
- # Notes
222
- # -----
223
- # This is the main class that handles all RAG operations
224
- # """
225
- # self.load_environment()
226
- # self.setup_mongodb()
227
- # self.setup_embedding_model()
228
- # self.setup_vector_search()
229
- # self.setup_rag_chain()
230
-
231
- # def load_environment(self) -> None:
232
- # """
233
- # Parameters
234
- # ----------
235
- # None
236
-
237
- # Output
238
- # ------
239
- # None
240
-
241
- # Purpose
242
- # -------
243
- # Loads environment variables from .env file and sets up configuration constants.
244
-
245
- # Assumptions
246
- # -----------
247
- # Expects a .env file with MONGO_URI and HF_TOKEN defined
248
-
249
- # Notes
250
- # -----
251
- # Will stop the application if required environment variables are missing
252
- # """
253
-
254
- # load_dotenv(find_dotenv())
255
- # self.MONGO_URI = os.getenv("MONGO_URI")
256
- # # self.HF_TOKEN = os.getenv("HF_TOKEN")
257
- # self.OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
258
-
259
-
260
- # if not self.MONGO_URI or not self.OPENAI_API_KEY:
261
- # st.error("Please ensure MONGO_URI and OPENAI_API_KEY are set in your .env file")
262
- # st.stop()
263
-
264
- # # MongoDB configuration.
265
- # self.DB_NAME = "txts"
266
- # self.COLLECTION_NAME = "txts_collection"
267
- # self.VECTOR_SEARCH_INDEX = "vector_index"
268
-
269
- # def setup_mongodb(self) -> None:
270
- # """
271
- # Parameters
272
- # ----------
273
- # None
274
-
275
- # Output
276
- # ------
277
- # None
278
-
279
- # Purpose
280
- # -------
281
- # Initializes the MongoDB connection and sets up the collection.
282
-
283
- # Assumptions
284
- # -----------
285
- # - Valid MongoDB URI is available
286
- # - Database and collection exist in MongoDB Atlas
287
-
288
- # Notes
289
- # -----
290
- # Uses st.cache_resource for efficient connection management
291
- # """
292
-
293
- # @st.cache_resource
294
- # def init_mongodb() -> Collection:
295
- # cluster = MongoClient(self.MONGO_URI)
296
- # return cluster[self.DB_NAME][self.COLLECTION_NAME]
297
-
298
- # self.mongodb_collection = init_mongodb()
299
-
300
- # def setup_embedding_model(self) -> None:
301
- # """
302
- # Parameters
303
- # ----------
304
- # None
305
-
306
- # Output
307
- # ------
308
- # None
309
-
310
- # Purpose
311
- # -------
312
- # Initializes the embedding model for vector search.
313
-
314
- # Assumptions
315
- # -----------
316
- # - Valid Hugging Face API token
317
- # - Internet connection to access the model
318
-
319
- # Notes
320
- # -----
321
- # Uses the all-mpnet-base-v2 model from sentence-transformers
322
- # """
323
-
324
- # # @st.cache_resource
325
- # # def init_embedding_model() -> HuggingFaceInferenceAPIEmbeddings:
326
- # # return HuggingFaceInferenceAPIEmbeddings(
327
- # # api_key=self.HF_TOKEN,
328
- # # model_name="sentence-transformers/all-mpnet-base-v2",
329
- # # )
330
-
331
- # @st.cache_resource
332
- # def init_embedding_model() -> OpenAIEmbeddings:
333
- # return OpenAIEmbeddings(model="text-embedding-3-small", openai_api_key=self.OPENAI_API_KEY)
334
-
335
- # self.embedding_model = init_embedding_model()
336
-
337
- # def setup_vector_search(self) -> None:
338
- # """
339
- # Parameters
340
- # ----------
341
- # None
342
-
343
- # Output
344
- # ------
345
- # None
346
-
347
- # Purpose
348
- # -------
349
- # Sets up the vector search functionality using MongoDB Atlas.
350
-
351
- # Assumptions
352
- # -----------
353
- # - MongoDB Atlas vector search index is properly configured
354
- # - Valid embedding model is initialized
355
-
356
- # Notes
357
- # -----
358
- # Creates a retriever with similarity search and score threshold
359
- # """
360
-
361
- # @st.cache_resource
362
- # def init_vector_search() -> MongoDBAtlasVectorSearch:
363
- # return MongoDBAtlasVectorSearch.from_connection_string(
364
- # connection_string=self.MONGO_URI,
365
- # namespace=f"{self.DB_NAME}.{self.COLLECTION_NAME}",
366
- # embedding=self.embedding_model,
367
- # index_name=self.VECTOR_SEARCH_INDEX,
368
- # )
369
-
370
- # self.vector_search = init_vector_search()
371
- # self.retriever = self.vector_search.as_retriever(
372
- # search_type="similarity", search_kwargs={"k": 10, "score_threshold": 0.85}
373
- # )
374
-
375
- # def format_docs(self, docs: list[Document]) -> str:
376
- # """
377
- # Parameters
378
- # ----------
379
- # **docs:** list[Document] - List of documents to be formatted
380
-
381
- # Output
382
- # ------
383
- # str: Formatted string containing concatenated document content
384
-
385
- # Purpose
386
- # -------
387
- # Formats the retrieved documents into a single string for processing
388
-
389
- # Assumptions
390
- # -----------
391
- # Documents have page_content attribute
392
-
393
- # Notes
394
- # -----
395
- # Joins documents with double newlines for better readability
396
- # """
397
-
398
- # return "\n\n".join(doc.page_content for doc in docs)
399
-
400
- # # def generate_response(self, input_dict: Dict[str, Any]) -> str:
401
- # # """
402
- # # Parameters
403
- # # ----------
404
- # # **input_dict:** Dict[str, Any] - Dictionary containing context and question
405
-
406
- # # Output
407
- # # ------
408
- # # str: Generated response from the model
409
-
410
- # # Purpose
411
- # # -------
412
- # # Generates a response using the Hugging Face model based on context and question
413
-
414
- # # Assumptions
415
- # # -----------
416
- # # - Valid Hugging Face API token
417
- # # - Input dictionary contains 'context' and 'question' keys
418
-
419
- # # Notes
420
- # # -----
421
- # # Uses Zephyr model with controlled temperature
422
- # # """
423
- # # hf_client = InferenceClient(api_key=self.HF_TOKEN)
424
- # # formatted_prompt = self.prompt.format(**input_dict)
425
-
426
- # # response = hf_client.chat.completions.create(
427
- # # model="HuggingFaceH4/zephyr-7b-beta"
428
-
429
- # # messages=[
430
- # # {"role": "system", "content": formatted_prompt},
431
- # # {"role": "user", "content": input_dict["question"]},
432
- # # ],
433
- # # max_tokens=1000,
434
- # # temperature=0.2,
435
- # # )
436
-
437
- # # return response.choices[0].message.content
438
- # from langchain.chat_models import ChatOpenAI
439
- # from langchain.schema.messages import SystemMessage, HumanMessage
440
-
441
- # def generate_response(self, input_dict: Dict[str, Any]) -> str:
442
- # llm = ChatOpenAI(
443
- # model="gpt-4", # or "gpt-3.5-turbo"
444
- # temperature=0.2,
445
- # openai_api_key=self.OPENAI_API_KEY,
446
- # )
447
-
448
- # messages = [
449
- # SystemMessage(content=self.prompt.format(**input_dict)),
450
- # HumanMessage(content=input_dict["question"]),
451
- # ]
452
-
453
- # return llm(messages).content
454
-
455
-
456
- # def setup_rag_chain(self) -> None:
457
- # """
458
- # Parameters
459
- # ----------
460
- # None
461
-
462
- # Output
463
- # ------
464
- # None
465
-
466
- # Purpose
467
- # -------
468
- # Sets up the RAG chain for processing questions and generating answers
469
-
470
- # Assumptions
471
- # -----------
472
- # Retriever and response generator are properly initialized
473
-
474
- # Notes
475
- # -----
476
- # Creates a chain that combines retrieval and response generation
477
- # """
478
-
479
- # self.prompt = PromptTemplate.from_template(
480
- # """Use the following pieces of context to answer the question at the end.
481
-
482
- # START OF CONTEXT:
483
- # {context}
484
- # END OF CONTEXT:
485
-
486
- # START OF QUESTION:
487
- # {question}
488
- # END OF QUESTION:
489
-
490
- # If you do not know the answer, just say that you do not know.
491
- # NEVER assume things.
492
- # """
493
- # )
494
-
495
- # self.rag_chain = {
496
- # "context": self.retriever | RunnableLambda(self.format_docs),
497
- # "question": RunnablePassthrough(),
498
- # } | RunnableLambda(self.generate_response)
499
-
500
- # def process_question(self, question: str) -> str:
501
- # """
502
- # Parameters
503
- # ----------
504
- # **question:** str - The user's question to be answered
505
 
506
- # Output
507
- # ------
508
- # str: The generated answer to the question
509
-
510
- # Purpose
511
- # -------
512
- # Processes a user question through the RAG chain and returns an answer
513
-
514
- # Assumptions
515
- # -----------
516
- # - Question is a non-empty string
517
- # - RAG chain is properly initialized
518
-
519
- # Notes
520
- # -----
521
- # Main interface for question-answering functionality
522
- # """
523
-
524
- # return self.rag_chain.invoke(question)
525
-
526
-
527
- # #############################################################################################################################
528
- # def setup_streamlit_ui() -> None:
529
- # """
530
- # Parameters
531
- # ----------
532
- # None
533
-
534
- # Output
535
- # ------
536
- # None
537
-
538
- # Purpose
539
- # -------
540
- # Sets up the Streamlit user interface with proper styling and layout
541
-
542
- # Assumptions
543
- # -----------
544
- # - CSS file exists at ./static/styles/style.css
545
- # - Image file exists at ./static/images/ctp.png
546
-
547
- # Notes
548
- # -----
549
- # Handles all UI-related setup and styling
550
- # """
551
-
552
- # st.set_page_config(page_title="RAG Question Answering", page_icon="🤖")
553
-
554
- # # Load CSS.
555
- # with open("./static/styles/style.css") as f:
556
- # st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
557
-
558
- # # Title and subtitles.
559
- # st.markdown(
560
- # '<h1 align="center" style="font-family: monospace; font-size: 2.1rem; margin-top: -4rem">RAG Question Answering</h1>',
561
- # unsafe_allow_html=True,
562
- # )
563
- # st.markdown(
564
- # '<h3 align="center" style="font-family: monospace; font-size: 1.5rem; margin-top: -2rem">Using Zoom Closed Captioning From The Lectures</h3>',
565
- # unsafe_allow_html=True,
566
- # )
567
- # st.markdown(
568
- # '<h2 align="center" style="font-family: monospace; font-size: 1.5rem; margin-top: 0rem">CUNY Tech Prep Tutorial 5</h2>',
569
- # unsafe_allow_html=True,
570
- # )
571
-
572
- # # Display logo.
573
- # left_co, cent_co, last_co = st.columns(3)
574
- # with cent_co:
575
- # st.image("./static/images/ctp.png")
576
-
577
-
578
- # #############################################################################################################################
579
-
580
-
581
- # def main():
582
- # """
583
- # Parameters
584
- # ----------
585
- # None
586
-
587
- # Output
588
- # ------
589
- # None
590
-
591
- # Purpose
592
- # -------
593
- # Main function that runs the Streamlit application
594
-
595
- # Assumptions
596
- # -----------
597
- # All required environment variables and files are present
598
-
599
- # Notes
600
- # -----
601
- # Entry point for the application
602
- # """
603
-
604
- # # Setup UI.
605
- # setup_streamlit_ui()
606
-
607
- # # Initialize RAG system.
608
- # rag_system = RAGQuestionAnswering()
609
-
610
- # # Create input elements.
611
- # query = st.text_input("Question:", key="question_input")
612
-
613
- # # Handle submission.
614
- # if st.button("Submit", type="primary"):
615
- # if query:
616
- # with st.spinner("Generating response..."):
617
- # response = rag_system.process_question(query)
618
- # st.text_area("Answer:", value=response, height=200, disabled=True)
619
- # else:
620
- # st.warning("Please enter a question.")
621
-
622
- # # Add GitHub link.
623
- # st.markdown(
624
- # """
625
- # <p align="center" style="font-family: monospace; color: #FAF9F6; font-size: 1rem;">
626
- # <b>Check out our <a href="https://github.com/GeorgiosIoannouCoder/" style="color: #FAF9F6;">GitHub repository</a></b>
627
- # </p>
628
- # """,
629
- # unsafe_allow_html=True,
630
- # )
631
-
632
-
633
- # #############################################################################################################################
634
- # if __name__ == "__main__":
635
- # main()
 
16
  from PyPDF2 import PdfReader
17
 
18
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
19
+ from langchain.embeddings import HuggingFaceEmbeddings
20
+
21
  from langchain_community.vectorstores import MongoDBAtlasVectorSearch
22
  from langchain.prompts import PromptTemplate
23
  from langchain.schema import Document
 
54
  )
55
 
56
  # =================== Vector Search Setup ===================
57
+ @st.cache_resource
58
+ def init_embedding_model():
59
+ return HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
60
+
61
  @st.cache_resource
62
  def init_vector_search() -> MongoDBAtlasVectorSearch:
63
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
64
  from langchain_community.vectorstores import MongoDBAtlasVectorSearch
65
 
66
  HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
67
+ # model_name = "thenlper/gte-small"
68
 
69
  try:
70
  st.write(f"🔌 Connecting to Hugging Face model: `{model_name}`")
71
+ # embedding_model = HuggingFaceInferenceAPIEmbeddings(
72
+ # api_key=HF_TOKEN,
73
+ # model_name=model_name
74
+ # )
75
 
76
+ embedding_model=init_embedding_model()
77
  # Test if embedding works
78
  test_vector = embedding_model.embed_query("Test query for Grant Buddy")
79
  st.success(f"✅ HF embedding model connected. Vector length: {len(test_vector)}")
 
181
  if __name__ == "__main__":
182
  main()
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184