|
import os |
|
import cv2 |
|
import pytesseract |
|
from pdf2image import convert_from_path |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
|
|
|
|
|
|
pdf_path = "kessan.pdf" |
|
|
|
|
|
images = convert_from_path(pdf_path) |
|
|
|
|
|
|
|
|
|
|
|
extracted_text = "" |
|
|
|
for i, image in enumerate(images): |
|
|
|
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) |
|
|
|
|
|
gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY) |
|
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] |
|
|
|
text = pytesseract.image_to_string(thresh, lang="jpn") |
|
extracted_text += text + "\n" |
|
|
|
print("OCR抽出完了。") |
|
|
|
|
|
|
|
model_name = "deepseek-ai/deepseek-coder-1.3b" |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
|
|
prompt = ( |
|
"以下の決算短信の内容を要約し、投資家向けに分かりやすく説明してください:\n\n" + |
|
extracted_text |
|
) |
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=4096) |
|
|
|
|
|
output_ids = model.generate(inputs.input_ids, max_length=512, num_beams=5, early_stopping=True) |
|
summary = tokenizer.decode(output_ids[0], skip_special_tokens=True) |
|
|
|
print("\n=== 要約結果 ===") |
|
print(summary) |
|
|
|
|