File size: 30,521 Bytes
04aed77
 
 
 
 
 
 
eae2ecb
04aed77
d4bdb14
04aed77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44100d0
04aed77
 
 
 
44100d0
5cd650a
 
 
44100d0
04aed77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc569e
04aed77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cd650a
d4bdb14
 
 
 
 
d32760a
 
 
 
 
 
 
bdc569e
d32760a
 
04aed77
 
d32760a
04aed77
 
d32760a
04aed77
d4bdb14
d32760a
 
 
 
 
 
 
 
 
 
 
eae2ecb
 
d32760a
 
 
 
 
 
 
 
eae2ecb
d32760a
 
d4bdb14
 
d32760a
 
 
 
 
d4bdb14
bdc569e
 
 
 
04aed77
d32760a
04aed77
 
d4bdb14
 
eae2ecb
d4bdb14
 
 
 
 
d32760a
04aed77
 
 
d32760a
04aed77
 
d32760a
04aed77
 
d32760a
04aed77
d32760a
04aed77
 
 
d32760a
 
04aed77
d32760a
 
 
 
 
 
 
 
 
eae2ecb
d32760a
 
 
 
 
 
 
 
 
 
 
 
 
eae2ecb
d32760a
 
04aed77
 
60a7b9d
 
 
 
 
 
 
 
 
04aed77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4bdb14
 
 
 
 
 
 
 
 
04aed77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4bdb14
 
 
04aed77
d4bdb14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eae2ecb
04aed77
 
d4bdb14
04aed77
 
d4bdb14
 
 
 
 
04aed77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
import base64
import io
import json
import os
import re
import subprocess
import time
from datetime import datetime
import cairosvg
from concurrent.futures import ThreadPoolExecutor

from PIL import Image
from pdf2image import convert_from_path
from playwright.sync_api import sync_playwright
from pydantic import BaseModel, Field, create_model
from tqdm import tqdm

from langchain import hub

# from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_openai import ChatOpenAI, AzureChatOpenAI
from langchain_openai.chat_models.base import BaseChatOpenAI

from langchain_community.document_loaders import PyMuPDFLoader
from langchain_core.prompts import (
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
    ChatPromptTemplate,
    MessagesPlaceholder,
    PromptTemplate,
)
from langchain_core.prompts.image import ImagePromptTemplate
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.exceptions import OutputParserException
from langchain.output_parsers import OutputFixingParser
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage


def create_dynamic_poster_model(sections: dict[str, str]) -> type[BaseModel]:
    """Dynamically create a Poster model based on sections returned by LLM."""
    fields = {
        "title": (str, Field(default="", description="Title of the paper")),
        "authors": (str, Field(default="", description="Authors of the paper")),
        "affiliation": (
            str,
            Field(default="", description="Affiliation of the authors"),
        ),
    }

    for section_name, description in sections.items():
        fields[section_name] = (str, Field(default="", description=description))

    return create_model("DynamicPoster", **fields)


def remove_think_tags(llm_output):
    if hasattr(llm_output, "content"):
        content = llm_output.content
        cleaned_content = re.sub(r"<think>.*?</think>", "", content, flags=re.DOTALL)
        cleaned_content = re.sub(r"<think>.*", "", cleaned_content, flags=re.DOTALL)
        return AIMessage(content=cleaned_content)
    elif isinstance(llm_output, str):
        cleaned_output = re.sub(r"<think>.*?</think>", "", llm_output, flags=re.DOTALL)
        cleaned_output = re.sub(r"<think>.*", "", cleaned_output, flags=re.DOTALL)
        return cleaned_output
    return llm_output


def replace_figures_in_markdown(
    markdown: str,
    figures: list[str],
) -> str:
    pattern = r"!\[(.*?)\]\((\d+)\)"

    def replacer(match):
        figure_index = int(match.group(2))
        if 0 <= figure_index < len(figures):
            return f"![{match.group(1)}]({figures[figure_index]})"
        return match.group(0)

    return re.sub(pattern, replacer, markdown)


