DeepSeek-VL/deepseek_vl/serve/app_modules/overwrites.py
haoy945 601426030d
feat: gradio demo integration (#16)
Co-authored-by: Bo Liu <benjaminliu.eecs@gmail.com>
Co-authored-by: Haoyu Lu <ruclhy1998@163.com>
2024-03-13 17:47:43 +08:00

82 lines
3.2 KiB
Python
Executable File

# Copyright (c) 2023-2024 DeepSeek.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
import logging
from typing import List, Tuple
from app_modules.presets import gr
from app_modules.utils import convert_asis, convert_mdtext, detect_converted_mark
def compact_text_chunks(self, prompt, text_chunks: List[str]) -> List[str]:
logging.debug("Compacting text chunks...🚀🚀🚀")
combined_str = [c.strip() for c in text_chunks if c.strip()]
combined_str = [f"[{index+1}] {c}" for index, c in enumerate(combined_str)]
combined_str = "\n\n".join(combined_str)
# resplit based on self.max_chunk_overlap
text_splitter = self.get_text_splitter_given_prompt(prompt, 1, padding=1)
return text_splitter.split_text(combined_str)
def postprocess(
self, y: List[Tuple[str | None, str | None]]
) -> 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
with open("deepseek_vl/serve/assets/custom.js", "r", encoding="utf-8") as f, open(
"deepseek_vl/serve/assets/Kelpy-Codos.js", "r", encoding="utf-8"
) as f2:
customJS = f.read()
kelpyCodos = f2.read()
def reload_javascript():
print("Reloading javascript...")
js = f"<script>{customJS}</script><script>{kelpyCodos}</script>"
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
GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse