Corin1998 commited on
Commit
e8cc220
·
verified ·
1 Parent(s): dc31d2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -35
app.py CHANGED
@@ -1,25 +1,17 @@
1
  import os
2
  import json
3
- from typing import Any
4
-
5
  import gradio as gr
6
  from fastapi import FastAPI
7
 
8
 
9
- # ---- 遅延インポート(重い依存は起動後に読み込む) ----
10
  def _lazy_imports_for_main():
11
  from modules.utils import ensure_dirs # noqa: F401
12
  from modules.rag_indexer import index_files_and_urls
13
  from modules.workflow import run_full_workflow
14
 
15
- # 書き込み可能ディレクトリなどを初期化
16
  ensure_dirs()
17
-
18
- def build_tracking_url(token: str) -> str:
19
- # デモではダミー(詳細トラッキングは未実装)
20
- return f"/t/{token}"
21
-
22
- return index_files_and_urls, run_full_workflow, build_tracking_url
23
 
24
 
25
  # ---- UI コールバック ----
@@ -33,15 +25,13 @@ def ui_company_score_and_proposal(
33
  ):
34
  """
35
  戻り値順:
36
- score_json(str), contexts_text(str), proposal_md(str),
37
  docx_path(str), pptx_path(str), next_actions(str), index_report(str)
38
  """
39
- index_files_and_urls, run_full_workflow, build_tracking_url = _lazy_imports_for_main()
40
 
41
- # URL ファイルパスを正規化
42
  urls = [u.strip() for u in (urls_text or "").splitlines() if u.strip()]
43
-
44
- # gr.File(file_count="multiple") は 単一 or list を返す可能性がある
45
  file_paths = []
46
  if files:
47
  if isinstance(files, list):
