vineeth N
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,3 @@
|
|
1 |
-
# import os
|
2 |
-
# from typing import List
|
3 |
-
# from dotenv import load_dotenv
|
4 |
-
# import chainlit as cl
|
5 |
-
# from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
-
# from langchain_text_splitters import RecursiveCharacterTextSplitter
|
7 |
-
# from langchain_community.vectorstores import FAISS
|
8 |
-
# from langchain_community.document_loaders import PyPDFLoader
|
9 |
-
# from langchain.chains import RetrievalQA
|
10 |
-
# from langchain_groq import ChatGroq
|
11 |
-
# from langchain_huggingface import HuggingFaceEmbeddings
|
12 |
-
|
13 |
-
# # Load environment variables
|
14 |
-
# load_dotenv()
|
15 |
-
|
16 |
-
# # Initialize embedding model
|
17 |
-
# # embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
18 |
-
|
19 |
-
# openai.api_key = os.getenv("OPENAI_API_KEY")
|
20 |
-
|
21 |
-
# # Initialize embedding model using OpenAI
|
22 |
-
# embeddings = OpenAIEmbeddings(openai_api_key=openai.api_key,model="text-embedding-3-small")
|
23 |
-
|
24 |
-
|
25 |
-
# # Initialize vector store
|
26 |
-
# vector_store = None
|
27 |
-
|
28 |
-
# # Store PDF file paths
|
29 |
-
# pdf_files = {}
|
30 |
-
|
31 |
-
# # Define the path for the FAISS index
|
32 |
-
# FAISS_INDEX_PATH = "faiss_index"
|
33 |
-
|
34 |
-
# def process_pdfs(directory: str) -> None:
|
35 |
-
# """Process all PDFs in the given directory and add them to the vector store."""
|
36 |
-
# global vector_store, pdf_files
|
37 |
-
# documents = []
|
38 |
-
|
39 |
-
# for filename in os.listdir(directory):
|
40 |
-
# if filename.endswith(".pdf"):
|
41 |
-
# file_path = os.path.join(directory, filename)
|
42 |
-
# loader = PyPDFLoader(file_path)
|
43 |
-
# documents.extend(loader.load())
|
44 |
-
# pdf_files[filename] = file_path
|
45 |
-
|
46 |
-
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
47 |
-
# texts = text_splitter.split_documents(documents)
|
48 |
-
|
49 |
-
# if os.path.exists(FAISS_INDEX_PATH):
|
50 |
-
# vector_store = FAISS.load_local(FAISS_INDEX_PATH, embeddings, allow_dangerous_deserialization=True)
|
51 |
-
# vector_store.add_documents(texts)
|
52 |
-
# else:
|
53 |
-
# vector_store = FAISS.from_documents(texts, embeddings)
|
54 |
-
|
55 |
-
# # Save the updated vector store
|
56 |
-
# vector_store.save_local(FAISS_INDEX_PATH)
|
57 |
-
# @cl.on_chat_start
|
58 |
-
# async def start():
|
59 |
-
# """Initialize the chat session."""
|
60 |
-
# await cl.Message(content="Welcome! Processing PDFs...").send()
|
61 |
-
|
62 |
-
# # Process PDFs (replace with your PDF directory)
|
63 |
-
# process_pdfs(r"C:\Users\sumes\OneDrive\Documents\pdf_docs")
|
64 |
-
|
65 |
-
# await cl.Message(content="PDFs processed. You can now ask questions!").send()
|
66 |
-
|
67 |
-
# @cl.on_message
|
68 |
-
# async def main(message: cl.Message):
|
69 |
-
# """Handle user messages and generate responses."""
|
70 |
-
# if vector_store is None:
|
71 |
-
# await cl.Message(content="Error: Vector store not initialized.").send()
|
72 |
-
# return
|
73 |
-
|
74 |
-
# query = message.content
|
75 |
-
|
76 |
-
# retriever = vector_store.as_retriever(search_kwargs={"k": 1})
|
77 |
-
|
78 |
-
# llm = OpenAI(openai_api_key=openai.api_key, model="gpt-4o-mini", temperature=0.4)
|
79 |
-
|
80 |
-
# qa_chain = RetrievalQA.from_chain_type(
|
81 |
-
# llm=llm,
|
82 |
-
# chain_type="stuff",
|
83 |
-
# retriever=retriever,
|
84 |
-
# return_source_documents=True
|
85 |
-
# )
|
86 |
-
|
87 |
-
# result = qa_chain(query)
|
88 |
-
# answer = result['result']
|
89 |
-
# source_docs = result['source_documents']
|
90 |
-
|
91 |
-
# await cl.Message(content=answer).send()
|
92 |
-
|
93 |
-
# if source_docs:
|
94 |
-
# sources_message = "Sources:\n"
|
95 |
-
# for doc in source_docs:
|
96 |
-
# file_name = os.path.basename(doc.metadata['source'])
|
97 |
-
# if file_name in pdf_files:
|
98 |
-
# file_path = pdf_files[file_name]
|
99 |
-
# elements = [
|
100 |
-
# cl.Text(name=file_name, content=f"Source: {file_name}"),
|
101 |
-
# cl.File(name=file_name, path=file_path, display="inline")
|
102 |
-
# ]
|
103 |
-
# await cl.Message(content=f"Source: {file_name}", elements=elements).send()
|
104 |
-
# else:
|
105 |
-
# sources_message += f"- {doc.metadata['source']}\n"
|
106 |
-
|
107 |
-
# if sources_message != "Sources:\n":
|
108 |
-
# await cl.Message(content=sources_message).send()
|
109 |
-
|
110 |
-
# if __name__ == "__main__":
|
111 |
-
# cl.run()
|
112 |
-
|
113 |
import os
|
114 |
from typing import List
|
115 |
from dotenv import load_dotenv
|
@@ -122,13 +10,11 @@ from langchain.chains import RetrievalQA
|
|
122 |
from langchain_openai import ChatOpenAI
|
123 |
from langchain_openai import OpenAIEmbeddings
|
124 |
|
125 |
-
|
126 |
# Load environment variables
|
127 |
load_dotenv()
|
128 |
|
129 |
# Initialize OpenAI API key
|
130 |
-
|
131 |
-
openai_api_key = 'sk-None-Nn6BodKwwjNYiNYT2QtWT3BlbkFJqTm7b3Fq4HftPntWdkUa'
|
132 |
|
133 |
# Initialize embedding model using OpenAI
|
134 |
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key,model="text-embedding-3-small")
|
@@ -183,52 +69,6 @@ async def start():
|
|
183 |
|
184 |
await cl.Message(content="PDFs processed. You can now ask questions!").send()
|
185 |
|
186 |
-
# @cl.on_message
|
187 |
-
# async def main(message: cl.Message):
|
188 |
-
# """Handle user messages and generate responses."""
|
189 |
-
# if vector_store is None:
|
190 |
-
# await cl.Message(content="Error: Vector store not initialized.").send()
|
191 |
-
# return
|
192 |
-
|
193 |
-
# query = message.content
|
194 |
-
|
195 |
-
# retriever = vector_store.as_retriever(search_kwargs={"k": 3})
|
196 |
-
|
197 |
-
# # Initialize the OpenAI language model
|
198 |
-
# llm = ChatOpenAI(openai_api_key=openai_api_key, model="gpt-4o-mini", temperature=0)
|
199 |
-
|
200 |
-
# qa_chain = RetrievalQA.from_chain_type(
|
201 |
-
# llm=llm,
|
202 |
-
# chain_type="stuff",
|
203 |
-
# retriever=retriever,
|
204 |
-
# return_source_documents=True
|
205 |
-
# )
|
206 |
-
|
207 |
-
# result = qa_chain(query)
|
208 |
-
# answer = result['result']
|
209 |
-
# source_docs = result['source_documents']
|
210 |
-
|
211 |
-
# await cl.Message(content=answer).send()
|
212 |
-
|
213 |
-
# if source_docs:
|
214 |
-
# sources_message = "Sources:\n"
|
215 |
-
# for doc in source_docs:
|
216 |
-
# file_name = os.path.basename(doc.metadata['source'])
|
217 |
-
# if file_name in pdf_files:
|
218 |
-
# file_path = pdf_files[file_name]
|
219 |
-
# elements = [
|
220 |
-
# cl.Text(name=file_name, content=f"Source: {file_name}"),
|
221 |
-
# cl.File(name=file_name, path=file_path, display="inline")
|
222 |
-
# ]
|
223 |
-
# await cl.Message(content=f"Source: {file_name}", elements=elements).send()
|
224 |
-
# else:
|
225 |
-
# sources_message += f"- {doc.metadata['source']}\n"
|
226 |
-
|
227 |
-
# if sources_message != "Sources:\n":
|
228 |
-
# await cl.Message(content=sources_message).send()
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
@cl.on_message
|
233 |
async def main(message: cl.Message):
|
234 |
"""Handle user messages and generate responses."""
|
@@ -276,4 +116,4 @@ async def main(message: cl.Message):
|
|
276 |
await cl.Message(content=sources_message).send()
|
277 |
|
278 |
if __name__ == "__main__":
|
279 |
-
cl.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from typing import List
|
3 |
from dotenv import load_dotenv
|
|
|
10 |
from langchain_openai import ChatOpenAI
|
11 |
from langchain_openai import OpenAIEmbeddings
|
12 |
|
|
|
13 |
# Load environment variables
|
14 |
load_dotenv()
|
15 |
|
16 |
# Initialize OpenAI API key
|
17 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
18 |
|
19 |
# Initialize embedding model using OpenAI
|
20 |
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key,model="text-embedding-3-small")
|
|
|
69 |
|
70 |
await cl.Message(content="PDFs processed. You can now ask questions!").send()
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
@cl.on_message
|
73 |
async def main(message: cl.Message):
|
74 |
"""Handle user messages and generate responses."""
|
|
|
116 |
await cl.Message(content=sources_message).send()
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
+
cl.run()
|