Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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 |
-
dataID =
|
| 43 |
-
|
| 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 =
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
chat_history[data.chatID].append({"role": "
|
| 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 |
-
|
| 91 |
-
|
| 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 |
+
|