@@ -50,14 +40,14 @@ def ui_company_score_and_proposal(
50
  file_paths = [getattr(files, "name", None) or files]
51
  file_paths = [p for p in file_paths if p]
52
 
53
- # まずインデックス(空でOK
54
  try:
55
  index_report = index_files_and_urls(file_paths=file_paths, urls=urls)
56
  except Exception as e:
57
  index_report = f"Index error:\n\n{e}"
58
 
59
- # メインワークフロー
60
- result: dict[str, Any] = run_full_workflow(
61
  company_name=company_name or "(未入力)",
62
  company_website=company_website or "",
63
  lead_email=lead_email or "",
@@ -67,17 +57,17 @@ def ui_company_score_and_proposal(
67
 
68
  score_json = json.dumps(result.get("score", {}), ensure_ascii=False, indent=2)
69
  contexts_text = "\n\n---\n\n".join(result.get("top_contexts") or [])
70
- proposal_md = result.get("proposal_markdown", "")
71
  next_actions = result.get("next_actions", "")
72
 
73
- # エクスポートされたファイルパス(/tmp 下)
74
  docx_path = result.get("exports", {}).get("docx_path", "")
75
  pptx_path = result.get("exports", {}).get("pptx_path", "")
76
 
77
  return (
78
  score_json,
79
  contexts_text,
80
- proposal_md,
81
  docx_path,
82
  pptx_path,
83
  next_actions,
@@ -87,7 +77,7 @@ def ui_company_score_and_proposal(
87
 
88
  # ---- Gradio UI ----
89
  with gr.Blocks(title="営業自動化 Agent Studio", theme=gr.themes.Soft()) as demo:
90
- gr.Markdown("## 営業自動化 Agent Studio(Space内完結)")
91
 
92
  with gr.Row():
93
  with gr.Column():
@@ -101,37 +91,35 @@ with gr.Blocks(title="営業自動化 Agent Studio", theme=gr.themes.Soft()) as
101
  lines=4,
102
  placeholder="https://example.com\nhttps://example.com/blog/post1",
103
  )
104
- # ★ JSON スキーマの安定化のため Files ではなく File を使用(複数可
105
  files = gr.File(label="インデックス対象ファイル(任意・テキスト/MD推奨)", file_count="multiple")
106
 
107
  run_btn = gr.Button("ワークフロー実行", variant="primary")
108
 
109
  with gr.Column():
110
- # ★ gr.Code(language='json') は一部環境でスキーマ生成エラーのため Textbox に変更
111
  score_json = gr.Textbox(label="✅ 企業スコア(JSON)", lines=10, interactive=False)
112
- contexts_text = gr.Textbox(label="🧠 抽出コンテキスト(上位)", lines=8)
113
- proposal_md = gr.Markdown(label="✍️ 提案ドラフト(Markdown)")
114
 
115
  with gr.Row():
116
- # 出力は File ではなく DownloadButton を使用(スマが安定
117
- docx_btn = gr.DownloadButton(label="DOCX ダウンロード", variant="secondary")
118
- pptx_btn = gr.DownloadButton(label="PPTX ダウンロード", variant="secondary")
119
 
120
- next_actions = gr.Textbox(label="🤖 次アクション提案", lines=4)
121
- index_report = gr.Textbox(label="🧩 インデックス更新ログ", lines=3, interactive=False)
122
 
123
  run_btn.click(
124
  fn=ui_company_score_and_proposal,
125
  inputs=[company_name, company_website, lead_email, objective, urls_text, files],
126
- outputs=[score_json, contexts_text, proposal_md, docx_btn, pptx_btn, next_actions, index_report],
127
  )
128
 
129
- # ---- FastAPI にマウント(Spaces は uvicorn app:app で起動するため app を公開)----
130
  app = FastAPI()
131
  app = gr.mount_gradio_app(app, demo.queue(), path="/")
132
 
133
-
134
- # ローカル起動用(Spaces では未使用)
135
  if __name__ == "__main__":
136
  import uvicorn
137
 
 
1
  import os
2
  import json
 
 
3
  import gradio as gr
4
  from fastapi import FastAPI
5
 
6
 
7
+ # ---- 遅延インポート(起動高速化 & 依存衝突を回避) ----
8
  def _lazy_imports_for_main():
9
  from modules.utils import ensure_dirs # noqa: F401
10
  from modules.rag_indexer import index_files_and_urls
11
  from modules.workflow import run_full_workflow
12
 
 
13
  ensure_dirs()
14
+ return index_files_and_urls, run_full_workflow
 
 
 
 
 
15
 
16
 
17
  # ---- UI コールバック ----
 
25
  ):
26
  """
27
  戻り値順:
28
+ score_json(str), contexts_text(str), proposal_text(str),
29
  docx_path(str), pptx_path(str), next_actions(str), index_report(str)
30
  """
31
+ index_files_and_urls, run_full_workflow = _lazy_imports_for_main()
32
 
33
+ # URL & ファイル
34
  urls = [u.strip() for u in (urls_text or "").splitlines() if u.strip()]
 
 
35
  file_paths = []
36
  if files:
37
  if isinstance(files, list):
 
40
  file_paths = [getattr(files, "name", None) or files]
41
  file_paths = [p for p in file_paths if p]
42
 
43
+ # インデックス(失敗して先へ進む
44
  try:
45
  index_report = index_files_and_urls(file_paths=file_paths, urls=urls)
46
  except Exception as e:
47
  index_report = f"Index error:\n\n{e}"
48
 
49
+ # メイン処理
50
+ result = run_full_workflow(
51
  company_name=company_name or "(未入力)",
52
  company_website=company_website or "",
53
  lead_email=lead_email or "",
 
57
 
58
  score_json = json.dumps(result.get("score", {}), ensure_ascii=False, indent=2)
59
  contexts_text = "\n\n---\n\n".join(result.get("top_contexts") or [])
60
+ proposal_text = result.get("proposal_markdown", "")
61
  next_actions = result.get("next_actions", "")
62
 
63
+ # 出力ファイル(/tmp を想定
64
  docx_path = result.get("exports", {}).get("docx_path", "")
65
  pptx_path = result.get("exports", {}).get("pptx_path", "")
66
 
67
  return (
68
  score_json,
69
  contexts_text,
70
+ proposal_text,
71
  docx_path,
72
  pptx_path,
73
  next_actions,
 
77
 
78
  # ---- Gradio UI ----
79
  with gr.Blocks(title="営業自動化 Agent Studio", theme=gr.themes.Soft()) as demo:
80
+ gr.Markdown("## 営業自動化 Agent Studio(Space内完結・ダウンロード可)")
81
 
82
  with gr.Row():
83
  with gr.Column():
 
91
  lines=4,
92
  placeholder="https://example.com\nhttps://example.com/blog/post1",
93
  )
94
+ # Files ではなく File(multiple) を使用(スキーマを単純化
95
  files = gr.File(label="インデックス対象ファイル(任意・テキスト/MD推奨)", file_count="multiple")
96
 
97
  run_btn = gr.Button("ワークフロー実行", variant="primary")
98
 
99
  with gr.Column():
100
+ # Code/Markdown なく Textbox に統してスキーマ安定化
101
  score_json = gr.Textbox(label="✅ 企業スコア(JSON)", lines=10, interactive=False)
102
+ contexts_text = gr.Textbox(label="🧠 抽出コンテキスト(上位)", lines=8, interactive=False)
103
+ proposal_text = gr.Textbox(label="✍️ 提案ドラフト(Markdownテキスト)", lines=16)
104
 
105
  with gr.Row():
106
+ # 出力は安定の gr.File(を返せばダウンロド可能
107
+ docx_file = gr.File(label="DOCX ダウンロード")
108
+ pptx_file = gr.File(label="PPTX ダウンロード")
109
 
110
+ next_actions = gr.Textbox(label="🤖 次アクション提案", lines=4, interactive=False)
111
+ index_report = gr.Textbox(label="🧩 インデックス更新ログ", lines=4, interactive=False)
112
 
113
  run_btn.click(
114
  fn=ui_company_score_and_proposal,
115
  inputs=[company_name, company_website, lead_email, objective, urls_text, files],
116
+ outputs=[score_json, contexts_text, proposal_text, docx_file, pptx_file, next_actions, index_report],
117
  )
118
 
119
+ # ---- FastAPI にマウント(Spaces は uvicorn app:app で起動)----
120
  app = FastAPI()
121
  app = gr.mount_gradio_app(app, demo.queue(), path="/")
122
 
 
 
123
  if __name__ == "__main__":
124
  import uvicorn
125