File size: 5,225 Bytes
365de9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
from fastapi.templating import Jinja2Templates
from fastapi import Request, UploadFile

from app.backend.controllers.chats import list_user_chats, verify_ownership_rights
from app.backend.controllers.users import get_current_user
from app.backend.models.users import User
from app.core.rag_generator import RagSystem
from app.settings import BASE_DIR

from uuid import uuid4
import markdown
import os

rag = None


# <----------------------- System ----------------------->
def initialize_rag() -> RagSystem:
    global rag
    if rag is None:
        rag = RagSystem()
    return rag


# <----------------------- Tools ----------------------->
"""
Updates response context and adds context of navbar (role, instance(or none)) and footer (none)
"""


def extend_context(context: dict, selected: int = None):
    user = 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": 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


"""
Validates chat viewing permission by comparing user's chats and requested one
"""


def protect_chat(user: User, chat_id: int) -> bool:
    return verify_ownership_rights(user, chat_id)


async def save_documents(
    collection_name: str,
    files: list[UploadFile],
    RAG: RagSystem,
    user: User,
    chat_id: int,
) -> None:
    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

    os.makedirs(os.path.join(storage, "pdfs"), exist_ok=True)

    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]
            )

        with open(saved_file, "wb") as f:
            f.write(content)

        docs.append(saved_file)

    if len(files) > 0:
        RAG.upload_documents(collection_name, docs)


def get_pdf_path(path: str) -> str:
    parts = path.split("chats_storage")
    if len(parts) < 2:
        return ""
    return "chats_storage" + "".join(parts[1:])


def construct_collection_name(user: User, chat_id: int) -> str:
    return f"user_id_{user.id}_chat_id_{chat_id}"


def create_collection(user: User, chat_id: int, RAG: RagSystem) -> None:
    if RAG is None:
        raise RuntimeError("RAG was not initialized")

    RAG.create_new_collection(construct_collection_name(user, chat_id))
    print(rag.get_collections_names())


def lines_to_markdown(lines: list[str]) -> list[str]:
    return [markdown.markdown(line) for line in lines]


# <----------------------- Handlers ----------------------->
def PDFHandler(
    request: Request, path: str, page: int, templates
) -> Jinja2Templates.TemplateResponse:
    print(path)
    url_path = get_pdf_path(path=path)
    print(url_path)

    current_template = "pages/show_pdf.html"
    return templates.TemplateResponse(
        current_template,
        extend_context(
            {
                "request": request,
                "page": str(page or 1),
                "url_path": url_path,
                "user": get_current_user(request),
            }
        ),
    )


def TextHandler(
    request: Request, path: str, lines: str, templates
) -> Jinja2Templates.TemplateResponse:
    file_content = ""
    with open(path, "r") as f:
        file_content = 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,
        extend_context(
            {
                "request": request,
                "text_before_citation": lines_to_markdown(text_before_citation),
                "text_after_citation": lines_to_markdown(text_after_citation),
                "citation": lines_to_markdown(citation),
                "anchor_added": anchor_added,
                "user": get_current_user(request),
            }
        ),
    )


"""
Optional handler
"""


def DocHandler():
    pass