Spaces:
Running
Running
File size: 5,393 Bytes
48ec4db |
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
from app.backend.controllers.chats import list_user_chats, verify_ownership_rights
from app.backend.controllers.users import get_current_user
from app.settings import BASE_DIR, logger, settings
from fastapi.templating import Jinja2Templates
from app.core.rag_generator import RagSystem
from app.backend.models.users import User
from fastapi import Request, UploadFile
from uuid import uuid4
import markdown
import aiofiles
import asyncio
import shutil
import os
rag = None
def initialize_rag() -> RagSystem:
global rag
if rag is None:
print("Start RAG initialization")
rag = RagSystem()
return rag
async def extend_context(context: dict, selected: int = None):
user = await get_current_user(context.get("request"))
navbar = {
"navbar": False,
"navbar_path": "components/navbar.html",
"navbar_context": {
"chats": [],
"user": {"role": "user" if user else "guest", "instance": user},
},
}
sidebar = {
"sidebar": True,
"sidebar_path": "components/sidebar.html",
"sidebar_context": {
"selected": selected if selected is not None else None,
"chat_groups": await list_user_chats(user.id) if user else [],
},
}
footer = {"footer": False, "footer_context": None}
context.update(**navbar)
context.update(**footer)
context.update(**sidebar)
return context
async def protect_chat(user: User, chat_id: str) -> bool:
return await verify_ownership_rights(user, chat_id)
async def save_documents(
files: list[UploadFile],
user: User,
chat_id: str,
) -> list[str]:
storage = os.path.join(
BASE_DIR,
"chats_storage",
f"user_id={user.id}",
f"chat_id={chat_id}",
"documents",
)
docs = []
if files is None or len(files) == 0:
return
await aiofiles.os.makedirs(os.path.join(storage, "pdfs"), exist_ok=True)
if settings.debug:
await logger.info(f"Documents for saving: {len(files)}")
for file in files:
content = await file.read()
if file.filename.endswith(".pdf"):
saved_file = os.path.join(storage, "pdfs", str(uuid4()) + ".pdf")
else:
saved_file = os.path.join(
storage, str(uuid4()) + "." + file.filename.split(".")[-1]
)
async with aiofiles.open(saved_file, "wb") as f:
await f.write(content)
docs.append(saved_file)
return docs
async def get_pdf_path(path: str) -> str:
parts = path.split("chats_storage")
if len(parts) < 2:
return ""
return "chats_storage" + "".join(parts[1:])
async def construct_collection_name(user: User, chat_id: int) -> str:
return f"user_id_{user.id}_chat_id_{chat_id}"
async def create_collection(user: User, chat_id: int, RAG: RagSystem) -> None:
if RAG is None:
raise RuntimeError("RAG was not initialized")
await RAG.create_new_collection(await construct_collection_name(user, chat_id))
if settings.debug:
for collection in await rag.get_collections_names():
await logger.info(collection)
async def lines_to_markdown(lines: list[str]) -> list[str]:
loop = asyncio.get_running_loop()
return await asyncio.gather(*[
loop.run_in_executor(None, markdown.markdown, line)
for line in lines
])
# <----------------------- Handlers ----------------------->
async def PDFHandler(
request: Request, path: str, page: int, templates
) -> Jinja2Templates.TemplateResponse:
url_path = await get_pdf_path(path=path)
if settings.debug:
await logger.info(f"PDF path - {path}, url-path - {url_path}")
current_template = "pages/show_pdf.html"
return templates.TemplateResponse(
current_template,
await extend_context(
{
"request": request,
"page": str(page or 1),
"url_path": url_path,
"user": await get_current_user(request),
}
),
)
async def TextHandler(
request: Request, path: str, lines: str, templates
) -> Jinja2Templates.TemplateResponse:
file_content = ""
async with aiofiles.open(path, "r") as f:
file_content = await f.read()
start_line, end_line = map(int, lines.split("-"))
text_before_citation = []
text_after_citation = []
citation = []
anchor_added = False
for index, line in enumerate(file_content.split("\n")):
if line == "" or line == "\n":
continue
if index + 1 < start_line:
text_before_citation.append(line)
elif end_line < index + 1:
text_after_citation.append(line)
else:
anchor_added = True
citation.append(line)
current_template = "pages/show_text.html"
return templates.TemplateResponse(
current_template,
await extend_context(
{
"request": request,
"text_before_citation": await lines_to_markdown(text_before_citation),
"text_after_citation": await lines_to_markdown(text_after_citation),
"citation": await lines_to_markdown(citation),
"anchor_added": anchor_added,
"user": await get_current_user(request),
}
),
)
|