def replace_figures_in_poster(
    poster: BaseModel,
    figures: list[str],
) -> BaseModel:
    for field in poster.model_fields:
        if hasattr(poster, field):
            value = getattr(poster, field)
            if isinstance(value, str):
                setattr(poster, field, replace_figures_in_markdown(value, figures))
    return poster


def replace_figures_size_in_markdown(
    markdown: str,
    figures: list[str],
) -> str:
    pattern = r"!\[(.*?)\]\((\d+)\)"

    def replacer(match):
        figure_index = int(match.group(2))
        if 0 <= figure_index < len(figures):
            data = base64.b64decode(figures[figure_index])
            image = Image.open(io.BytesIO(data))
            width, height = image.size
            return f"![{match.group(1)}, width = {width}, height = {height}, aspect ratio = {width / height:.4f}]({match.group(2)})"
        return match.group(0)

    return re.sub(pattern, replacer, markdown)


def replace_figures_size_in_poster(
    poster: BaseModel,
    figures: list[str],
) -> BaseModel:
    for field in poster.model_fields:
        if hasattr(poster, field):
            value = getattr(poster, field)
            if isinstance(value, str):
                setattr(poster, field, replace_figures_size_in_markdown(value, figures))
    return poster


def replace_figures_in_html(html: str, figures: list[str]) -> str:
    pattern = r"src=\"(\d+)\""

    def replacer(match):
        figure_index = int(match.group(1))
        if 0 <= figure_index < len(figures):
            return f'src="data:image/png;base64,{figures[figure_index]}"'
        return match.group(0)

    return re.sub(pattern, replacer, html)


def get_sizes(type: str, html: str) -> list[list[dict]]:
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        page = browser.new_page()

        page.set_content(html)

        contents = page.query_selector_all(f".{type}-content")
        content_sizes = []

        for content in contents:
            groups = content.query_selector_all(f"> *")
            group_sizes = []

            for group in groups:
                is_group = group.evaluate(
                    f"element => element.classList.contains('{type}-group')"
                )
                if not is_group:
                    bounding_box = group.bounding_box()
                    group_sizes.append(
                        [
                            {
                                "width": bounding_box["width"],
                                "height": bounding_box["height"],
                            }
                        ]
                    )
                    continue

                group.evaluate("(element) => element.style.alignItems = 'start'")

                columns = group.query_selector_all(f".{type}-column")
                column_sizes = []

                for column in columns:
                    bounding_box = column.bounding_box()
                    column_sizes.append(
                        {
                            "width": bounding_box["width"],
                            "height": bounding_box["height"],
                        }
                    )

                group_sizes.append(column_sizes)

            content_sizes.append(group_sizes)

        browser.close()
        return content_sizes


