Sazzz02 commited on
Commit
1aba22b
·
verified ·
1 Parent(s): 679c60d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import pickle
4
+ import tempfile
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain_community.document_loaders import PyPDFLoader
7
+ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
8
+ from langchain_community.vectorstores import FAISS
9
+ from langchain.chains import RetrievalQA
10
+
11
+ # Load/OpenAI key
12
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
+
14
+ def build_vectorstore(pdf_path):
15
+ """Load PDF, chunk it, embed, and create FAISS index"""
16
+ loader = PyPDFLoader(pdf_path)
17
+ documents = loader.load()
18
+
19
+ text_splitter = RecursiveCharacterTextSplitter(
20
+ chunk_size=1000,
21
+ chunk_overlap=200,
22
+ separators=["\n\n", "\n", " ", ""]
23
+ )
24
+ chunks = text_splitter.split_documents(documents)
25
+
26
+ embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
27
+ vectorstore = FAISS.from_documents(chunks, embeddings)
28
+ return vectorstore
29
+
30
+ def rag_bot(question, pdf_file):
31
+ """Answer user queries using the uploaded PDF"""
32
+ if pdf_file is None:
33
+ return "Please upload a PDF first."
34
+
35
+ # Save uploaded PDF to a temp file
36
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
37
+ tmp_pdf.write(pdf_file.read())
38
+ pdf_path = tmp_pdf.name
39
+
40
+ try:
41
+ vectorstore = build_vectorstore(pdf_path)
42
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
43
+
44
+ qa = RetrievalQA.from_chain_type(
45
+ llm=ChatOpenAI(model="gpt-3.5-turbo", temperature=0, openai_api_key=OPENAI_API_KEY),
46
+ chain_type="stuff",
47
+ retriever=retriever,
48
+ )
49
+ result = qa.run(question)
50
+ return result
51
+ except Exception as e:
52
+ return f"Error: {e}"
53
+
54
+ # Gradio UI
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown("## 📖 RAG Q&A Bot – Upload a PDF and Ask Questions")
57
+ with gr.Row():
58
+ pdf_file = gr.File(label="Upload PDF", type="file")
59
+ with gr.Row():
60
+ question = gr.Textbox(label="Ask a Question")
61
+ answer = gr.Textbox(label="Answer", interactive=False)
62
+
63
+ submit = gr.Button("Submit")
64
+ submit.click(fn=rag_bot, inputs=[question, pdf_file], outputs=answer)
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch(server_name="0.0.0.0", server_port=7860)