Spaces:
Running
Running
File size: 20,359 Bytes
9502853 0354172 9502853 ef3044e 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 d2155df 6d7e01a d2155df b87dc18 d2155df b87dc18 d2155df 0354172 9502853 d7880e6 9502853 d7880e6 b99c74d d7880e6 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 0354172 9502853 b99c74d 9502853 b99c74d 1a91761 9502853 |
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
# #############################################################################################################################
# # Filename : app.py
# # Description: A Streamlit application to showcase how RAG works.
# # Author : Georgios Ioannou
# #
# # Copyright © 2024 by Georgios Ioannou
# #############################################################################################################################
# app.py
import os
import json
from huggingface_hub import HfApi
import streamlit as st
from typing import List, Dict, Any
from urllib.parse import quote_plus
from pymongo import MongoClient
from PyPDF2 import PdfReader
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from langchain.prompts import PromptTemplate
from langchain.schema import Document
from langchain.schema.runnable import RunnableLambda, RunnablePassthrough
from huggingface_hub import InferenceClient
# =================== Secure Env via Hugging Face Secrets ===================
user = quote_plus(os.getenv("MONGO_USERNAME"))
password = quote_plus(os.getenv("MONGO_PASSWORD"))
cluster = os.getenv("MONGO_CLUSTER")
db_name = os.getenv("MONGO_DB_NAME", "files")
collection_name = os.getenv("MONGO_COLLECTION", "files_collection")
index_name = os.getenv("MONGO_VECTOR_INDEX", "vector_index")
HF_TOKEN = os.getenv("HF_TOKEN")
MONGO_URI = f"mongodb+srv://{user}:{password}@{cluster}/{db_name}?retryWrites=true&w=majority"
# =================== Prompt ===================
grantbuddy_prompt = PromptTemplate.from_template(
"""You are Grant Buddy, a specialized assistant helping nonprofits apply for grants.
Always align answers with the nonprofit’s mission to combat systemic poverty through education, technology, and social innovation.
Use the following context to answer the question. Be concise and mission-aligned.
CONTEXT:
{context}
QUESTION:
{question}
Respond truthfully. If the answer is not available, say "This information is not available in the current context."
"""
)
# =================== Vector Search Setup ===================
@st.cache_resource
def init_vector_search() -> MongoDBAtlasVectorSearch:
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
model_name = "thenlper/gte-small"
try:
st.write(f"🔌 Connecting to Hugging Face model: `{model_name}`")
embedding_model = HuggingFaceInferenceAPIEmbeddings(
api_key=HF_TOKEN,
model_name=model_name
)
# Test if embedding works
test_vector = embedding_model.embed_query("Test query for Grant Buddy")
st.success(f"✅ HF embedding model connected. Vector length: {len(test_vector)}")
except Exception as e:
st.error("❌ Failed to connect to Hugging Face Embedding API")
st.error(f"Error: {e}")
raise e # Stop app here if embedding fails
# MongoDB setup
user = quote_plus(os.getenv("MONGO_USERNAME", "").strip())
password = quote_plus(os.getenv("MONGO_PASSWORD", "").strip())
cluster = os.getenv("MONGO_CLUSTER", "").strip()
db_name = os.getenv("MONGO_DB_NAME", "files").strip()
collection_name = os.getenv("MONGO_COLLECTION", "files_collection").strip()
index_name = os.getenv("MONGO_VECTOR_INDEX", "vector_index").strip()
MONGO_URI = f"mongodb+srv://{user}:{password}@{cluster}/{db_name}?retryWrites=true&w=majority"
# Connect to vector search
try:
vector_store = MongoDBAtlasVectorSearch.from_connection_string(
connection_string=MONGO_URI,
namespace=f"{db_name}.{collection_name}",
embedding=embedding_model,
index_name=index_name
)
st.success("✅ Connected to MongoDB Vector Search")
return vector_store
except Exception as e:
st.error("❌ Failed to connect to MongoDB Atlas Vector Search")
st.error(f"Error: {e}")
raise e
# =================== Format Retrieved Chunks ===================
def format_docs(docs: List[Document]) -> str:
return "\n\n".join(doc.page_content or doc.metadata.get("content", "") for doc in docs)
# =================== Generate Response from Hugging Face Model ===================
def generate_response(input_dict: Dict[str, Any]) -> str:
client = InferenceClient(api_key=HF_TOKEN.strip())
prompt = grantbuddy_prompt.format(**input_dict)
try:
response = client.chat.completions.create(
model="HuggingFaceH4/zephyr-7b-beta",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": input_dict["question"]},
],
max_tokens=1000,
temperature=0.2,
)
return response.choices[0].message.content
except Exception as e:
st.error(f"❌ Error from model: {e}")
return "⚠️ Failed to generate response. Please check your model, HF token, or request format."
# =================== RAG Chain ===================
def get_rag_chain(retriever):
return {
"context": retriever | RunnableLambda(format_docs),
"question": RunnablePassthrough()
} | RunnableLambda(generate_response)
# =================== Streamlit UI ===================
def main():
st.set_page_config(page_title="Grant Buddy RAG", page_icon="🤖")
st.title("🤖 Grant Buddy: Grant-Writing Assistant")
uploaded_file = st.file_uploader("Upload PDF or TXT for extra context (optional)", type=["pdf", "txt"])
uploaded_text = ""
if uploaded_file:
if uploaded_file.name.endswith(".pdf"):
reader = PdfReader(uploaded_file)
uploaded_text = "\n".join([page.extract_text() for page in reader.pages])
elif uploaded_file.name.endswith(".txt"):
uploaded_text = uploaded_file.read().decode("utf-8")
retriever = init_vector_search().as_retriever(search_kwargs={"k": 10, "score_threshold": 0.75})
rag_chain = get_rag_chain(retriever)
query = st.text_input("Ask a grant-related question")
if st.button("Submit"):
if not query:
st.warning("Please enter a question.")
return
full_query = f"{query}\n\nAdditional context:\n{uploaded_text}" if uploaded_text else query
with st.spinner("Thinking..."):
response = rag_chain.invoke(full_query)
st.text_area("Grant Buddy says:", value=response, height=250, disabled=True)
with st.expander("🔍 Retrieved Chunks"):
context_docs = retriever.get_relevant_documents(full_query)
for doc in context_docs:
st.markdown(f"**Chunk ID:** {doc.metadata.get('chunk_id', 'unknown')}")
st.markdown(doc.page_content[:700] + "...")
st.markdown("---")
if __name__ == "__main__":
main()
# # Import libraries.
# import os
# import streamlit as st
# from dotenv import load_dotenv, find_dotenv
# from huggingface_hub import InferenceClient
# from langchain.prompts import PromptTemplate
# from langchain.schema import Document
# from langchain.schema.runnable import RunnablePassthrough, RunnableLambda
# # from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
# from langchain.embeddings import OpenAIEmbeddings
# from langchain_community.vectorstores import MongoDBAtlasVectorSearch
# from pymongo import MongoClient
# from pymongo.collection import Collection
# from typing import Dict, Any
# from langchain.chat_models import ChatOpenAI
# #############################################################################################################################
# class RAGQuestionAnswering:
# def __init__(self):
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Initializes the RAG Question Answering system by setting up configuration
# and loading environment variables.
# Assumptions
# -----------
# - Expects .env file with MONGO_URI and HF_TOKEN
# - Requires proper MongoDB setup with vector search index
# - Needs connection to Hugging Face API
# Notes
# -----
# This is the main class that handles all RAG operations
# """
# self.load_environment()
# self.setup_mongodb()
# self.setup_embedding_model()
# self.setup_vector_search()
# self.setup_rag_chain()
# def load_environment(self) -> None:
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Loads environment variables from .env file and sets up configuration constants.
# Assumptions
# -----------
# Expects a .env file with MONGO_URI and HF_TOKEN defined
# Notes
# -----
# Will stop the application if required environment variables are missing
# """
# load_dotenv(find_dotenv())
# self.MONGO_URI = os.getenv("MONGO_URI")
# # self.HF_TOKEN = os.getenv("HF_TOKEN")
# self.OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
# if not self.MONGO_URI or not self.OPENAI_API_KEY:
# st.error("Please ensure MONGO_URI and OPENAI_API_KEY are set in your .env file")
# st.stop()
# # MongoDB configuration.
# self.DB_NAME = "txts"
# self.COLLECTION_NAME = "txts_collection"
# self.VECTOR_SEARCH_INDEX = "vector_index"
# def setup_mongodb(self) -> None:
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Initializes the MongoDB connection and sets up the collection.
# Assumptions
# -----------
# - Valid MongoDB URI is available
# - Database and collection exist in MongoDB Atlas
# Notes
# -----
# Uses st.cache_resource for efficient connection management
# """
# @st.cache_resource
# def init_mongodb() -> Collection:
# cluster = MongoClient(self.MONGO_URI)
# return cluster[self.DB_NAME][self.COLLECTION_NAME]
# self.mongodb_collection = init_mongodb()
# def setup_embedding_model(self) -> None:
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Initializes the embedding model for vector search.
# Assumptions
# -----------
# - Valid Hugging Face API token
# - Internet connection to access the model
# Notes
# -----
# Uses the all-mpnet-base-v2 model from sentence-transformers
# """
# # @st.cache_resource
# # def init_embedding_model() -> HuggingFaceInferenceAPIEmbeddings:
# # return HuggingFaceInferenceAPIEmbeddings(
# # api_key=self.HF_TOKEN,
# # model_name="sentence-transformers/all-mpnet-base-v2",
# # )
# @st.cache_resource
# def init_embedding_model() -> OpenAIEmbeddings:
# return OpenAIEmbeddings(model="text-embedding-3-small", openai_api_key=self.OPENAI_API_KEY)
# self.embedding_model = init_embedding_model()
# def setup_vector_search(self) -> None:
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Sets up the vector search functionality using MongoDB Atlas.
# Assumptions
# -----------
# - MongoDB Atlas vector search index is properly configured
# - Valid embedding model is initialized
# Notes
# -----
# Creates a retriever with similarity search and score threshold
# """
# @st.cache_resource
# def init_vector_search() -> MongoDBAtlasVectorSearch:
# return MongoDBAtlasVectorSearch.from_connection_string(
# connection_string=self.MONGO_URI,
# namespace=f"{self.DB_NAME}.{self.COLLECTION_NAME}",
# embedding=self.embedding_model,
# index_name=self.VECTOR_SEARCH_INDEX,
# )
# self.vector_search = init_vector_search()
# self.retriever = self.vector_search.as_retriever(
# search_type="similarity", search_kwargs={"k": 10, "score_threshold": 0.85}
# )
# def format_docs(self, docs: list[Document]) -> str:
# """
# Parameters
# ----------
# **docs:** list[Document] - List of documents to be formatted
# Output
# ------
# str: Formatted string containing concatenated document content
# Purpose
# -------
# Formats the retrieved documents into a single string for processing
# Assumptions
# -----------
# Documents have page_content attribute
# Notes
# -----
# Joins documents with double newlines for better readability
# """
# return "\n\n".join(doc.page_content for doc in docs)
# # def generate_response(self, input_dict: Dict[str, Any]) -> str:
# # """
# # Parameters
# # ----------
# # **input_dict:** Dict[str, Any] - Dictionary containing context and question
# # Output
# # ------
# # str: Generated response from the model
# # Purpose
# # -------
# # Generates a response using the Hugging Face model based on context and question
# # Assumptions
# # -----------
# # - Valid Hugging Face API token
# # - Input dictionary contains 'context' and 'question' keys
# # Notes
# # -----
# # Uses Zephyr model with controlled temperature
# # """
# # hf_client = InferenceClient(api_key=self.HF_TOKEN)
# # formatted_prompt = self.prompt.format(**input_dict)
# # response = hf_client.chat.completions.create(
# # model="HuggingFaceH4/zephyr-7b-beta"
# # messages=[
# # {"role": "system", "content": formatted_prompt},
# # {"role": "user", "content": input_dict["question"]},
# # ],
# # max_tokens=1000,
# # temperature=0.2,
# # )
# # return response.choices[0].message.content
# from langchain.chat_models import ChatOpenAI
# from langchain.schema.messages import SystemMessage, HumanMessage
# def generate_response(self, input_dict: Dict[str, Any]) -> str:
# llm = ChatOpenAI(
# model="gpt-4", # or "gpt-3.5-turbo"
# temperature=0.2,
# openai_api_key=self.OPENAI_API_KEY,
# )
# messages = [
# SystemMessage(content=self.prompt.format(**input_dict)),
# HumanMessage(content=input_dict["question"]),
# ]
# return llm(messages).content
# def setup_rag_chain(self) -> None:
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Sets up the RAG chain for processing questions and generating answers
# Assumptions
# -----------
# Retriever and response generator are properly initialized
# Notes
# -----
# Creates a chain that combines retrieval and response generation
# """
# self.prompt = PromptTemplate.from_template(
# """Use the following pieces of context to answer the question at the end.
# START OF CONTEXT:
# {context}
# END OF CONTEXT:
# START OF QUESTION:
# {question}
# END OF QUESTION:
# If you do not know the answer, just say that you do not know.
# NEVER assume things.
# """
# )
# self.rag_chain = {
# "context": self.retriever | RunnableLambda(self.format_docs),
# "question": RunnablePassthrough(),
# } | RunnableLambda(self.generate_response)
# def process_question(self, question: str) -> str:
# """
# Parameters
# ----------
# **question:** str - The user's question to be answered
# Output
# ------
# str: The generated answer to the question
# Purpose
# -------
# Processes a user question through the RAG chain and returns an answer
# Assumptions
# -----------
# - Question is a non-empty string
# - RAG chain is properly initialized
# Notes
# -----
# Main interface for question-answering functionality
# """
# return self.rag_chain.invoke(question)
# #############################################################################################################################
# def setup_streamlit_ui() -> None:
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Sets up the Streamlit user interface with proper styling and layout
# Assumptions
# -----------
# - CSS file exists at ./static/styles/style.css
# - Image file exists at ./static/images/ctp.png
# Notes
# -----
# Handles all UI-related setup and styling
# """
# st.set_page_config(page_title="RAG Question Answering", page_icon="🤖")
# # Load CSS.
# with open("./static/styles/style.css") as f:
# st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
# # Title and subtitles.
# st.markdown(
# '<h1 align="center" style="font-family: monospace; font-size: 2.1rem; margin-top: -4rem">RAG Question Answering</h1>',
# unsafe_allow_html=True,
# )
# st.markdown(
# '<h3 align="center" style="font-family: monospace; font-size: 1.5rem; margin-top: -2rem">Using Zoom Closed Captioning From The Lectures</h3>',
# unsafe_allow_html=True,
# )
# st.markdown(
# '<h2 align="center" style="font-family: monospace; font-size: 1.5rem; margin-top: 0rem">CUNY Tech Prep Tutorial 5</h2>',
# unsafe_allow_html=True,
# )
# # Display logo.
# left_co, cent_co, last_co = st.columns(3)
# with cent_co:
# st.image("./static/images/ctp.png")
# #############################################################################################################################
# def main():
# """
# Parameters
# ----------
# None
# Output
# ------
# None
# Purpose
# -------
# Main function that runs the Streamlit application
# Assumptions
# -----------
# All required environment variables and files are present
# Notes
# -----
# Entry point for the application
# """
# # Setup UI.
# setup_streamlit_ui()
# # Initialize RAG system.
# rag_system = RAGQuestionAnswering()
# # Create input elements.
# query = st.text_input("Question:", key="question_input")
# # Handle submission.
# if st.button("Submit", type="primary"):
# if query:
# with st.spinner("Generating response..."):
# response = rag_system.process_question(query)
# st.text_area("Answer:", value=response, height=200, disabled=True)
# else:
# st.warning("Please enter a question.")
# # Add GitHub link.
# st.markdown(
# """
# <p align="center" style="font-family: monospace; color: #FAF9F6; font-size: 1rem;">
# <b>Check out our <a href="https://github.com/GeorgiosIoannouCoder/" style="color: #FAF9F6;">GitHub repository</a></b>
# </p>
# """,
# unsafe_allow_html=True,
# )
# #############################################################################################################################
# if __name__ == "__main__":
# main()
|