speed up
Browse files- main.py +1 -1
- poster/poster.py +77 -21
main.py
CHANGED
@@ -99,7 +99,7 @@ def generate_paper_poster(
|
|
99 |
# with open(output.replace(".json", ".html"), "w") as f:
|
100 |
# f.write(html)
|
101 |
# take_screenshot(output, html)
|
102 |
-
|
103 |
return poster, html
|
104 |
|
105 |
except Exception as e:
|
|
|
99 |
# with open(output.replace(".json", ".html"), "w") as f:
|
100 |
# f.write(html)
|
101 |
# take_screenshot(output, html)
|
102 |
+
print("海报生成成功!")
|
103 |
return poster, html
|
104 |
|
105 |
except Exception as e:
|
poster/poster.py
CHANGED
@@ -395,30 +395,61 @@ def generate_html_v2(vendor: str, model: str, poster: BaseModel, figures: list[s
|
|
395 |
min_proportion = float('inf')
|
396 |
min_html = None
|
397 |
min_html_with_figures = None
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
|
|
|
|
|
|
|
|
|
|
402 |
html = HTML_TEMPLATE.format(style=style, body=body)
|
403 |
html_with_figures = replace_figures_in_html(html, figures)
|
404 |
-
|
405 |
poster_sizes = get_sizes("poster", html_with_figures)
|
406 |
section_sizes = get_sizes("section", html_with_figures)
|
407 |
-
|
408 |
proportion = calculate_blank_proportion(poster_sizes, section_sizes)
|
409 |
|
410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
411 |
if proportion < min_proportion:
|
412 |
min_proportion = proportion
|
413 |
-
min_html = html
|
414 |
-
min_html_with_figures = html_with_figures
|
|
|
|
|
|
|
415 |
|
|
|
416 |
if proportion <= 0.1:
|
417 |
-
print(
|
418 |
-
|
419 |
-
)
|
420 |
-
return {"html": html, "html_with_figures": html_with_figures}
|
421 |
|
|
|
422 |
attempt += 1
|
423 |
if attempt > max_attempts:
|
424 |
if min_proportion <= 0.2:
|
@@ -429,26 +460,51 @@ def generate_html_v2(vendor: str, model: str, poster: BaseModel, figures: list[s
|
|
429 |
else:
|
430 |
raise ValueError(f"Invalid blank spaces: {min_proportion:.0%}")
|
431 |
|
432 |
-
|
433 |
react = [
|
434 |
HumanMessage(
|
435 |
content=f"""# Previous Body
|
436 |
-
{
|
437 |
|
438 |
# Previous Size of Columns in Poster
|
439 |
-
{
|
440 |
|
441 |
# Previous Size of Columns in Section
|
442 |
-
{
|
443 |
|
444 |
-
Now there are {
|
445 |
"""
|
446 |
),
|
447 |
]
|
448 |
-
|
449 |
-
|
450 |
{"style": style, "poster": poster, "react": react}
|
451 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
452 |
|
453 |
|
454 |
# def take_screenshot(output: str, html: str):
|
|
|
395 |
min_proportion = float('inf')
|
396 |
min_html = None
|
397 |
min_html_with_figures = None
|
398 |
+
min_body = None
|
399 |
+
min_poster_sizes = None
|
400 |
+
min_section_sizes = None
|
401 |
+
|
402 |
+
def generate_single_html(prompt_input):
|
403 |
+
"""单个HTML生成函数,用于多线程执行"""
|
404 |
+
result_output = layout_chain.invoke(prompt_input).content
|
405 |
+
body = re.search(r"```html\n(.*?)\n```", result_output, re.DOTALL).group(1)
|
406 |
+
|
407 |
html = HTML_TEMPLATE.format(style=style, body=body)
|
408 |
html_with_figures = replace_figures_in_html(html, figures)
|
409 |
+
|
410 |
poster_sizes = get_sizes("poster", html_with_figures)
|
411 |
section_sizes = get_sizes("section", html_with_figures)
|
412 |
+
|
413 |
proportion = calculate_blank_proportion(poster_sizes, section_sizes)
|
414 |
|
415 |
+
return {
|
416 |
+
"body": body,
|
417 |
+
"html": html,
|
418 |
+
"html_with_figures": html_with_figures,
|
419 |
+
"poster_sizes": poster_sizes,
|
420 |
+
"section_sizes": section_sizes,
|
421 |
+
"proportion": proportion
|
422 |
+
}
|
423 |
+
|
424 |
+
# 初始生成两个HTML布局
|
425 |
+
prompt_inputs = [
|
426 |
+
{"style": style, "poster": poster},
|
427 |
+
{"style": style, "poster": poster}
|
428 |
+
]
|
429 |
+
|
430 |
+
with ThreadPoolExecutor(max_workers=2) as executor:
|
431 |
+
initial_results = list(executor.map(generate_single_html, prompt_inputs))
|
432 |
+
|
433 |
+
# 检查初始生成的两个结果
|
434 |
+
for result in initial_results:
|
435 |
+
proportion = result["proportion"]
|
436 |
+
print(f"初始生成比例: {proportion:.0%}")
|
437 |
+
|
438 |
+
# 更新最佳结果
|
439 |
if proportion < min_proportion:
|
440 |
min_proportion = proportion
|
441 |
+
min_html = result["html"]
|
442 |
+
min_html_with_figures = result["html_with_figures"]
|
443 |
+
min_body = result["body"]
|
444 |
+
min_poster_sizes = result["poster_sizes"]
|
445 |
+
min_section_sizes = result["section_sizes"]
|
446 |
|
447 |
+
# 如果找到满足条件的结果,直接返回
|
448 |
if proportion <= 0.1:
|
449 |
+
print(f"Initial generation successful, remaining {proportion:.0%} blank spaces.")
|
450 |
+
return {"html": result["html"], "html_with_figures": result["html_with_figures"]}
|
|
|
|
|
451 |
|
452 |
+
while True:
|
453 |
attempt += 1
|
454 |
if attempt > max_attempts:
|
455 |
if min_proportion <= 0.2:
|
|
|
460 |
else:
|
461 |
raise ValueError(f"Invalid blank spaces: {min_proportion:.0%}")
|
462 |
|
463 |
+
# 基于最好的结果生成两个新的
|
464 |
react = [
|
465 |
HumanMessage(
|
466 |
content=f"""# Previous Body
|
467 |
+
{min_body}
|
468 |
|
469 |
# Previous Size of Columns in Poster
|
470 |
+
{min_poster_sizes}
|
471 |
|
472 |
# Previous Size of Columns in Section
|
473 |
+
{min_section_sizes}
|
474 |
|
475 |
+
Now there are {min_proportion:.0%} blank spaces. Please regenerate the content to create a more balanced poster layout.
|
476 |
"""
|
477 |
),
|
478 |
]
|
479 |
+
prompt_inputs = [
|
480 |
+
{"style": style, "poster": poster, "react": react},
|
481 |
{"style": style, "poster": poster, "react": react}
|
482 |
+
]
|
483 |
+
|
484 |
+
# 使用多线程同时生成两个HTML
|
485 |
+
with ThreadPoolExecutor(max_workers=2) as executor:
|
486 |
+
results = list(executor.map(generate_single_html, prompt_inputs))
|
487 |
+
|
488 |
+
# 检查两个结果
|
489 |
+
for result in results:
|
490 |
+
proportion = result["proportion"]
|
491 |
+
print(f"当前比例: {proportion:.0%}")
|
492 |
+
|
493 |
+
# 更新最佳结果
|
494 |
+
if proportion < min_proportion:
|
495 |
+
min_proportion = proportion
|
496 |
+
min_html = result["html"]
|
497 |
+
min_html_with_figures = result["html_with_figures"]
|
498 |
+
min_body = result["body"]
|
499 |
+
min_poster_sizes = result["poster_sizes"]
|
500 |
+
min_section_sizes = result["section_sizes"]
|
501 |
+
|
502 |
+
# 如果找到满足条件的结果,直接返回
|
503 |
+
if proportion <= 0.1:
|
504 |
+
print(
|
505 |
+
f"Attempted {attempt} times, remaining {proportion:.0%} blank spaces."
|
506 |
+
)
|
507 |
+
return {"html": result["html"], "html_with_figures": result["html_with_figures"]}
|
508 |
|
509 |
|
510 |
# def take_screenshot(output: str, html: str):
|