chinese-langchain/app_modules/overwrites.py

50 lines
1.5 KiB
Python
Raw Normal View History

2023-04-19 01:46:46 +08:00
from __future__ import annotations
from typing import List, Tuple
from app_modules.utils import *
def postprocess(
2023-04-19 02:09:00 +08:00
self, y: List[Tuple[str | None, str | None]]
2023-04-19 01:46:46 +08:00
) -> List[Tuple[str | None, str | None]]:
"""
Parameters:
y: List of tuples representing the message and response pairs. Each message and response should be a string, which may be in Markdown format.
Returns:
List of tuples representing the message and response. Each message and response will be a string of HTML.
"""
if y is None or y == []:
return []
temp = []
for x in y:
user, bot = x
if not detect_converted_mark(user):
user = convert_asis(user)
if not detect_converted_mark(bot):
bot = convert_mdtext(bot)
temp.append((user, bot))
return temp
2023-04-19 02:09:00 +08:00
with open("./assets/custom.js", "r", encoding="utf-8") as f, open("./assets/Kelpy-Codos.js", "r",
encoding="utf-8") as f2:
2023-04-19 01:46:46 +08:00
customJS = f.read()
kelpyCodos = f2.read()
2023-04-19 02:09:00 +08:00
2023-04-19 01:46:46 +08:00
def reload_javascript():
print("Reloading javascript...")
js = f'<script>{customJS}</script><script>{kelpyCodos}</script>'
2023-04-19 02:09:00 +08:00
2023-04-19 01:46:46 +08:00
def template_response(*args, **kwargs):
res = GradioTemplateResponseOriginal(*args, **kwargs)
res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))
res.init_headers()
return res
gr.routes.templates.TemplateResponse = template_response
2023-04-19 02:09:00 +08:00
2023-04-19 01:46:46 +08:00
GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse