Spaces:
Sleeping
Sleeping
updated file for bot
Browse files
app.py
CHANGED
|
@@ -1,11 +1,73 @@
|
|
| 1 |
-
from flask import Flask,request,jsonify
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
app=Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
@app.route("/query",methods=["POST"])
|
| 6 |
-
def hello():
|
| 7 |
-
question=request.json["query"]
|
| 8 |
-
if question=="hi":
|
| 9 |
-
return {"hi":"whatsup"}
|
| 10 |
-
else:
|
| 11 |
-
return {"ask":"by hi"}
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 6 |
+
from langchain.vectorstores import FAISS
|
| 7 |
+
from langchain.chat_models import ChatOpenAI
|
| 8 |
+
from langchain.memory import ConversationBufferMemory
|
| 9 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
load_dotenv()
|
| 15 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
| 16 |
+
|
| 17 |
+
def get_pdf_text(pdf_docs):
|
| 18 |
+
text = ""
|
| 19 |
+
for pdf in pdf_docs:
|
| 20 |
+
pdf_reader = PdfReader(pdf)
|
| 21 |
+
for page in pdf_reader.pages:
|
| 22 |
+
text += page.extract_text()
|
| 23 |
+
return text
|
| 24 |
+
|
| 25 |
+
def get_text_chunks(text):
|
| 26 |
+
text_splitter = CharacterTextSplitter(
|
| 27 |
+
separator="\n",
|
| 28 |
+
chunk_size=1000,
|
| 29 |
+
chunk_overlap=200,
|
| 30 |
+
length_function=len
|
| 31 |
+
)
|
| 32 |
+
chunks = text_splitter.split_text(text)
|
| 33 |
+
return chunks
|
| 34 |
+
|
| 35 |
+
def get_vectorstore(text_chunks):
|
| 36 |
+
embeddings = OpenAIEmbeddings()
|
| 37 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 38 |
+
return vectorstore
|
| 39 |
+
|
| 40 |
+
def get_conversation_chain(vectorstore):
|
| 41 |
+
llm = ChatOpenAI()
|
| 42 |
+
memory = ConversationBufferMemory(
|
| 43 |
+
memory_key='chat_history', return_messages=True)
|
| 44 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 45 |
+
llm=llm,
|
| 46 |
+
retriever=vectorstore.as_retriever(),
|
| 47 |
+
memory=memory
|
| 48 |
+
)
|
| 49 |
+
return conversation_chain
|
| 50 |
+
|
| 51 |
+
@app.route('/upload', methods=['POST'])
|
| 52 |
+
def upload_files():
|
| 53 |
+
if 'files' not in request.files:
|
| 54 |
+
return jsonify({"error": "No file part in the request"}), 400
|
| 55 |
+
files = request.files.getlist('files')
|
| 56 |
+
raw_text = get_pdf_text(files)
|
| 57 |
+
text_chunks = get_text_chunks(raw_text)
|
| 58 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 59 |
+
global conversation_chain
|
| 60 |
+
conversation_chain = get_conversation_chain(vectorstore)
|
| 61 |
+
return jsonify({"status": "Files processed successfully"}), 200
|
| 62 |
+
|
| 63 |
+
@app.route('/query', methods=['POST'])
|
| 64 |
+
def query():
|
| 65 |
+
if 'question' not in request.json:
|
| 66 |
+
return jsonify({"error": "No question provided"}), 400
|
| 67 |
+
question = request.json['question']
|
| 68 |
+
if 'conversation_chain' not in globals():
|
| 69 |
+
return jsonify({"error": "No conversation chain initialized. Please upload documents first."}), 400
|
| 70 |
+
|
| 71 |
+
response = conversation_chain({'question': question})
|
| 72 |
+
return jsonify({"response": response['answer']})
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|