Intoval/privateChatGPT
1
1from __future__ import annotations2import logging3 4from llama_index import Prompt5from typing import List, Tuple6import mdtex2html7from gradio_client import utils as client_utils8 9from modules.presets import *10from modules.llama_func import *11 12 13def compact_text_chunks(self, prompt: Prompt, text_chunks: List[str]) -> List[str]:14 logging.debug("Compacting text chunks...๐๐๐")15 combined_str = [c.strip() for c in text_chunks if c.strip()]16 combined_str = [f"[{index+1}] {c}" for index, c in enumerate(combined_str)]17 combined_str = "\n\n".join(combined_str)18 # resplit based on self.max_chunk_overlap19 text_splitter = self.get_text_splitter_given_prompt(prompt, 1, padding=1)20 return text_splitter.split_text(combined_str)21 22 23def postprocess(24 self,25 y: List[List[str | Tuple[str] | Tuple[str, str] | None] | Tuple],26 ) -> List[List[str | Dict | None]]:27 """28 Parameters:29 y: List of lists representing the message and response pairs. Each message and response should be a string, which may be in Markdown format. It can also be a tuple whose first element is a string filepath or URL to an image/video/audio, and second (optional) element is the alt text, in which case the media file is displayed. It can also be None, in which case that message is not displayed.30 Returns:31 List of lists representing the message and response. Each message and response will be a string of HTML, or a dictionary with media information. Or None if the message is not to be displayed.32 """33 if y is None:34 return []35 processed_messages = []36 for message_pair in y:37 assert isinstance(38 message_pair, (tuple, list)39 ), f"Expected a list of lists or list of tuples. Received: {message_pair}"40 assert (41 len(message_pair) == 242 ), f"Expected a list of lists of length 2 or list of tuples of length 2. Received: {message_pair}"43 44 processed_messages.append(45 [46 self._postprocess_chat_messages(message_pair[0], "user"),47 self._postprocess_chat_messages(message_pair[1], "bot"),48 ]49 )50 return processed_messages51 52def postprocess_chat_messages(53 self, chat_message: str | Tuple | List | None, message_type: str54 ) -> str | Dict | None:55 if chat_message is None:56 return None57 elif isinstance(chat_message, (tuple, list)):58 filepath = chat_message[0]59 mime_type = client_utils.get_mimetype(filepath)60 filepath = self.make_temp_copy_if_needed(filepath)61 return {62 "name": filepath,63 "mime_type": mime_type,64 "alt_text": chat_message[1] if len(chat_message) > 1 else None,65 "data": None, # These last two fields are filled in by the frontend66 "is_file": True,67 }68 elif isinstance(chat_message, str):69 if message_type == "bot":70 if not detect_converted_mark(chat_message):71 chat_message = convert_mdtext(chat_message)72 elif message_type == "user":73 if not detect_converted_mark(chat_message):74 chat_message = convert_asis(chat_message)75 return chat_message76 else:77 raise ValueError(f"Invalid message for Chatbot component: {chat_message}")78 79with open("./assets/custom.js", "r", encoding="utf-8") as f, open("./assets/Kelpy-Codos.js", "r", encoding="utf-8") as f2:80 customJS = f.read()81 kelpyCodos = f2.read()82 83def reload_javascript():84 print("Reloading javascript...")85 js = f'<script>{customJS}</script><script>{kelpyCodos}</script>'86 def template_response(*args, **kwargs):87 res = GradioTemplateResponseOriginal(*args, **kwargs)88 res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))89 res.init_headers()90 return res91 92 gr.routes.templates.TemplateResponse = template_response93 94GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse