html_generator.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. '''
  2. This is a library for formatting GPT-4chan and chat outputs as nice HTML.
  3. '''
  4. import re
  5. from pathlib import Path
  6. import copy
  7. def generate_basic_html(s):
  8. s = '\n'.join([f'<p style="margin-bottom: 20px">{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">{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: 20px;
  162. font-size: 15px;
  163. font-family: helvetica;
  164. }
  165. .circle-you {
  166. width: 45px;
  167. height: 45px;
  168. background-color: rgb(244, 78, 59);
  169. border-radius: 50%;
  170. }
  171. .circle-bot {
  172. width: 45px;
  173. height: 45px;
  174. background-color: rgb(59, 78, 244);
  175. border-radius: 50%;
  176. }
  177. .circle-bot img, .circle-you img {
  178. border-radius: 50%;
  179. width: 100%;
  180. height: 100%;
  181. object-fit: cover;
  182. }
  183. .text {
  184. }
  185. .text p {
  186. margin-top: 5px;
  187. }
  188. .username {
  189. font-weight: bold;
  190. }
  191. .body {
  192. }
  193. """
  194. output = ''
  195. output += f'<style>{css}</style><div class="chat" id="chat">'
  196. img = ''
  197. for i in [
  198. f"characters/{character}.png",
  199. f"characters/{character}.jpg",
  200. f"characters/{character}.jpeg",
  201. "img_bot.png",
  202. "img_bot.jpg",
  203. "img_bot.jpeg"
  204. ]:
  205. if Path(i).exists():
  206. img = f'<img src="file/{i}">'
  207. break
  208. img_you = ''
  209. for i in ["img_me.png", "img_me.jpg", "img_me.jpeg"]:
  210. if Path(i).exists():
  211. img_you = f'<img src="file/{i}">'
  212. break
  213. for i,_row in enumerate(history[::-1]):
  214. row = _row.copy()
  215. row[0] = re.sub(r"(\*\*)([^\*\n]*)(\*\*)", r"<b>\2</b>", row[0])
  216. row[1] = re.sub(r"(\*\*)([^\*\n]*)(\*\*)", r"<b>\2</b>", row[1])
  217. row[0] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[0])
  218. row[1] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[1])
  219. p = '\n'.join([f"<p>{x}</p>" for x in row[1].split('\n')])
  220. output += f"""
  221. <div class="message">
  222. <div class="circle-bot">
  223. {img}
  224. </div>
  225. <div class="text">
  226. <div class="username">
  227. {name2}
  228. </div>
  229. <div class="body">
  230. {p}
  231. </div>
  232. </div>
  233. </div>
  234. """
  235. if not (i == len(history)-1 and len(row[0]) == 0):
  236. p = '\n'.join([f"<p>{x}</p>" for x in row[0].split('\n')])
  237. output += f"""
  238. <div class="message">
  239. <div class="circle-you">
  240. {img_you}
  241. </div>
  242. <div class="text">
  243. <div class="username">
  244. {name1}
  245. </div>
  246. <div class="body">
  247. {p}
  248. </div>
  249. </div>
  250. </div>
  251. """
  252. output += "</div>"
  253. return output