HFswapnil commited on
Commit
95218af
·
verified ·
1 Parent(s): 3abdd3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -94
app.py CHANGED
@@ -1,94 +1,95 @@
1
- import os
2
- import uuid
3
- from fastapi import FastAPI, Request, File, UploadFile, Form
4
- from run_model import generate_response
5
- from fastapi.responses import HTMLResponse
6
- from fastapi.staticfiles import StaticFiles
7
- from fastapi.templating import Jinja2Templates
8
- from pydantic import BaseModel, Field
9
- from rag_util import Create_RAG_Prompt, ProcessDocuments
10
- import time
11
- import bleach
12
-
13
- class DataModel(BaseModel):
14
- data: str
15
- id : str = ""
16
- chatID :str = Field(..., min_length=36, description="Chat ID must not be empty")
17
-
18
-
19
- app = FastAPI(
20
- docs_url=None, # disables /docs
21
- redoc_url=None, # disables /redoc
22
- openapi_url=None # disables /openapi.json
23
- )
24
-
25
- app.mount("/static", StaticFiles(directory="static"), name="static")
26
-
27
- templates = Jinja2Templates(directory="templates")
28
-
29
- chat_history = {}
30
- CHAT_START_TIME = time.time()
31
-
32
-
33
-
34
- @app.get("/", response_class=HTMLResponse)
35
- async def root(request: Request):
36
- chatID = uuid.uuid4()
37
- return templates.TemplateResponse("index.html", {"request" : request, "chatID" : chatID})
38
-
39
-
40
- @app.post("/generate")
41
- async def generate(data: DataModel):
42
- dataID = bleach.clean(data.id)
43
- if dataID == "":
44
- input_text = bleach.clean(data.data)
45
- history = chat_history[data.chatID] = [] if data.chatID not in chat_history else chat_history[data.chatID]
46
-
47
- llm_response = generate_response(prompt=input_text, history=history)
48
-
49
- if data.chatID not in chat_history:
50
- chat_history[data.chatID] = []
51
- chat_history[data.chatID].append({"role": "user", "content": input_text})
52
- chat_history[data.chatID].append({"role": "assistant", "content": llm_response})
53
-
54
- return {"llm_response" : llm_response}
55
- else :
56
- input_text = bleach.clean(data.data)
57
- prompt, context = Create_RAG_Prompt(input_text, chatID=data.chatID, history=chat_history[data.chatID] if chat_history[data.chatID] else [])
58
-
59
- llm_response = generate_response(prompt=prompt, context=context)
60
-
61
- if data.chatID not in chat_history:
62
- chat_history[data.chatID] = []
63
- chat_history[data.chatID].append({"role": "user", "content": input_text})
64
- chat_history[data.chatID].append({"role": "assistant", "content": llm_response})
65
-
66
- return {"llm_response" : llm_response}
67
-
68
-
69
- def isValidPDF(file):
70
- if not file.filename.endswith(".pdf") or file.content_type != "application/pdf":
71
- return False
72
- return True
73
-
74
-
75
- @app.post("/upload_pdf")
76
- async def upload_pdf(file: UploadFile = File(...), chatID:str = Form(...)):
77
- fileID = uuid.uuid4()
78
- save_path = f"./uploads/{fileID}.pdf"
79
-
80
- # Ensure uploads directory exists
81
- os.makedirs(os.path.dirname(save_path), exist_ok=True)
82
-
83
- if isValidPDF(file):
84
- with open(save_path, "wb") as f:
85
- f.write(await file.read())
86
- # print(save_path)
87
-
88
- ProcessDocuments(save_path, chatID)
89
- return {"filename": fileID, "message": "PDF uploaded successfully."}
90
- else :
91
- return {"filename" : "UPLOADED_FILE", "message" : "Uploaded File is not a valid PDF."}
92
-
93
-
94
-
 
 
1
+ import os
2
+ import uuid
3
+ from fastapi import FastAPI, Request, File, UploadFile, Form
4
+ from run_model import generate_response
5
+ from fastapi.responses import HTMLResponse
6
+ from fastapi.staticfiles import StaticFiles
7
+ from fastapi.templating import Jinja2Templates
8
+ from pydantic import BaseModel, Field
9
+ from rag_util import Create_RAG_Prompt, ProcessDocuments
10
+ import time
11
+
12
+ class DataModel(BaseModel):
13
+ data: str
14
+ id : str = ""
15
+ chatID :str = Field(..., min_length=36, description="Chat ID must not be empty")
16
+
17
+
18
+ app = FastAPI(
19
+ docs_url=None, # disables /docs
20
+ redoc_url=None, # disables /redoc
21
+ openapi_url=None # disables /openapi.json
22
+ )
23
+
24
+ app.mount("/static", StaticFiles(directory="static"), name="static")
25
+
26
+ templates = Jinja2Templates(directory="templates")
27
+
28
+ chat_history = {}
29
+ CHAT_START_TIME = time.time()
30
+
31
+
32
+
33
+ @app.get("/", response_class=HTMLResponse)
34
+ async def root(request: Request):
35
+ chatID = uuid.uuid4()
36
+ return templates.TemplateResponse("index.html", {"request" : request, "chatID" : chatID})
37
+
38
+
39
+ @app.post("/generate")
40
+ async def generate(data: DataModel):
41
+ dataID = bleach.clean(data.id)
42
+ if dataID == "":
43
+ input_text = data.data
44
+ # input_text = bleach.clean(data.data)
45
+ history = chat_history[data.chatID] = [] if data.chatID not in chat_history else chat_history[data.chatID]
46
+
47
+ llm_response = generate_response(prompt=input_text, history=history)
48
+
49
+ if data.chatID not in chat_history:
50
+ chat_history[data.chatID] = []
51
+ chat_history[data.chatID].append({"role": "user", "content": input_text})
52
+ chat_history[data.chatID].append({"role": "assistant", "content": llm_response})
53
+
54
+ return {"llm_response" : llm_response}
55
+ else :
56
+ input_text = data.data
57
+ # input_text = bleach.clean(data.data)
58
+ prompt, context = Create_RAG_Prompt(input_text, chatID=data.chatID, history=chat_history[data.chatID] if chat_history[data.chatID] else [])
59
+
60
+ llm_response = generate_response(prompt=prompt, context=context)
61
+
62
+ if data.chatID not in chat_history:
63
+ chat_history[data.chatID] = []
64
+ chat_history[data.chatID].append({"role": "user", "content": input_text})
65
+ chat_history[data.chatID].append({"role": "assistant", "content": llm_response})
66
+
67
+ return {"llm_response" : llm_response}
68
+
69
+
70
+ def isValidPDF(file):
71
+ if not file.filename.endswith(".pdf") or file.content_type != "application/pdf":
72
+ return False
73
+ return True
74
+
75
+
76
+ @app.post("/upload_pdf")
77
+ async def upload_pdf(file: UploadFile = File(...), chatID:str = Form(...)):
78
+ fileID = uuid.uuid4()
79
+ save_path = f"./uploads/{fileID}.pdf"
80
+
81
+ # Ensure uploads directory exists
82
+ os.makedirs(os.path.dirname(save_path), exist_ok=True)
83
+
84
+ if isValidPDF(file):
85
+ with open(save_path, "wb") as f:
86
+ f.write(await file.read())
87
+ # print(save_path)
88
+
89
+ ProcessDocuments(save_path, chatID)
90
+ return {"filename": fileID, "message": "PDF uploaded successfully."}
91
+ else :
92
+ return {"filename" : "UPLOADED_FILE", "message" : "Uploaded File is not a valid PDF."}
93
+
94
+
95
+