MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / generate_slides

Function generate_slides

utils/src/experiment/baseline_docpres.py:111–161  ·  view source on GitHub ↗
(
    output_dir: str,
    source_text: str,
    bird_eye: dict,
    images: list[str],
    model: CLIPModel,
    processor: CLIPProcessor,
)

Source from the content-addressed store, hash-verified

109
110
111def generate_slides(
112 output_dir: str,
113 source_text: str,
114 bird_eye: dict,
115 images: list[str],
116 model: CLIPModel,
117 processor: CLIPProcessor,
118):
119 os.makedirs(output_dir, exist_ok=True)
120 images = filter_aspect_ratio(images)
121 slides = generate_content(source_text, bird_eye, 7)
122 image_embeddings = model.get_image_features(
123 **processor(images=[Image.open(i) for i in images], return_tensors="pt").to(
124 "cuda"
125 )
126 ).unsqueeze(0)
127 text_embeddings = model.get_text_features(
128 **processor(
129 text=["\n".join(slide["bullets"]) for slide in slides],
130 return_tensors="pt",
131 padding=True,
132 max_length=77,
133 truncation=True,
134 ).to("cuda")
135 ).unsqueeze(1)
136 similarity = cosine_similarity(image_embeddings, text_embeddings, dim=-1)
137 pptx = Presentation()
138 for slide_idx, slide in enumerate(slides): # match image here
139 title = slide["title"]
140 bullets = slide["bullets"]
141
142 subsimilarity = similarity[slide_idx]
143 if subsimilarity.max() > 0.8:
144 slide = pptx.slides.add_slide(pptx.slide_layouts[6])
145 bullets_placeholder = slide.shapes.placeholders[2]
146 image = images[subsimilarity.argmax()]
147 slides[slide_idx]["image"] = image
148 slide.shapes.placeholders[1].insert_picture(image)
149 else:
150 slide = pptx.slides.add_slide(pptx.slide_layouts[1])
151 bullets_placeholder = slide.shapes.placeholders[1]
152 slide.shapes.title.text = title
153 text_frame = bullets_placeholder.text_frame
154 for bullet in bullets:
155 para = text_frame.add_paragraph()
156 para.text = bullet
157 para.level = 1
158 with jsonlines.open(output_dir + "/final.jsonl", "w") as writer:
159 writer.write_all(slides)
160 pptx.save(output_dir + "/final.pptx")
161 ppt_to_images(output_dir + "/final.pptx", output_dir + "/slide_images")
162
163
164def generate(model: Literal["Qwen2.5", "gpt"]):

Callers 1

process_folderFunction · 0.70

Calls 5

saveMethod · 0.95
PresentationClass · 0.90
ppt_to_imagesFunction · 0.90
filter_aspect_ratioFunction · 0.70
generate_contentFunction · 0.70

Tested by

no test coverage detected