-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py.bak
90 lines (72 loc) · 2.8 KB
/
app.py.bak
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
from fastapi import FastAPI,File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from data_processing import pdf_processing , vectorise , create_conversation ,chat
from fastapi.responses import JSONResponse
import os
from typing import List
import logging
logging.basicConfig(level=logging.INFO)
app = FastAPI()
#update this during deployemnt
origins=["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
conversation = None
def pipeline():
global conversation
text_chunks=pdf_processing()
logging.info("**** PDF's Processed ****")
vectorstore=vectorise(text_chunks)
logging.info(f"**** Pinecone Vectors Created:{vectorstore} ****")
conversation=create_conversation(vectorstore)
logging.info("**** CONVERSATION MODEL CREATED ****")
logging.info("**** CHAT STARTED KINDLY ASK QUESTIONS ****")
@app.get("/")
def index():
return {"message","Server Running"}
@app.post("/upload_files")
async def start(files: List[UploadFile] = File(...)):
try:
UPLOAD_DIR="pdfs"
os.makedirs(UPLOAD_DIR,exist_ok=True)
uploaded_files=[]
for file in files:
file_location=os.path.join(UPLOAD_DIR,file.filename)
with open(file_location,"wb") as f:
f.write(await file.read())
uploaded_files.append(file.filename)
logging.info(f"**** FILES UPLOADED {uploaded_files} ****")
logging.info("**** FILES UPLOADED SUCCESSFULLY ****")
pipeline()
logging.info("**** CHAT CREATED, CLEARING PATH ****")
for root,_,files in os.walk(UPLOAD_DIR):
for name in files:
os.remove(os.path.join(root,name))
logging.info("**** PATH CLEARED ****")
logging.info("**** START CHAT ****")
# headers = {
# "Access-Control-Allow-Origin": "*",}
return JSONResponse(content={"message": "Files uploaded successfully. Chat Created", "filenames": uploaded_files},status_code=200)
except:
return JSONResponse(content={"error":"Files not Uploaded"},status_code=400)
@app.post("/chat")
async def chat_start(request):
global conversation
data=await request.json()
question=data.get('question')
if conversation is None:
return JSONResponse(content={"message": "Conversation model not created yet. Upload files first."}, status_code=400)
try:
response=chat(conversation,question)
logging.info(response)
return JSONResponse(content={"response":response['answer']})
except Exception as e:
return JSONResponse(content={"error":"Chat Error"},status_code=400)
if __name__=="main":
uvicorn.run(app,port=5001)