html_generator.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. '''
  2. This is a library for formatting GPT-4chan and chat outputs as nice HTML.
  3. '''
  4. import base64
  5. import copy
  6. import os
  7. import re
  8. from io import BytesIO
  9. from pathlib import Path
  10. from PIL import Image
  11. # This is to store chat profile pictures as base64-encoded thumbnails
  12. image_cache = {}
  13. def generate_basic_html(s):
  14. css = """
  15. .container {
  16. max-width: 600px;
  17. margin-left: auto;
  18. margin-right: auto;
  19. background-color: rgb(31, 41, 55);
  20. padding:3em;
  21. }
  22. .container p {
  23. font-size: 16px !important;
  24. color: white !important;
  25. margin-bottom: 22px;
  26. line-height: 1.4 !important;
  27. }
  28. """
  29. s = '\n'.join([f'<p>{line}</p>' for line in s.split('\n')])
  30. s = f'<style>{css}</style><div class="container">{s}</div>'
  31. return s
  32. def process_post(post, c):
  33. t = post.split('\n')
  34. number = t[0].split(' ')[1]
  35. if len(t) > 1:
  36. src = '\n'.join(t[1:])
  37. else:
  38. src = ''
  39. src = re.sub('>', '&gt;', src)
  40. src = re.sub('(&gt;&gt;[0-9]*)', '<span class="quote">\\1</span>', src)
  41. src = re.sub('\n', '<br>\n', src)
  42. src = f'<blockquote class="message">{src}\n'
  43. src = f'<span class="name">Anonymous </span> <span class="number">No.{number}</span>\n{src}'
  44. return src
  45. def generate_4chan_html(f):
  46. css = """
  47. #parent #container {
  48. background-color: #eef2ff;
  49. padding: 17px;
  50. }
  51. #parent #container .reply {
  52. background-color: rgb(214, 218, 240);
  53. border-bottom-color: rgb(183, 197, 217);
  54. border-bottom-style: solid;
  55. border-bottom-width: 1px;
  56. border-image-outset: 0;
  57. border-image-repeat: stretch;
  58. border-image-slice: 100%;
  59. border-image-source: none;
  60. border-image-width: 1;
  61. border-left-color: rgb(0, 0, 0);
  62. border-left-style: none;
  63. border-left-width: 0px;
  64. border-right-color: rgb(183, 197, 217);
  65. border-right-style: solid;
  66. border-right-width: 1px;
  67. border-top-color: rgb(0, 0, 0);
  68. border-top-style: none;
  69. border-top-width: 0px;
  70. color: rgb(0, 0, 0);
  71. display: table;
  72. font-family: arial, helvetica, sans-serif;
  73. font-size: 13.3333px;
  74. margin-bottom: 4px;
  75. margin-left: 0px;
  76. margin-right: 0px;
  77. margin-top: 4px;
  78. overflow-x: hidden;
  79. overflow-y: hidden;
  80. padding-bottom: 4px;
  81. padding-left: 2px;
  82. padding-right: 2px;
  83. padding-top: 4px;
  84. }
  85. #parent #container .number {
  86. color: rgb(0, 0, 0);
  87. font-family: arial, helvetica, sans-serif;
  88. font-size: 13.3333px;
  89. width: 342.65px;
  90. margin-right: 7px;
  91. }
  92. #parent #container .op {
  93. color: rgb(0, 0, 0);
  94. font-family: arial, helvetica, sans-serif;
  95. font-size: 13.3333px;
  96. margin-bottom: 8px;
  97. margin-left: 0px;
  98. margin-right: 0px;
  99. margin-top: 4px;
  100. overflow-x: hidden;
  101. overflow-y: hidden;
  102. }
  103. #parent #container .op blockquote {
  104. margin-left: 0px !important;
  105. }
  106. #parent #container .name {
  107. color: rgb(17, 119, 67);
  108. font-family: arial, helvetica, sans-serif;
  109. font-size: 13.3333px;
  110. font-weight: 700;
  111. margin-left: 7px;
  112. }
  113. #parent #container .quote {
  114. color: rgb(221, 0, 0);
  115. font-family: arial, helvetica, sans-serif;
  116. font-size: 13.3333px;
  117. text-decoration-color: rgb(221, 0, 0);
  118. text-decoration-line: underline;
  119. text-decoration-style: solid;
  120. text-decoration-thickness: auto;
  121. }
  122. #parent #container .greentext {
  123. color: rgb(120, 153, 34);
  124. font-family: arial, helvetica, sans-serif;
  125. font-size: 13.3333px;
  126. }
  127. #parent #container blockquote {
  128. margin: 0px !important;
  129. margin-block-start: 1em;
  130. margin-block-end: 1em;
  131. margin-inline-start: 40px;
  132. margin-inline-end: 40px;
  133. margin-top: 13.33px !important;
  134. margin-bottom: 13.33px !important;
  135. margin-left: 40px !important;
  136. margin-right: 40px !important;
  137. }
  138. #parent #container .message {
  139. color: black;
  140. border: none;
  141. }
  142. """
  143. posts = []
  144. post = ''
  145. c = -2
  146. for line in f.splitlines():
  147. line += "\n"
  148. if line == '-----\n':
  149. continue
  150. elif line.startswith('--- '):
  151. c += 1
  152. if post != '':
  153. src = process_post(post, c)
  154. posts.append(src)
  155. post = line
  156. else:
  157. post += line
  158. if post != '':
  159. src = process_post(post, c)
  160. posts.append(src)
  161. for i in range(len(posts)):
  162. if i == 0:
  163. posts[i] = f'<div class="op">{posts[i]}</div>\n'
  164. else:
  165. posts[i] = f'<div class="reply">{posts[i]}</div>\n'
  166. output = ''
  167. output += f'<style>{css}</style><div id="parent"><div id="container">'
  168. for post in posts:
  169. output += post
  170. output += '</div></div>'
  171. output = output.split('\n')
  172. for i in range(len(output)):
  173. output[i] = re.sub(r'^(&gt;(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
  174. output[i] = re.sub(r'^<blockquote class="message">(&gt;(.*?)(<br>|</div>))', r'<blockquote class="message"><span class="greentext">\1</span>', output[i])
  175. output = '\n'.join(output)
  176. return output
  177. def image_to_base64(path):
  178. mtime = os.stat(path).st_mtime
  179. if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache):
  180. img = Image.open(path)
  181. img.thumbnail((100, 100))
  182. img_buffer = BytesIO()
  183. img.convert('RGB').save(img_buffer, format='PNG')
  184. image_cache[path] = [mtime, base64.b64encode(img_buffer.getvalue()).decode("utf-8")]
  185. return image_cache[path][1]
  186. def generate_chat_html(history, name1, name2, character):
  187. css = """
  188. .chat {
  189. margin-left: auto;
  190. margin-right: auto;
  191. max-width: 800px;
  192. height: 66.67vh;
  193. overflow-y: auto;
  194. padding-right: 20px;
  195. display: flex;
  196. flex-direction: column-reverse;
  197. }
  198. .message {
  199. display: grid;
  200. grid-template-columns: 60px 1fr;
  201. padding-bottom: 25px;
  202. font-size: 15px;
  203. font-family: Helvetica, Arial, sans-serif;
  204. line-height: 1.428571429;
  205. }
  206. .circle-you {
  207. width: 50px;
  208. height: 50px;
  209. background-color: rgb(238, 78, 59);
  210. border-radius: 50%;
  211. }
  212. .circle-bot {
  213. width: 50px;
  214. height: 50px;
  215. background-color: rgb(59, 78, 244);
  216. border-radius: 50%;
  217. }
  218. .circle-bot img, .circle-you img {
  219. border-radius: 50%;
  220. width: 100%;
  221. height: 100%;
  222. object-fit: cover;
  223. }
  224. .text {
  225. }
  226. .text p {
  227. margin-top: 5px;
  228. }
  229. .username {
  230. font-weight: bold;
  231. }
  232. .message-body {
  233. }
  234. .message-body img {
  235. max-width: 300px;
  236. max-height: 300px;
  237. border-radius: 20px;
  238. }
  239. .message-body p {
  240. margin-bottom: 0 !important;
  241. font-size: 15px !important;
  242. line-height: 1.428571429 !important;
  243. }
  244. .dark .message-body p em {
  245. color: rgb(138, 138, 138) !important;
  246. }
  247. .message-body p em {
  248. color: rgb(110, 110, 110) !important;
  249. }
  250. """
  251. output = ''
  252. output += f'<style>{css}</style><div class="chat" id="chat">'
  253. img = ''
  254. for i in [
  255. f"characters/{character}.png",
  256. f"characters/{character}.jpg",
  257. f"characters/{character}.jpeg",
  258. "img_bot.png",
  259. "img_bot.jpg",
  260. "img_bot.jpeg"
  261. ]:
  262. path = Path(i)
  263. if path.exists():
  264. img = f'<img src="data:image/png;base64,{image_to_base64(path)}">'
  265. break
  266. img_me = ''
  267. for i in ["img_me.png", "img_me.jpg", "img_me.jpeg"]:
  268. path = Path(i)
  269. if path.exists():
  270. img_me = f'<img src="data:image/png;base64,{image_to_base64(path)}">'
  271. break
  272. for i,_row in enumerate(history[::-1]):
  273. row = _row.copy()
  274. row[0] = re.sub(r"(\*\*)([^\*\n]*)(\*\*)", r"<b>\2</b>", row[0])
  275. row[1] = re.sub(r"(\*\*)([^\*\n]*)(\*\*)", r"<b>\2</b>", row[1])
  276. row[0] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[0])
  277. row[1] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[1])
  278. p = '\n'.join([f"<p>{x}</p>" for x in row[1].split('\n')])
  279. output += f"""
  280. <div class="message">
  281. <div class="circle-bot">
  282. {img}
  283. </div>
  284. <div class="text">
  285. <div class="username">
  286. {name2}
  287. </div>
  288. <div class="message-body">
  289. {p}
  290. </div>
  291. </div>
  292. </div>
  293. """
  294. if not (i == len(history)-1 and len(row[0]) == 0):
  295. p = '\n'.join([f"<p>{x}</p>" for x in row[0].split('\n')])
  296. output += f"""
  297. <div class="message">
  298. <div class="circle-you">
  299. {img_me}
  300. </div>
  301. <div class="text">
  302. <div class="username">
  303. {name1}
  304. </div>
  305. <div class="message-body">
  306. {p}
  307. </div>
  308. </div>
  309. </div>
  310. """
  311. output += "</div>"
  312. return output