aaaaasss/0406
0
1from __future__ import annotations2import logging3 4from llama_index import Prompt5from typing import List, Tuple6import mdtex2html7 8from modules.presets import *9from modules.llama_func import *10 11 12def compact_text_chunks(self, prompt: Prompt, text_chunks: List[str]) -> List[str]:13 logging.debug("Compacting text chunks...๐๐๐")14 combined_str = [c.strip() for c in text_chunks if c.strip()]15 combined_str = [f"[{index+1}] {c}" for index, c in enumerate(combined_str)]16 combined_str = "\n\n".join(combined_str)17 # resplit based on self.max_chunk_overlap18 text_splitter = self.get_text_splitter_given_prompt(prompt, 1, padding=1)19 return text_splitter.split_text(combined_str)20 21 22def postprocess(23 self, y: List[Tuple[str | None, str | None]]24) -> List[Tuple[str | None, str | None]]:25 """26 Parameters:27 y: List of tuples representing the message and response pairs. Each message and response should be a string, which may be in Markdown format.28 Returns:29 List of tuples representing the message and response. Each message and response will be a string of HTML.30 """31 if y is None or y == []:32 return []33 user, bot = y[-1]34 if not detect_converted_mark(user):35 user = convert_asis(user)36 if not detect_converted_mark(bot):37 bot = convert_mdtext(bot)38 y[-1] = (user, bot)39 return y40 41with open("./assets/custom.js", "r", encoding="utf-8") as f, open("./assets/Kelpy-Codos.js", "r", encoding="utf-8") as f2:42 customJS = f.read()43 kelpyCodos = f2.read()44 45def reload_javascript():46 print("Reloading javascript...")47 js = f'<script>{customJS}</script><script>{kelpyCodos}</script>'48 def template_response(*args, **kwargs):49 res = GradioTemplateResponseOriginal(*args, **kwargs)50 res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))51 res.init_headers()52 return res53 54 gr.routes.templates.TemplateResponse = template_response55 56GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse