html_generator.py 8.5 KB

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