Spaces:
Sleeping
Sleeping
Update modules/workflow.py
Browse files- modules/workflow.py +65 -6
modules/workflow.py
CHANGED
|
@@ -2,18 +2,77 @@
|
|
| 2 |
import os
|
| 3 |
from typing import Dict, Any, List
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
-
from .rag_retriever import retrieve_contexts
|
| 8 |
except Exception:
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# exporters / utils
|
| 12 |
try:
|
| 13 |
from .exporters import export_docx, export_pptx
|
| 14 |
from .utils import ensure_dirs, export_dir, make_tracking_token
|
| 15 |
except Exception:
|
| 16 |
-
# 万一相対が失敗する環境でも動くように
|
| 17 |
from modules.exporters import export_docx, export_pptx # type: ignore
|
| 18 |
from modules.utils import ensure_dirs, export_dir, make_tracking_token # type: ignore
|
| 19 |
|
|
@@ -77,7 +136,7 @@ def run_full_workflow(
|
|
| 77 |
) -> Dict[str, Any]:
|
| 78 |
"""
|
| 79 |
app.py から呼ばれるメイン処理。
|
| 80 |
-
- RAG
|
| 81 |
- 簡易スコアリング
|
| 82 |
- 提案ドラフト生成
|
| 83 |
- DOCX/PPTX にエクスポート
|
|
|
|
| 2 |
import os
|
| 3 |
from typing import Dict, Any, List
|
| 4 |
|
| 5 |
+
# =========================
|
| 6 |
+
# retriever のロード(相対 → 絶対 → フォールバック実装)
|
| 7 |
+
# =========================
|
| 8 |
+
def _fallback_retrieve_contexts(query: str, top_k: int = 5) -> List[str]:
|
| 9 |
+
"""
|
| 10 |
+
フォールバックの簡易 RAG:
|
| 11 |
+
- modules.utils.data_dir()/chunks.jsonl を読み込み
|
| 12 |
+
- キーワード一致スコアで上位を返す(埋め込みなし)
|
| 13 |
+
"""
|
| 14 |
+
import json
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
try:
|
| 17 |
+
# 遅延 import(循環回避 & 起動高速化)
|
| 18 |
+
from modules.utils import data_dir, ensure_dirs
|
| 19 |
+
ensure_dirs()
|
| 20 |
+
p = data_dir() / "chunks.jsonl"
|
| 21 |
+
if not p.exists():
|
| 22 |
+
return []
|
| 23 |
+
rows = []
|
| 24 |
+
with open(p, "r", encoding="utf-8") as f:
|
| 25 |
+
for line in f:
|
| 26 |
+
line = line.strip()
|
| 27 |
+
if not line:
|
| 28 |
+
continue
|
| 29 |
+
try:
|
| 30 |
+
obj = json.loads(line)
|
| 31 |
+
if isinstance(obj, dict) and obj.get("text"):
|
| 32 |
+
rows.append(obj)
|
| 33 |
+
except Exception:
|
| 34 |
+
continue
|
| 35 |
+
if not rows:
|
| 36 |
+
return []
|
| 37 |
+
|
| 38 |
+
# ごく簡易なキーワード一致スコア
|
| 39 |
+
q = (query or "").lower()
|
| 40 |
+
def score(t: str) -> int:
|
| 41 |
+
t = (t or "").lower()
|
| 42 |
+
s = 0
|
| 43 |
+
for w in q.split():
|
| 44 |
+
if w and w in t:
|
| 45 |
+
s += 1
|
| 46 |
+
return s
|
| 47 |
+
|
| 48 |
+
scored = sorted(rows, key=lambda r: score(str(r.get("text",""))), reverse=True)
|
| 49 |
+
top = scored[:max(1, int(top_k))]
|
| 50 |
+
out = []
|
| 51 |
+
for r in top:
|
| 52 |
+
txt = str(r.get("text","")).strip()
|
| 53 |
+
src = r.get("source")
|
| 54 |
+
out.append(f"{txt}\n[source] {src}" if src else txt)
|
| 55 |
+
return out
|
| 56 |
+
except Exception:
|
| 57 |
+
return []
|
| 58 |
+
|
| 59 |
+
# まず相対 import を試す
|
| 60 |
try:
|
| 61 |
+
from .rag_retriever import retrieve_contexts # type: ignore
|
| 62 |
except Exception:
|
| 63 |
+
# 絶対 import
|
| 64 |
+
try:
|
| 65 |
+
from modules.rag_retriever import retrieve_contexts # type: ignore
|
| 66 |
+
except Exception:
|
| 67 |
+
# 最後の手段: フォールバックを使う
|
| 68 |
+
retrieve_contexts = _fallback_retrieve_contexts # type: ignore
|
| 69 |
+
|
| 70 |
|
| 71 |
+
# exporters / utils は相対を優先
|
| 72 |
try:
|
| 73 |
from .exporters import export_docx, export_pptx
|
| 74 |
from .utils import ensure_dirs, export_dir, make_tracking_token
|
| 75 |
except Exception:
|
|
|
|
| 76 |
from modules.exporters import export_docx, export_pptx # type: ignore
|
| 77 |
from modules.utils import ensure_dirs, export_dir, make_tracking_token # type: ignore
|
| 78 |
|
|
|
|
| 136 |
) -> Dict[str, Any]:
|
| 137 |
"""
|
| 138 |
app.py から呼ばれるメイン処理。
|
| 139 |
+
- RAG で上位コンテキスト取得(retriever が無い場合はフォールバック)
|
| 140 |
- 簡易スコアリング
|
| 141 |
- 提案ドラフト生成
|
| 142 |
- DOCX/PPTX にエクスポート
|