def generate_html_v2(vendor: str, model: str, poster: BaseModel, figures: list[str]):
    if vendor == "openai":
        if "o1" in model or "o3" in model or "o4" in model:
            llm = ChatOpenAI(
                model=model,
                temperature=1,
                max_tokens=8000,
            )
        else:
            llm = BaseChatOpenAI(
                model=model,
                temperature=1,
                max_tokens=8000,
                # model_kwargs={
                #     "extra_body": {"chat_template_kwargs": {"enable_thinking": False}}
                # },
            )

    style = """<style>
      html {
        font-family: "Times New Roman", Times, serif;
        font-size: 16px;
      }

      body {
        width: 1280px;
        margin: 0;
      }

      ol,
      ul {
        margin-left: 0.5rem;
      }

      li {
        margin-bottom: 0.5rem;
      }

      img {
        width: calc(100% - 2rem);
        margin: 0.5rem 1rem;
      }

      .poster-header {
        padding: 2rem;
        text-align: center;
      }

      .poster-title {
        margin-bottom: 1rem;
        font-size: 1.875rem;
        font-weight: bold;
        color: inherit !important;
      }

      .poster-author {
        margin-bottom: 0.5rem;
        color: inherit !important;
      }

      .poster-affiliation {
        color: inherit !important;
      }

      .poster-content {
        padding: 1rem;
      }

      .section {
        margin-bottom: 1rem;
      }

      .section-title {
        padding: 0.5rem 1rem;
        font-weight: bold;
      }

      .section-content {
        margin: 0 1rem;
      }
    </style>
"""

    layout_prompt = ChatPromptTemplate.from_messages(
        [
            SystemMessage(
                content="You are a professional academic poster web page creator and your task is to generate the HTML code for a nicely laid out academic poster web page based on the object provided."
            ),
            HumanMessagePromptTemplate.from_template(
                """# Object Description
- The object contains several fields. Each field represents a section, except for the title, author and affiliation fields. The field name is the title of the section and the field value is the Markdown content of the section.
- The image in Markdown is given in the format ![alt_text, width = original_width, height = original_height, aspect ratio = aspect_ratio](image_index).

# HTML Structure
- Only generate the HTML code inside <body>, without any other things.
- Place title, author and affiliation inside <div class="poster-header">. Place title inside <div class="poster-title">, author inside <div class="poster-author"> and affiliation inside <div class="poster-affiliation">.
- Place content inside <div class="poster-content">.
- Place each section inside <div class="section">. Place section title inside <div class="section-title"> and section content inside <div class="section-content">.
- Use <p> for paragraphs.
- Use <ol> and <li> for ordered lists, and <ul> and <li> for unordered lists.
- Use <img src="image_index" alt="alt_text"> for images.
- Use <strong> for bold text and <em> for italic text.
- Do not use tags other than <div>, <p>, <ol>, <ul>, <li>, <img>, <strong>, <em>.
- Do not create any sections that are not in the object. Do not split or merge any existing sections.
- Sections and contents should be strictly equal to the object, and should be placed strictly in the order of the object.

# Color Specification
- Select at least 2 colors from the visual identity of the affiliation. If there are multiple affiliations, consider the most well-known one.
- For example, Tsinghua University uses #660874 and #d93379, Beihang University uses #005bac and #003da6, Zhejiang University uses #003f88 and #b01f24. These are just examples, you must pick colors from the actual visual identity of the affiliation.
- Add text and background color to poster header and section title using inline style. Use gradient to make the poster more beautiful.
- The text and background color of each section title should be the same.
- Do not add styles other than color, background, border, box-shadow.
- Do not add styles like width, height, padding, margin, font-size, font-weight, border-radius.

# Layout Specification
- Optionally, inside <div class="poster-content">, group sections into columns using <div class="poster-group" style="display: flex; gap: 1rem"> and <div class="poster-column" style="flex: 1">.
- You must determine the optimal number and flex grow value of columns to create a balanced poster layout. If one column becomes too tall, redistribute sections to other columns.
- There can be multiple groups with different number and flex grow of columns.
- Optionally, inside <div class="section-content">, group texts and images into columns using <div class="section-group" style="display: flex; gap: 0.5rem"> and <div class="section-column" style="flex: 1">.
- For example, if there are two images in two columns whose aspect ratios are 1.2 and 2 respectively, the flex grow of two columns should be 1.2 and 2 respectively, to make the columns have the same height.
- Calculate the size of each image based on column width and aspect ratios. Add comment <!-- width = display_width, height = display_height --> before each image.
- Rearrange the structure and order of sections, texts and images to make the height of each column in the same group approximately the same.
- For example, if there are too many images in one section that make the height of the column too large, group the images into columns.
- The display width of each image should not be too large or too small compared to its original width.
- DO NOT LEAVE MORE THAN 5% BLANK SPACE IN THE POSTER.
- Use a 3-column or 4-column layout with a landscape (horizontal) orientation for optimal visual presentation.

# Output Requirement
- Please output the result in the following format:
  <think>
    Think step by step, considering all structures and specifications listed above one by one.
    Calculate the width and height of each column, text and image in detail, based on given style.
  </think>
  ```html
    HTML code inside <body>.
  ```
- Please make the content in <think> as detailed and comprehensive as possible.

# Existing Style
{style}

# Object
{poster}
"""
            ),
        ]
    )
    layout_chain = layout_prompt | llm
    # output = layout_chain.invoke({"style": style, "poster": poster}).content
    layout_prompt.append(
        MessagesPlaceholder(variable_name="react"),
    )

    HTML_TEMPLATE = """<!DOCTYPE html>
<html>
  <head>
    <title>Poster</title>
    {style}
    <script>
      MathJax = {{ tex: {{ inlineMath: [["$", "$"]] }} }};
    </script>
    <script
      id="MathJax-script"
      async
      src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
    ></script>
  </head>
  <body>
    {body}
  </body>
</html>
"""

    def get_content_sizes(sizes: list[list[dict]]) -> float:
        """Calculate the total content size from the sizes data structure"""
        return sum(
            column["width"] * column["height"]
            for content in sizes
            for group in content
            for column in group
        )

    def get_total_size(sizes: list[list[dict]]) -> float:
        """Calculate the total size including spacing from the sizes data structure"""
        return sum(
            (
                sum(column["width"] for column in group)
                * max((column["height"] for column in group), default=0)
            )
            for content in sizes
            for group in content
        )

    def calculate_blank_proportion(poster_sizes, section_sizes) -> float:
        """Calculate the proportion of blank space in the poster"""
        poster_content_sizes = get_content_sizes(poster_sizes)
        section_content_sizes = get_content_sizes(section_sizes)
        poster_total_size = get_total_size(poster_sizes)
        section_total_size = get_total_size(section_sizes)

        if poster_total_size == 0:
            return 1.0

        return (
            1.0
            - (poster_content_sizes - (section_total_size - section_content_sizes))
            / poster_total_size
        )

    max_attempts = 5
    attempt = 0

    min_proportion = float('inf')
    min_html = None
    min_html_with_figures = None
    min_body = None
    min_poster_sizes = None
    min_section_sizes = None

    def generate_single_html(prompt_input):
        """单个HTML生成函数,用于多线程执行"""
        result_output = layout_chain.invoke(prompt_input).content
        print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Generated HTML")
        body = re.search(r"```html\n(.*?)\n```", result_output, re.DOTALL).group(1)
        
        html = HTML_TEMPLATE.format(style=style, body=body)
        html_with_figures = replace_figures_in_html(html, figures)
        
        poster_sizes = get_sizes("poster", html_with_figures)
        section_sizes = get_sizes("section", html_with_figures)
        
        proportion = calculate_blank_proportion(poster_sizes, section_sizes)
        
        return {
            "body": body,
            "html": html,
            "html_with_figures": html_with_figures,
            "poster_sizes": poster_sizes,
            "section_sizes": section_sizes,
            "proportion": proportion
        }

    # 初始生成两个HTML布局
    prompt_inputs = [
        {"style": style, "poster": poster, "react": []},
        {"style": style, "poster": poster, "react": []}
    ]
    
    with ThreadPoolExecutor(max_workers=2) as executor:
        initial_results = list(executor.map(generate_single_html, prompt_inputs))
    
    # 检查初始生成的两个结果
    for result in initial_results:
        proportion = result["proportion"]
        print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 初始生成比例: {proportion:.0%}")
        
        # 更新最佳结果
        if proportion < min_proportion:
            min_proportion = proportion
            min_html = result["html"]
            min_html_with_figures = result["html_with_figures"]
            min_body = result["body"]
            min_poster_sizes = result["poster_sizes"]
            min_section_sizes = result["section_sizes"]
        
    # 如果找到满足条件的结果,直接返回
    if min_proportion <= 0.1:
        print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Initial generation successful, remaining {min_proportion:.0%} blank spaces.")
        return {"html": min_html, "html_with_figures": min_html_with_figures}

    while True:
        attempt += 1
        if attempt > max_attempts:
            if min_proportion <= 0.2:
                print(
                    f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Reached max attempts ({max_attempts}), returning best result with {min_proportion:.0%} blank spaces."
                )
                return {"html": min_html, "html_with_figures": min_html_with_figures}
            else:
                raise ValueError(f"Invalid blank spaces: {min_proportion:.0%}")

        # 基于最好的结果生成两个新的
        react = [
            HumanMessage(
                content=f"""# Previous Body
{min_body}

# Previous Size of Columns in Poster
{min_poster_sizes}

# Previous Size of Columns in Section
{min_section_sizes}

Now there are {min_proportion:.0%} blank spaces. Please regenerate the content to create a more balanced poster layout.
"""
            ),
        ]
        prompt_inputs = [
            {"style": style, "poster": poster, "react": react},
            {"style": style, "poster": poster, "react": react}
        ]

        # 使用多线程同时生成两个HTML
        with ThreadPoolExecutor(max_workers=2) as executor:
            results = list(executor.map(generate_single_html, prompt_inputs))

        # 检查两个结果
        for result in results:
            proportion = result["proportion"]
            print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 当前比例: {proportion:.0%}")
            
            # 更新最佳结果
            if proportion < min_proportion:
                min_proportion = proportion
                min_html = result["html"]
                min_html_with_figures = result["html_with_figures"]
                min_body = result["body"]
                min_poster_sizes = result["poster_sizes"]
                min_section_sizes = result["section_sizes"]
            
            # 如果找到满足条件的结果,直接返回
            if proportion <= 0.1:
                print(
                    f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Attempted {attempt} times, remaining {proportion:.0%} blank spaces."
                )
                return {"html": result["html"], "html_with_figures": result["html_with_figures"]}


