Spaces:
Runtime error
Runtime error
Update pipelines/openai_ingest.py
Browse files- pipelines/openai_ingest.py +4 -16
pipelines/openai_ingest.py
CHANGED
|
@@ -14,7 +14,7 @@ _client = None
|
|
| 14 |
def _client_lazy():
|
| 15 |
global _client
|
| 16 |
if _client is None:
|
| 17 |
-
key = os.environ.get("OPENAI_API_KEY") or os.environ.get("OPENAI_APIKEY")
|
| 18 |
if not key:
|
| 19 |
raise RuntimeError(
|
| 20 |
"OPENAI_API_KEY (または OPENAI_APIKEY) が未設定です。Spaces > Settings > Variables and secrets で追加してください。"
|
|
@@ -38,7 +38,6 @@ def extract_text_with_openai(payload: bytes, filename: str, filetype: str) -> st
|
|
| 38 |
"""画像/PDFは画像化してVisionに渡す。テキストは整形依頼してきれいな本文を返す。"""
|
| 39 |
client = _client_lazy()
|
| 40 |
|
| 41 |
-
# 画像群を構築
|
| 42 |
images: List[Image.Image] = []
|
| 43 |
if filetype == "pdf":
|
| 44 |
images = _pdf_to_images(payload)
|
|
@@ -81,9 +80,7 @@ def structure_with_openai(text: str) -> dict:
|
|
| 81 |
" JSONキー: work_experience_raw, education_raw, certifications_raw, skills_list。"
|
| 82 |
" skills_list は重複除去済み配列。work_experience_raw等は原文抜粋で良い。"
|
| 83 |
)
|
| 84 |
-
user =
|
| 85 |
-
"以下のテキストを解析し、指定のJSONキーで返してください。\n\n" + text
|
| 86 |
-
)
|
| 87 |
resp = client.responses.create(
|
| 88 |
model=MODEL_TEXT,
|
| 89 |
input=[
|
|
@@ -105,9 +102,7 @@ def structure_with_openai(text: str) -> dict:
|
|
| 105 |
|
| 106 |
def summarize_with_openai(text: str) -> dict:
|
| 107 |
client = _client_lazy()
|
| 108 |
-
prompt = (
|
| 109 |
-
"以下の候補者レジュメ本文を、(1)300字、(2)100字、(3)1文 の3粒度で日本語要約してください。余計な記号は避け、事実を簡潔に。"
|
| 110 |
-
)
|
| 111 |
resp = client.responses.create(
|
| 112 |
model=MODEL_TEXT,
|
| 113 |
input=[
|
|
@@ -116,15 +111,8 @@ def summarize_with_openai(text: str) -> dict:
|
|
| 116 |
],
|
| 117 |
)
|
| 118 |
full = resp.output_text
|
| 119 |
-
|
| 120 |
-
# 簡易パース(区切り語で抽出)。失敗時は同文を複写
|
| 121 |
-
def _slice(full_txt, marker, fallback):
|
| 122 |
-
import re
|
| 123 |
-
m = re.search(marker + r"[\s\S]*?\n", full_txt)
|
| 124 |
-
return (m.group(0).split("\n")[0] if m else fallback).strip()
|
| 125 |
-
|
| 126 |
return {
|
| 127 |
-
"300chars": full[:
|
| 128 |
"100chars": full[:120] if len(full) > 0 else "",
|
| 129 |
"onesent": full.split("。")[0] + "。" if "。" in full else full,
|
| 130 |
}
|
|
|
|
| 14 |
def _client_lazy():
|
| 15 |
global _client
|
| 16 |
if _client is None:
|
| 17 |
+
key = os.environ.get("OPENAI_API_KEY") or os.environ.get("OPENAI_APIKEY") # ← フォールバック追加
|
| 18 |
if not key:
|
| 19 |
raise RuntimeError(
|
| 20 |
"OPENAI_API_KEY (または OPENAI_APIKEY) が未設定です。Spaces > Settings > Variables and secrets で追加してください。"
|
|
|
|
| 38 |
"""画像/PDFは画像化してVisionに渡す。テキストは整形依頼してきれいな本文を返す。"""
|
| 39 |
client = _client_lazy()
|
| 40 |
|
|
|
|
| 41 |
images: List[Image.Image] = []
|
| 42 |
if filetype == "pdf":
|
| 43 |
images = _pdf_to_images(payload)
|
|
|
|
| 80 |
" JSONキー: work_experience_raw, education_raw, certifications_raw, skills_list。"
|
| 81 |
" skills_list は重複除去済み配列。work_experience_raw等は原文抜粋で良い。"
|
| 82 |
)
|
| 83 |
+
user = "以下のテキストを解析し、指定のJSONキーで返してください。\n\n" + text
|
|
|
|
|
|
|
| 84 |
resp = client.responses.create(
|
| 85 |
model=MODEL_TEXT,
|
| 86 |
input=[
|
|
|
|
| 102 |
|
| 103 |
def summarize_with_openai(text: str) -> dict:
|
| 104 |
client = _client_lazy()
|
| 105 |
+
prompt = "以下の候補者レジュメ本文を、(1)300字、(2)100字、(3)1文 の3粒度で日本語要約してください。余計な記号は避け、事実を簡潔に。"
|
|
|
|
|
|
|
| 106 |
resp = client.responses.create(
|
| 107 |
model=MODEL_TEXT,
|
| 108 |
input=[
|
|
|
|
| 111 |
],
|
| 112 |
)
|
| 113 |
full = resp.output_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
return {
|
| 115 |
+
"300chars": full[:600] if len(full) > 0 else "",
|
| 116 |
"100chars": full[:120] if len(full) > 0 else "",
|
| 117 |
"onesent": full.split("。")[0] + "。" if "。" in full else full,
|
| 118 |
}
|