html_generator.py 7.7 KB

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