Spaces:
Build error
Build error
File size: 3,120 Bytes
b0d689c | 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 | import fitz
import google.generativeai as genai
import numpy as np
# 內容萃取+標題判斷
# https://blog.csdn.net/star1210644725/article/details/136318768
def extractContents(filepath):
doc = fitz.open(filepath)
topics = []
re_contents = []
for page_num in range(len(doc)):
page = doc.load_page(page_num)
blocks = page.get_text("dict")["blocks"]
page_contents = []
# extract font sizes
page_number = page_num + 1
sizes = []
for block in blocks:
text_parts = []
size = 0
try:
for line in block["lines"]:
for span in line["spans"]:
sizes.append(span["size"])
text_parts.append(span["text"])
size = span["size"]
except KeyError:
continue
text = "".join(text_parts)
page_contents.append({
"text": text,
"size": size,
"pnum": page_number
})
# find median
sizes_arr = np.array(sizes)
med = np.median(sizes_arr)
# topic filter
for content in page_contents:
text = content["text"]
# 找出大小大於中位數的字體、過濾掉可能是文字塊、圖片、公式的語料
if content["size"] > med and not is_body_text(text) and not is_image(text) and not is_formula(text):
topics.append(content)
re_contents.append(content)
# 找出大小大於等於於中位數的字體、過濾掉可能是圖片的語料
elif content["size"] == med and not is_image(text):
re_contents.append(content)
return re_contents, topics
def is_body_text(text):
return len(text) > 100 or text.endswith(".") or text.endswith("?") or text.endswith("!")
def is_image(text):
return text.startswith("Image:") or text.startswith("Figure:")
def is_formula(text):
return text.startswith("Formula:") or text.startswith("Equation:")
# gemini再次確認是否為標題
def reCheck(text, model):
input = f"""please check whether "{text}" is a topic, and answer "yes" or "no" only
"""
reply = model.generate_content(
input,
generation_config=genai.types.GenerationConfig(temperature=0),
safety_settings=[
{"category": "HARM_CATEGORY_HARASSMENT","threshold": "BLOCK_NONE",},
{"category": "HARM_CATEGORY_HATE_SPEECH","threshold": "BLOCK_NONE",},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold": "BLOCK_NONE",},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE",},
]
)
if "yes" in reply.text:
return True
else:
return False
# === Topic Main === #
def Topics(filepath, model):
re_topics = []
contents, topics = extractContents(filepath)
for element in topics:
if reCheck(element['text'], model):
re_topics.append(element)
else:
continue
return contents, re_topics |