# def take_screenshot(output: str, html: str):
#     with sync_playwright() as p:
#         browser = p.chromium.launch(headless=True)
#         page = browser.new_page(viewport={"width": 1280, "height": 100})
#         page.set_content(html)
#         page.screenshot(
#             type="png", path=output.replace(".json", ".png"), full_page=True
#         )
#         browser.close()


def replace_figures_in_svg(svg: str, figures: list[str]) -> str:
    pattern = r"href=\"(\d+)\""

    def replacer(match):
        figure_index = int(match.group(1))
        if 0 <= figure_index < len(figures):
            return f'href="data:image/png;base64,{figures[figure_index]}"'
        return match.group(0)

    return re.sub(pattern, replacer, svg)


def svg_to_png(output: str, svg: str):
    cairosvg.svg2png(
        bytestring=svg.encode("utf-8"),
        write_to=output.replace(".json", ".png"),
        output_width=7000,
    )


def replace_figures_in_latex(latex: str, figures: list[str]) -> str:
    pattern = r"\\includegraphics(\[.*?\])?\{(\d+)\}"

    def replacer(match):
        figure_index = int(match.group(2))
        options = match.group(1) or ""
        if 0 <= figure_index < len(figures):
            return f"\\includegraphics{options}{{figure_{figure_index}.png}}"
        return match.group(0)

    return re.sub(pattern, replacer, latex)


