-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
67 lines (52 loc) · 1.36 KB
/
helper.py
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
"""
Helper functions for the app.
"""
# fastapi imports
from fastapi.templating import Jinja2Templates
# langchain imports
from langchain.llms.ctransformers import CTransformers
from langchain_openai.llms import OpenAI
# for pdf generation
from xhtml2pdf import pisa
# env variables
from constants import MODEL_PATH, OPENAI_API_KEY, USE_OPENAI
def load_model(
temperature=0,
):
"""
Load the model from the environment variables.
"""
if USE_OPENAI:
return OpenAI(
temperature=temperature,
api_key=OPENAI_API_KEY,
)
return CTransformers(
model=MODEL_PATH,
model_type="llama",
max_new_tokens=1024,
temperature=temperature,
config={
"context_length": 2048,
},
)
def render_to_pdf(
template_engine: Jinja2Templates,
template_src: str,
context_dict: dict,
):
"""
Shortcut function to render a template into a PDF.
Returns PDF value.
Args:
template_engine (Jinja2Templates): Jinja2Templates instance.
template_src (str): Template source.
context_dict (dict): Context dictionary.
"""
html = template_engine.get_template(template_src).render(context_dict)
pdf_data = pisa.CreatePDF(
html,
encoding="utf-8",
raise_exception=True,
)
return pdf_data.dest.getvalue()