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 os
  5. import re
  6. from pathlib import Path
  7. from PIL import Image
  8. # This is to store the paths to the thumbnails of the profile pictures
  9. image_cache = {}
  10. def generate_basic_html(s):
  11. css = """
  12. .container {
  13. max-width: 600px;
  14. margin-left: auto;
  15. margin-right: auto;
  16. background-color: rgb(31, 41, 55);
  17. padding:3em;
  18. }
  19. .container p {
  20. font-size: 16px !important;
  21. color: white !important;
  22. margin-bottom: 22px;
  23. line-height: 1.4 !important;
  24. }
  25. """
  26. s = '\n'.join([f'<p>{line}</p>' for line in s.split('\n')])
  27. s = f'<style>{css}</style><div class="container">{s}</div>'
  28. return s
  29. def process_post(post, c):
  30. t = post.split('\n')
  31. number = t[0].split(' ')[1]
  32. if len(t) > 1:
  33. src = '\n'.join(t[1:])
  34. else:
  35. src = ''
  36. src = re.sub('>', '&gt;', src)
  37. src = re.sub('(&gt;&gt;[0-9]*)', '<span class="quote">\\1</span>', src)
  38. src = re.sub('\n', '<br>\n', src)
  39. src = f'<blockquote class="message">{src}\n'
  40. src = f'<span class="name">Anonymous </span> <span class="number">No.{number}</span>\n{src}'
  41. return src
  42. def generate_4chan_html(f):
  43. css = """
  44. #parent #container {
  45. background-color: #eef2ff;
  46. padding: 17px;
  47. }
  48. #parent #container .reply {
  49. background-color: rgb(214, 218, 240);
  50. border-bottom-color: rgb(183, 197, 217);
  51. border-bottom-style: solid;
  52. border-bottom-width: 1px;
  53. border-image-outset: 0;
  54. border-image-repeat: stretch;
  55. border-image-slice: 100%;
  56. border-image-source: none;
  57. border-image-width: 1;
  58. border-left-color: rgb(0, 0, 0);
  59. border-left-style: none;
  60. border-left-width: 0px;
  61. border-right-color: rgb(183, 197, 217);
  62. border-right-style: solid;
  63. border-right-width: 1px;
  64. border-top-color: rgb(0, 0, 0);
  65. border-top-style: none;
  66. border-top-width: 0px;
  67. color: rgb(0, 0, 0);
  68. display: table;
  69. font-family: arial, helvetica, sans-serif;
  70. font-size: 13.3333px;
  71. margin-bottom: 4px;
  72. margin-left: 0px;
  73. margin-right: 0px;
  74. margin-top: 4px;
  75. overflow-x: hidden;
  76. overflow-y: hidden;
  77. padding-bottom: 4px;
  78. padding-left: 2px;
  79. padding-right: 2px;
  80. padding-top: 4px;
  81. }
  82. #parent #container .number {
  83. color: rgb(0, 0, 0);
  84. font-family: arial, helvetica, sans-serif;
  85. font-size: 13.3333px;
  86. width: 342.65px;
  87. margin-right: 7px;
  88. }
  89. #parent #container .op {
  90. color: rgb(0, 0, 0);
  91. font-family: arial, helvetica, sans-serif;
  92. font-size: 13.3333px;
  93. margin-bottom: 8px;
  94. margin-left: 0px;
  95. margin-right: 0px;
  96. margin-top: 4px;
  97. overflow-x: hidden;
  98. overflow-y: hidden;
  99. }
  100. #parent #container .op blockquote {
  101. margin-left: 0px !important;
  102. }
  103. #parent #container .name {
  104. color: rgb(17, 119, 67);
  105. font-family: arial, helvetica, sans-serif;
  106. font-size: 13.3333px;
  107. font-weight: 700;
  108. margin-left: 7px;
  109. }
  110. #parent #container .quote {
  111. color: rgb(221, 0, 0);
  112. font-family: arial, helvetica, sans-serif;
  113. font-size: 13.3333px;
  114. text-decoration-color: rgb(221, 0, 0);
  115. text-decoration-line: underline;
  116. text-decoration-style: solid;
  117. text-decoration-thickness: auto;
  118. }
  119. #parent #container .greentext {
  120. color: rgb(120, 153, 34);
  121. font-family: arial, helvetica, sans-serif;
  122. font-size: 13.3333px;
  123. }
  124. #parent #container blockquote {
  125. margin: 0px !important;
  126. margin-block-start: 1em;
  127. margin-block-end: 1em;
  128. margin-inline-start: 40px;
  129. margin-inline-end: 40px;
  130. margin-top: 13.33px !important;
  131. margin-bottom: 13.33px !important;
  132. margin-left: 40px !important;
  133. margin-right: 40px !important;
  134. }
  135. #parent #container .message {
  136. color: black;
  137. border: none;
  138. }
  139. """
  140. posts = []
  141. post = ''
  142. c = -2
  143. for line in f.splitlines():
  144. line += "\n"
  145. if line == '-----\n':
  146. continue
  147. elif line.startswith('--- '):
  148. c += 1
  149. if post != '':
  150. src = process_post(post, c)
  151. posts.append(src)
  152. post = line
  153. else:
  154. post += line
  155. if post != '':
  156. src = process_post(post, c)
  157. posts.append(src)
  158. for i in range(len(posts)):
  159. if i == 0:
  160. posts[i] = f'<div class="op">{posts[i]}</div>\n'
  161. else:
  162. posts[i] = f'<div class="reply">{posts[i]}</div>\n'
  163. output = ''
  164. output += f'<style>{css}</style><div id="parent"><div id="container">'
  165. for post in posts:
  166. output += post
  167. output += '</div></div>'
  168. output = output.split('\n')
  169. for i in range(len(output)):
  170. output[i] = re.sub(r'^(&gt;(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
  171. output[i] = re.sub(r'^<blockquote class="message">(&gt;(.*?)(<br>|</div>))', r'<blockquote class="message"><span class="greentext">\1</span>', output[i])
  172. output = '\n'.join(output)
  173. return output
  174. def get_image_cache(path):
  175. cache_folder = Path("cache")
  176. if not cache_folder.exists():
  177. cache_folder.mkdir()
  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((200, 200))
  182. output_file = Path(f'cache/{path.name}_cache.png')
  183. img.convert('RGB').save(output_file, format='PNG')
  184. image_cache[path] = [mtime, output_file.as_posix()]
  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="file/{get_image_cache(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="file/{get_image_cache(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