Corin1998 commited on
Commit
064525a
·
verified ·
1 Parent(s): c0f7869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os, json, traceback, io
2
- from typing import List
3
  from fastapi import FastAPI, Request, HTTPException
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from fastapi.responses import RedirectResponse, PlainTextResponse
@@ -8,14 +7,13 @@ from dotenv import load_dotenv
8
 
9
  load_dotenv()
10
 
11
- # ========= 内部ログ用バッファ(画面で見られるようにする) =========
12
  _APP_LOG = io.StringIO()
13
-
14
  def _log(msg: str):
15
  print(msg, flush=True)
16
  _APP_LOG.write(msg + "\n")
17
 
18
- # ========= 例外は必ず画面にも表示(Internal Server Error対策) =========
19
  async def _exception_printer(request: Request, exc: Exception):
20
  tb = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
21
  _log("=== Unhandled Exception ===\n" + tb)
@@ -30,14 +28,14 @@ def _lazy_imports_for_main():
30
  from modules.emailer import build_tracking_url
31
  return index_files_and_urls, run_full_workflow, build_tracking_url
32
 
33
- # ========= Gradio コールバック =========
34
- def ui_company_score_and_proposal(company_name: str,
35
- company_website: str,
36
- lead_email: str,
37
- urls_text: str,
38
- files: List[gr.File] | None,
39
- custom_objective: str,
40
- temperature: float = 0.4):
41
  try:
42
  index_files_and_urls, run_full_workflow, build_tracking_url = _lazy_imports_for_main()
43
  _log("[init] lazy imports loaded")
@@ -47,7 +45,15 @@ def ui_company_score_and_proposal(company_name: str,
47
  return "### ❌ 初期化エラー\n```\n" + tb + "\n```"
48
 
49
  urls = [u.strip() for u in (urls_text or "").splitlines() if u.strip()]
50
- file_paths = [f.name for f in (files or [])]
 
 
 
 
 
 
 
 
51
 
52
  # 1) インデックス
53
  try:
@@ -65,7 +71,7 @@ def ui_company_score_and_proposal(company_name: str,
65
  company_website=company_website,
66
  lead_email=lead_email,
67
  objective=custom_objective,
68
- temperature=temperature
69
  )
70
  _log("[workflow] finished")
71
  except Exception:
@@ -149,7 +155,7 @@ with gr.Blocks(title="営業自動化 Agent Studio") as demo:
149
  health_btn.click(fn=get_health_text, inputs=None, outputs=health_out)
150
  log_btn.click(fn=get_runtime_log, inputs=None, outputs=log_out)
151
 
152
- # ========= FastAPI(/ はUI、/t と /health はAPI。/ui に来た人は / へ転送) =========
153
  app = FastAPI(title="Agent Studio - Docker (root UI)")
154
  app.add_middleware(
155
  CORSMiddleware,
@@ -163,8 +169,7 @@ def health():
163
 
164
  @app.get("/ui")
165
  def old_ui():
166
- # 旧リンクで来た場合も Space 内で完結して同じページを表示
167
- return RedirectResponse(url="/")
168
 
169
  @app.get("/t/{token}")
170
  def track_click(token: str, request: Request):
@@ -183,5 +188,5 @@ def track_click(token: str, request: Request):
183
  _log("[error] tracking failed\n" + tb)
184
  return PlainTextResponse(f"tracking error\n\n{tb}", status_code=500)
185
 
186
- # ルート(/)で Gradio を表示(Space内完結)
187
  app = gr.mount_gradio_app(app, demo, path="/")
 
1
  import os, json, traceback, io
 
2
  from fastapi import FastAPI, Request, HTTPException
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from fastapi.responses import RedirectResponse, PlainTextResponse
 
7
 
8
  load_dotenv()
9
 
10
+ # ========= 画面から見られる簡易ログ =========
11
  _APP_LOG = io.StringIO()
 
12
  def _log(msg: str):
13
  print(msg, flush=True)
14
  _APP_LOG.write(msg + "\n")
15
 
16
+ # ========= 例外は必ず画面にも表示 =========
17
  async def _exception_printer(request: Request, exc: Exception):
18
  tb = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
19
  _log("=== Unhandled Exception ===\n" + tb)
 
28
  from modules.emailer import build_tracking_url
29
  return index_files_and_urls, run_full_workflow, build_tracking_url
30
 
31
+ # ========= Gradio コールバック(型アノテ外し版) =========
32
+ def ui_company_score_and_proposal(company_name,
33
+ company_website,
34
+ lead_email,
35
+ urls_text,
36
+ files=None,
37
+ custom_objective="",
38
+ temperature=0.4):
39
  try:
40
  index_files_and_urls, run_full_workflow, build_tracking_url = _lazy_imports_for_main()
41
  _log("[init] lazy imports loaded")
 
45
  return "### ❌ 初期化エラー\n```\n" + tb + "\n```"
46
 
47
  urls = [u.strip() for u in (urls_text or "").splitlines() if u.strip()]
48
+ # gr.File or str のどちらでも拾えるように安全化
49
+ try:
50
+ iterable = files or []
51
+ # Gradio の multiple=False のときに単体で来た場合に備える
52
+ if not isinstance(iterable, (list, tuple)):
53
+ iterable = [iterable]
54
+ file_paths = [getattr(f, "name", str(f)) for f in iterable if f]
55
+ except Exception:
56
+ file_paths = []
57
 
58
  # 1) インデックス
59
  try:
 
71
  company_website=company_website,
72
  lead_email=lead_email,
73
  objective=custom_objective,
74
+ temperature=float(temperature or 0.4),
75
  )
76
  _log("[workflow] finished")
77
  except Exception:
 
155
  health_btn.click(fn=get_health_text, inputs=None, outputs=health_out)
156
  log_btn.click(fn=get_runtime_log, inputs=None, outputs=log_out)
157
 
158
+ # ========= FastAPI(/ はUI、/ui は / へ転送、/t と /health はAPI =========
159
  app = FastAPI(title="Agent Studio - Docker (root UI)")
160
  app.add_middleware(
161
  CORSMiddleware,
 
169
 
170
  @app.get("/ui")
171
  def old_ui():
172
+ return RedirectResponse(url="/") # 旧リンク互換
 
173
 
174
  @app.get("/t/{token}")
175
  def track_click(token: str, request: Request):
 
188
  _log("[error] tracking failed\n" + tb)
189
  return PlainTextResponse(f"tracking error\n\n{tb}", status_code=500)
190
 
191
+ # ルート(/)で Gradio を表示(Space 内で完結)
192
  app = gr.mount_gradio_app(app, demo, path="/")