def latex_to_png(output: str, latex: str):
    subprocess.run(
        [
            "pdflatex",
            "-interaction=nonstopmode",
            f"-output-directory={os.path.dirname(output)}",
            output.replace(".json", ".tex"),
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    images = convert_from_path(output.replace(".json", ".pdf"), dpi=300)
    images[0].save(output.replace(".json", ".png"))


def generate_poster_v3(
    vendor: str,
    model: str,
    text_prompt: str,
    figures_prompt: str,
    pdf: str,
    figures: list[str],
    figures_index: list[str],
) -> dict:
    # Setup LLM
    if vendor == "openai":
        if "o1" in model or "o3" in model or "o4" in model:
            llm = ChatOpenAI(
                model=model,
                temperature=1,
                max_tokens=8000,
            )
        else:
            llm = BaseChatOpenAI(
                model=model,
                temperature=1,
                max_tokens=8000,
            )
    elif vendor == "azure":
        llm = AzureChatOpenAI(
            azure_deployment=model,
            temperature=1,
            max_tokens=8000,
        )
    else:
        raise ValueError(f"Unsupported vendor: {vendor}")

    loader = PyMuPDFLoader(pdf)
    pages = loader.load()
    paper_content = "\n".join([page.page_content for page in pages])

    from .compress import compress_image

    figure_messages = [
        HumanMessagePromptTemplate(
            prompt=[
                ImagePromptTemplate(
                    input_variables=["figure"],
                    template={"url": "data:image/png;base64,{figure}"},
                ),
            ],
        ).format(figure=compress_image(figure, quality=85, max_size=(64, 64)))
        for figure in figures
    ]

    json_format_example = """
```json
{{
    "Introduction": "Brief overview of the paper's main topic and objectives.",
    "Methodology": "Description of the methods used in the research.",
    "Results": "Summary of the key findings and results."
}}
```
"""
    sections = None
    for _ in range(5):
        section_prompt = ChatPromptTemplate.from_messages(
            [
                SystemMessage(content="You are an expert in academic paper analysis."),
                HumanMessagePromptTemplate.from_template(
                    """Please analyze the paper content and identify the key sections that should be included in the poster. 
For each section, provide a concise description of what should be included. First, determine the paper type:
- For methodology research papers: Focus on method description, experimental results, and research methodology.
- For benchmark papers: Highlight task definitions, dataset construction, and evaluation outcomes.
- For survey/review papers: Emphasize field significance, key developmental milestones, critical theories/techniques, current challenges, and emerging trends.

Note that the specific section names should be derived from the paper's content. Related sections can be combined to avoid fragmentation. Limit the total number of sections to maintain clarity. Do not include acknowledgements or references sections.

Return the result as a flat JSON object with section names as keys and descriptions as values, without nested structures. You MUST use Markdown code block syntax with the json language specifier.

Example format:
{json_format_example}

Paper content:
{paper_content}
"""
                ),
            ]
        )
        sections_response = llm.invoke(
            section_prompt.format(
                json_format_example=json_format_example, paper_content=paper_content
            )
        )

        json_pattern = r"```json(.*?)```"
        match = re.search(json_pattern, sections_response.content, re.DOTALL)
        if match:
            json_content = match.group(1)
        else:
            continue

        try:
            sections = eval(json_content.strip())
            if all(
                isinstance(k, str) and isinstance(v, str) for k, v in sections.items()
            ):
                break
        except Exception:
            continue

    if sections is None:
        raise ValueError("Failed to retrieve valid sections from LLM response.")

    DynamicPoster = create_dynamic_poster_model(sections)

    figures_description_prompt = ChatPromptTemplate.from_messages(
        [
            SystemMessage(
                content="You are an academic image analysis expert. Provide concise descriptions (under 100 words) of academic figures, diagrams, charts, or images. Identify what the figure displays, its likely purpose in academic literature, and highlight key data points or trends. Focus on clarity and academic relevance while maintaining precision in your analysis."
            ),
            HumanMessagePromptTemplate(
                prompt=[
                    # PromptTemplate(template="Describe this image:"),
                    ImagePromptTemplate(
                        input_variables=["image_data"],
                        template={"url": "data:image/png;base64,{image_data}"},
                    ),
                ],
            ),
        ]
    )

    use_claude = False
    mllm = BaseChatOpenAI(
        temperature=1,
        max_tokens=8000,
    )

    figures_with_descriptions = ""
    figure_list = []

    figures_description_cache = pdf.replace(".pdf", "_figures_description.json")
    if use_claude and os.path.exists(figures_description_cache):
        with open(figures_description_cache, "r") as f:
            figures_with_descriptions = f.read()
    else:
        figure_chain = figures_description_prompt | (mllm if use_claude else llm)
        
        def process_single_figure(figure_data):
            figure, index = figure_data
            figure_description_response = figure_chain.invoke({"image_data": figure})
            return {
                "index": index,
                "figure": figure,
                "description": figure_description_response.content
            }
        
        figure_data_list = [(figure, i) for i, figure in enumerate(figures)]
        
        with ThreadPoolExecutor(max_workers=4) as executor:
            results = list(tqdm(
                executor.map(process_single_figure, figure_data_list),
                total=len(figure_data_list),
                desc=f"处理图片 {pdf}"
            ))
        
        for result in results:
            i = result["index"]
            print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 处理图片 {i} 完成")
            figures_with_descriptions += f"""
<figure_{i}>
{result["description"]}
</figure_{i}>
"""
            figure_list.append({
                "figure": result["figure"], 
                "description": result["description"]
            })
            
        if use_claude:
            with open(figures_description_cache, "w") as f:
                f.write(figures_with_descriptions)

    text_prompt = ChatPromptTemplate.from_messages(
        [
            SystemMessage(
                content="You are a helpful academic expert, who is specialized in generating a text-based paper poster, from given contents."
            ),
            HumanMessagePromptTemplate.from_template(
                """Below is the figures with descriptions in the paper:
<figures>
{figures}
</figures>

Below is the content of the paper:
<paper_content>
{paper_content}
</paper_content>

If figures can effectively convey the poster content, simplify the related text to avoid redundancy. Include essential mathematical formulas where they enhance understanding.

{format_instructions}

Ensure all sections are precise, concise, and presented in markdown format without headings."""
            ),
        ]
    )
    parser = PydanticOutputParser(pydantic_object=DynamicPoster)
    fixing_parser = OutputFixingParser.from_llm(parser=parser, llm=llm)
    text_prompt = text_prompt.partial(
        format_instructions=parser.get_format_instructions()
    )
    text_chain = text_prompt | llm | remove_think_tags | parser
    try:
        text_poster = text_chain.invoke(
            {"paper_content": paper_content, "figures": figures_with_descriptions}
        )
    except OutputParserException as e:
        text_poster = fixing_parser.parse(e.llm_output)

    figures_prompt = ChatPromptTemplate.from_messages(
        [
            SystemMessagePromptTemplate.from_template(
                "You are a helpful academic expert, who is specialized in generating a paper poster, from given contents and figures. "
            ),
            HumanMessagePromptTemplate.from_template(
                """Below is the figures with descriptions in the paper:
<figures>
{figures}
</figures>

I have already generated a text-based poster as follows:
<poster_content>
{poster_content}
</poster_content>

The paper content is as follows:
<paper_content>
{paper_content}
</paper_content>

Insert figures into the poster content using figure index notation as `![figure_description](figure_index)`. For example, `![Overview](0)`.
The figure_index MUST be an integer starting from 0, and no other text should be used in the figure_index position.
Each figure should be used at most once, with precise and accurate placement.
Prioritize pictures and tables based on their relevance and importance to the content.

{format_instructions}"""
            ),
        ]
    )
    figures_prompt = figures_prompt.partial(
        figures=figures_with_descriptions,
        format_instructions=parser.get_format_instructions(),
    )
    figures_chain = figures_prompt | llm | remove_think_tags | parser
    try:
        figures_poster = figures_chain.invoke(
            {"poster_content": text_poster, "paper_content": paper_content}
        )
    except OutputParserException as e:
        figures_poster = fixing_parser.parse(e.llm_output)

    return {
        "sections": sections,
        "figures": figure_list,
        "text_based_poster": text_poster,
        "image_based_poster": figures_poster,
    }