2023-02-03 15:39:24 -08:00
|
|
|
"""Read Microsoft PowerPoint files."""
|
2023-02-01 17:35:33 -08:00
|
|
|
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2023-02-03 23:38:12 -08:00
|
|
|
from typing import Dict, List, Optional
|
2023-02-01 17:35:33 -08:00
|
|
|
|
2023-02-03 15:39:24 -08:00
|
|
|
from gpt_index.readers.base import BaseReader
|
|
|
|
from gpt_index.readers.schema.base import Document
|
2023-02-01 17:35:33 -08:00
|
|
|
|
|
|
|
|
2023-02-03 15:39:24 -08:00
|
|
|
class PptxReader(BaseReader):
|
|
|
|
"""Powerpoint reader.
|
2023-02-01 17:35:33 -08:00
|
|
|
|
|
|
|
Extract text, caption images, and specify slides.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2023-02-03 15:39:24 -08:00
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Init reader."""
|
2023-02-03 20:41:20 -08:00
|
|
|
from transformers import (
|
|
|
|
AutoTokenizer,
|
|
|
|
VisionEncoderDecoderModel,
|
|
|
|
ViTFeatureExtractor,
|
|
|
|
)
|
2023-02-01 17:35:33 -08:00
|
|
|
|
|
|
|
model = VisionEncoderDecoderModel.from_pretrained(
|
|
|
|
"nlpconnect/vit-gpt2-image-captioning"
|
|
|
|
)
|
|
|
|
feature_extractor = ViTFeatureExtractor.from_pretrained(
|
|
|
|
"nlpconnect/vit-gpt2-image-captioning"
|
|
|
|
)
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
"nlpconnect/vit-gpt2-image-captioning"
|
|
|
|
)
|
|
|
|
|
2023-02-03 15:39:24 -08:00
|
|
|
self.parser_config = {
|
2023-02-01 17:35:33 -08:00
|
|
|
"feature_extractor": feature_extractor,
|
|
|
|
"model": model,
|
|
|
|
"tokenizer": tokenizer,
|
|
|
|
}
|
|
|
|
|
|
|
|
def caption_image(self, tmp_image_file: str) -> str:
|
|
|
|
"""Generate text caption of image."""
|
|
|
|
import torch
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
model = self.parser_config["model"]
|
|
|
|
feature_extractor = self.parser_config["feature_extractor"]
|
|
|
|
tokenizer = self.parser_config["tokenizer"]
|
|
|
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
model.to(device)
|
|
|
|
|
|
|
|
max_length = 16
|
|
|
|
num_beams = 4
|
|
|
|
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
|
|
|
|
|
|
|
i_image = Image.open(tmp_image_file)
|
|
|
|
if i_image.mode != "RGB":
|
|
|
|
i_image = i_image.convert(mode="RGB")
|
|
|
|
|
|
|
|
pixel_values = feature_extractor(
|
|
|
|
images=[i_image], return_tensors="pt"
|
|
|
|
).pixel_values
|
|
|
|
pixel_values = pixel_values.to(device)
|
|
|
|
|
|
|
|
output_ids = model.generate(pixel_values, **gen_kwargs)
|
|
|
|
|
|
|
|
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
|
|
|
return preds[0].strip()
|
|
|
|
|
2023-02-03 15:39:24 -08:00
|
|
|
def load_data(
|
|
|
|
self, file: Path, extra_info: Optional[Dict] = None
|
|
|
|
) -> List[Document]:
|
2023-02-01 17:35:33 -08:00
|
|
|
"""Parse file."""
|
|
|
|
from pptx import Presentation
|
|
|
|
|
|
|
|
presentation = Presentation(file)
|
|
|
|
result = ""
|
|
|
|
for i, slide in enumerate(presentation.slides):
|
|
|
|
result += f"\n\nSlide #{i}: \n"
|
|
|
|
for shape in slide.shapes:
|
|
|
|
if hasattr(shape, "image"):
|
|
|
|
image = shape.image
|
|
|
|
# get image "file" contents
|
|
|
|
image_bytes = image.blob
|
|
|
|
# temporarily save the image to feed into model
|
|
|
|
image_filename = f"tmp_image.{image.ext}"
|
|
|
|
with open(image_filename, "wb") as f:
|
|
|
|
f.write(image_bytes)
|
|
|
|
result += f"\n Image: {self.caption_image(image_filename)}\n\n"
|
|
|
|
|
|
|
|
os.remove(image_filename)
|
|
|
|
if hasattr(shape, "text"):
|
|
|
|
result += f"{shape.text}\n"
|
|
|
|
|
2023-02-03 15:39:24 -08:00
|
|
|
return [Document(result, extra_info=extra_info)]
|