Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,141 +1,13 @@
|
|
| 1 |
-
import os, json, traceback
|
| 2 |
-
from typing import List
|
| 3 |
import gradio as gr
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
"
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
outputs="markdown",
|
| 14 |
-
title="Agent Studio (Safe Mode)"
|
| 15 |
-
)
|
| 16 |
-
return fallback
|
| 17 |
|
| 18 |
-
|
| 19 |
-
# ここから重い依存は try 内で遅延 import する
|
| 20 |
-
try:
|
| 21 |
-
from modules.utils import ensure_dirs, verify_tracking_token, log_event
|
| 22 |
-
ensure_dirs()
|
| 23 |
-
except Exception:
|
| 24 |
-
raise RuntimeError("modules.utils の初期化に失敗:\n" + traceback.format_exc())
|
| 25 |
-
|
| 26 |
-
# ---- クリック計測(/?t=TOKEN で呼ばれる)----
|
| 27 |
-
def handle_tracking_on_load(request: gr.Request):
|
| 28 |
-
try:
|
| 29 |
-
token = (request.query_params or {}).get("t")
|
| 30 |
-
if not token:
|
| 31 |
-
return ""
|
| 32 |
-
payload = verify_tracking_token(token)
|
| 33 |
-
if not payload:
|
| 34 |
-
return "<p>Invalid tracking token.</p>"
|
| 35 |
-
ip = request.client.host if request.client else "unknown"
|
| 36 |
-
ua = request.headers.get("User-Agent", "") if request.headers else ""
|
| 37 |
-
log_event("click", payload, {"ip": ip, "ua": ua})
|
| 38 |
-
redirect_to = payload.get("redirect") or os.getenv("PUBLIC_BASE_URL", "/")
|
| 39 |
-
return f"""<html><head>
|
| 40 |
-
<meta http-equiv="refresh" content="0; url={redirect_to}">
|
| 41 |
-
</head><body>Redirecting...</body></html>"""
|
| 42 |
-
except Exception:
|
| 43 |
-
return f"<pre>{traceback.format_exc()}</pre>"
|
| 44 |
-
|
| 45 |
-
# ---- メイン処理(ボタン押下時にだけ重い import)----
|
| 46 |
-
def ui_company_score_and_proposal(company_name: str,
|
| 47 |
-
company_website: str,
|
| 48 |
-
lead_email: str,
|
| 49 |
-
urls_text: str,
|
| 50 |
-
files: List[gr.File] | None,
|
| 51 |
-
custom_objective: str,
|
| 52 |
-
temperature: float = 0.4):
|
| 53 |
-
try:
|
| 54 |
-
from modules.rag_indexer import index_files_and_urls
|
| 55 |
-
from modules.workflow import run_full_workflow
|
| 56 |
-
from modules.emailer import build_tracking_url
|
| 57 |
-
except Exception:
|
| 58 |
-
return "### ❌ 初期化エラー\n```\n" + traceback.format_exc() + "\n```"
|
| 59 |
-
|
| 60 |
-
urls = [u.strip() for u in (urls_text or "").splitlines() if u.strip()]
|
| 61 |
-
file_paths = [f.name for f in (files or [])]
|
| 62 |
-
|
| 63 |
-
# 1) インデックス
|
| 64 |
-
try:
|
| 65 |
-
index_report = index_files_and_urls(file_paths=file_paths, urls=urls)
|
| 66 |
-
except Exception:
|
| 67 |
-
index_report = "Index error:\n```\n" + traceback.format_exc() + "\n```"
|
| 68 |
-
|
| 69 |
-
# 2) ワークフロー
|
| 70 |
-
try:
|
| 71 |
-
result = run_full_workflow(
|
| 72 |
-
company_name=company_name,
|
| 73 |
-
company_website=company_website,
|
| 74 |
-
lead_email=lead_email,
|
| 75 |
-
objective=custom_objective,
|
| 76 |
-
temperature=temperature
|
| 77 |
-
)
|
| 78 |
-
except Exception:
|
| 79 |
-
return "### ❌ ワークフロー実行エラー\n```\n" + traceback.format_exc() + "\n```"
|
| 80 |
-
|
| 81 |
-
# 3) 表示
|
| 82 |
-
outputs = []
|
| 83 |
-
outputs.append("### ✅ 企業スコア\n" + json.dumps(result.get("score", {}), ensure_ascii=False, indent=2))
|
| 84 |
-
ctxs = result.get("top_contexts", [])
|
| 85 |
-
outputs.append("### 🧠 抽出コンテキスト(上位)\n" + "\n\n".join([f"- {c[:300]}..." for c in ctxs]))
|
| 86 |
-
outputs.append("### ✍️ 提案ドラフト\n" + result.get("proposal_markdown", ""))
|
| 87 |
-
|
| 88 |
-
exports = result.get("exports", {})
|
| 89 |
-
outputs.append("### 📎 エクスポート\n" + "\n".join([
|
| 90 |
-
f"- DOCX: {exports.get('docx_path','')}",
|
| 91 |
-
f"- PPTX: {exports.get('pptx_path','')}"
|
| 92 |
-
]))
|
| 93 |
-
|
| 94 |
-
if lead_email:
|
| 95 |
-
email = result.get("email", {})
|
| 96 |
-
outputs.append("### ✉️ メール準備\n" + f"To: {lead_email}\nSubject: {email.get('subject','')}\n\n{email.get('body','')}")
|
| 97 |
-
try:
|
| 98 |
-
from modules.emailer import build_tracking_url
|
| 99 |
-
outputs.append(f"(本文内の計測リンク例)\n{build_tracking_url('preview-only', {'company':company_name})}")
|
| 100 |
-
except Exception:
|
| 101 |
-
pass
|
| 102 |
-
|
| 103 |
-
outputs.append("### 🤖 次アクション提案\n" + result.get("next_actions", ""))
|
| 104 |
-
outputs.append("### 🧩 インデックス更新ログ\n" + index_report)
|
| 105 |
-
return "\n\n".join(outputs)
|
| 106 |
-
|
| 107 |
-
# ---- Gradio Blocks ----
|
| 108 |
-
with gr.Blocks(title="営���自動化 Agent Studio") as demo:
|
| 109 |
-
tracking_html = gr.HTML(visible=True)
|
| 110 |
-
gr.Markdown("# 営業自動化 Agent Studio")
|
| 111 |
-
with gr.Row():
|
| 112 |
-
with gr.Column():
|
| 113 |
-
company_name = gr.Textbox(label="企業名", placeholder="例)Acme Corp")
|
| 114 |
-
company_website = gr.Textbox(label="企業サイトURL", placeholder="例)https://www.acme.com")
|
| 115 |
-
lead_email = gr.Textbox(label="送信先メール(任意)", placeholder="例)lead@acme.com")
|
| 116 |
-
custom_objective = gr.Textbox(label="提案の目的/狙い(任意)", placeholder="例)SaaS導入の無料PoC打診")
|
| 117 |
-
urls_text = gr.Textbox(label="RAG用URL(複数は改行)", lines=4)
|
| 118 |
-
files = gr.File(label="RAG用ファイル(複数可)", file_count="multiple")
|
| 119 |
-
temperature = gr.Slider(0.0, 1.0, value=0.4, step=0.05, label="生成温度")
|
| 120 |
-
run_btn = gr.Button("ワークフロー実行", variant="primary")
|
| 121 |
-
with gr.Column():
|
| 122 |
-
out = gr.Markdown(label="結果")
|
| 123 |
-
|
| 124 |
-
run_btn.click(
|
| 125 |
-
fn=ui_company_score_and_proposal,
|
| 126 |
-
inputs=[company_name, company_website, lead_email, urls_text, files, custom_objective, temperature],
|
| 127 |
-
outputs=[out]
|
| 128 |
-
)
|
| 129 |
-
demo.load(fn=handle_tracking_on_load, inputs=None, outputs=tracking_html)
|
| 130 |
-
|
| 131 |
-
return demo
|
| 132 |
-
|
| 133 |
-
# --- アプリ構築。失敗したらフェイルセーフを返す ---
|
| 134 |
-
try:
|
| 135 |
-
demo = _build_main_ui()
|
| 136 |
-
except Exception as _e:
|
| 137 |
-
err = "初期化に失敗しました。Safe Modeで起動しています。\n\n```\n" + traceback.format_exc() + "\n```"
|
| 138 |
-
demo = _fallback_ui(err)
|
| 139 |
-
|
| 140 |
-
# Spaces が検出できるように両方の名前でエクスポート
|
| 141 |
app = demo
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
def ping():
|
| 4 |
+
return "pong 🎯"
|
| 5 |
|
| 6 |
+
with gr.Blocks(title="Boot Probe") as demo:
|
| 7 |
+
gr.Markdown("## Gradio boot OK? \nこの画面が出れば **Spaces がアプリを検出**できています。")
|
| 8 |
+
btn = gr.Button("Ping")
|
| 9 |
+
out = gr.Markdown()
|
| 10 |
+
btn.click(fn=ping, inputs=None, outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Spaces は 'app' も探すことがあるので両方エクスポート
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
app = demo
|