html_generator.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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: 14px !important;
  24. color: white !important;
  25. font-family: Helvetica, Arial, sans-serif !important;
  26. margin-bottom: 22px;
  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: 2px;
  81. padding-left: 2px;
  82. padding-right: 2px;
  83. padding-top: 2px;
  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. }
  91. #parent #container .op {
  92. color: rgb(0, 0, 0);
  93. font-family: arial, helvetica, sans-serif;
  94. font-size: 13.3333px;
  95. margin-bottom: 8px;
  96. margin-left: 0px;
  97. margin-right: 0px;
  98. margin-top: 4px;
  99. overflow-x: hidden;
  100. overflow-y: hidden;
  101. }
  102. #parent #container .op blockquote {
  103. margin-left: 0px !important;
  104. }
  105. #parent #container .name {
  106. color: rgb(17, 119, 67);
  107. font-family: arial, helvetica, sans-serif;
  108. font-size: 13.3333px;
  109. font-weight: 700;
  110. margin-left: 7px;
  111. }
  112. #parent #container .quote {
  113. color: rgb(221, 0, 0);
  114. font-family: arial, helvetica, sans-serif;
  115. font-size: 13.3333px;
  116. text-decoration-color: rgb(221, 0, 0);
  117. text-decoration-line: underline;
  118. text-decoration-style: solid;
  119. text-decoration-thickness: auto;
  120. }
  121. #parent #container .greentext {
  122. color: rgb(120, 153, 34);
  123. font-family: arial, helvetica, sans-serif;
  124. font-size: 13.3333px;
  125. }
  126. #parent #container blockquote {
  127. margin: 0px !important;
  128. margin-block-start: 1em;
  129. margin-block-end: 1em;
  130. margin-inline-start: 40px;
  131. margin-inline-end: 40px;
  132. margin-top: 13.33px !important;
  133. margin-bottom: 13.33px !important;
  134. margin-left: 40px !important;
  135. margin-right: 40px !important;
  136. }
  137. #parent #container .message {
  138. color: black;
  139. border: none;
  140. }
  141. """
  142. posts = []
  143. post = ''
  144. c = -2
  145. for line in f.splitlines():
  146. line += "\n"
  147. if line == '-----\n':
  148. continue
  149. elif line.startswith('--- '):
  150. c += 1
  151. if post != '':
  152. src = process_post(post, c)
  153. posts.append(src)
  154. post = line
  155. else:
  156. post += line
  157. if post != '':
  158. src = process_post(post, c)
  159. posts.append(src)
  160. for i in range(len(posts)):
  161. if i == 0:
  162. posts[i] = f'<div class="op">{posts[i]}</div>\n'
  163. else:
  164. posts[i] = f'<div class="reply">{posts[i]}</div>\n'
  165. output = ''
  166. output += f'<style>{css}</style><div id="parent"><div id="container">'
  167. for post in posts:
  168. output += post
  169. output += '</div></div>'
  170. output = output.split('\n')
  171. for i in range(len(output)):
  172. output[i] = re.sub(r'^(&gt;(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
  173. output[i] = re.sub(r'^<blockquote class="message">(&gt;(.*?)(<br>|</div>))', r'<blockquote class="message"><span class="greentext">\1</span>', output[i])
  174. output = '\n'.join(output)
  175. return output
  176. def image_to_base64(path):
  177. mtime = os.stat(path).st_mtime
  178. if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache):
  179. img = Image.open(path)
  180. img.thumbnail((100, 100))
  181. img_buffer = BytesIO()
  182. img.convert('RGB').save(img_buffer, format='PNG')
  183. image_cache[path] = [mtime, base64.b64encode(img_buffer.getvalue()).decode("utf-8")]
  184. return image_cache[path][1]
  185. def generate_chat_html(history, name1, name2, character):
  186. css = """
  187. .chat {
  188. margin-left: auto;
  189. margin-right: auto;
  190. max-width: 800px;
  191. height: 66.67vh;
  192. overflow-y: auto;
  193. padding-right: 20px;
  194. display: flex;
  195. flex-direction: column-reverse;
  196. }
  197. .message {
  198. display: grid;
  199. grid-template-columns: 60px 1fr;
  200. padding-bottom: 22px;
  201. font-size: 15px;
  202. font-family: Helvetica, Arial, sans-serif;
  203. line-height: 1.428571429;
  204. }
  205. .circle-you {
  206. width: 50px;
  207. height: 50px;
  208. background-color: rgb(244, 78, 59);
  209. border-radius: 50%;
  210. }
  211. .circle-bot {
  212. width: 50px;
  213. height: 50px;
  214. background-color: rgb(59, 78, 244);
  215. border-radius: 50%;
  216. }
  217. .circle-bot img, .circle-you img {
  218. border-radius: 50%;
  219. width: 100%;
  220. height: 100%;
  221. object-fit: cover;
  222. }
  223. .text {
  224. }
  225. .text p {
  226. margin-top: 5px;
  227. }
  228. .username {
  229. font-weight: bold;
  230. }
  231. .message-body {
  232. }
  233. .message-body img {
  234. max-width: 300px;
  235. max-height: 300px;
  236. border-radius: 20px;
  237. }
  238. .message-body p {
  239. margin-bottom: 0 !important;
  240. font-size: 15px !important;
  241. line-height: 1.428571429 !important;
  242. }
  243. """
  244. output = ''
  245. output += f'<style>{css}</style><div class="chat" id="chat">'
  246. img = ''
  247. for i in [
  248. f"characters/{character}.png",
  249. f"characters/{character}.jpg",
  250. f"characters/{character}.jpeg",
  251. "img_bot.png",
  252. "img_bot.jpg",
  253. "img_bot.jpeg"
  254. ]:
  255. path = Path(i)
  256. if path.exists():
  257. img = f'<img src="data:image/png;base64,{image_to_base64(path)}">'
  258. break
  259. img_me = ''
  260. for i in ["img_me.png", "img_me.jpg", "img_me.jpeg"]:
  261. path = Path(i)
  262. if path.exists():
  263. img_me = f'<img src="data:image/png;base64,{image_to_base64(path)}">'
  264. break
  265. for i,_row in enumerate(history[::-1]):
  266. row = _row.copy()
  267. row[0] = re.sub(r"(\*\*)([^\*\n]*)(\*\*)", r"<b>\2</b>", row[0])
  268. row[1] = re.sub(r"(\*\*)([^\*\n]*)(\*\*)", r"<b>\2</b>", row[1])
  269. row[0] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[0])
  270. row[1] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[1])
  271. p = '\n'.join([f"<p>{x}</p>" for x in row[1].split('\n')])
  272. output += f"""
  273. <div class="message">
  274. <div class="circle-bot">
  275. {img}
  276. </div>
  277. <div class="text">
  278. <div class="username">
  279. {name2}
  280. </div>
  281. <div class="message-body">
  282. {p}
  283. </div>
  284. </div>
  285. </div>
  286. """
  287. if not (i == len(history)-1 and len(row[0]) == 0):
  288. p = '\n'.join([f"<p>{x}</p>" for x in row[0].split('\n')])
  289. output += f"""
  290. <div class="message">
  291. <div class="circle-you">
  292. {img_me}
  293. </div>
  294. <div class="text">
  295. <div class="username">
  296. {name1}
  297. </div>
  298. <div class="message-body">
  299. {p}
  300. </div>
  301. </div>
  302. </div>
  303. """
  304. output += "</div>"
  305. return output