Corin1998 commited on
Commit
0fe9399
·
verified ·
1 Parent(s): 360c452

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -17,16 +17,29 @@ DESCRIPTION = """
17
  **LLM: OpenAI**(環境変数 `OPENAI_API_KEY` を設定してください)
18
  """
19
 
 
 
 
 
 
 
 
 
20
  def choose_source_text(source_text, url, pdf_file):
21
  log = ""
22
  text = (source_text or "").strip()
23
- if pdf_file is not None:
 
 
 
24
  try:
25
- text, lg = extract_pdf_file(pdf_file.name)
26
  log = f"[PDF] {lg}"
27
  return text, log
28
  except Exception as e:
29
  log = f"[PDF] Error: {e}"
 
 
30
  if url and url.strip():
31
  try:
32
  text, lg = fetch_url_text(url.strip())
@@ -34,8 +47,11 @@ def choose_source_text(source_text, url, pdf_file):
34
  return text, log
35
  except Exception as e:
36
  log = f"[URL] Error: {e}"
 
 
37
  if text:
38
  return text, "[TEXT] Using pasted text."
 
39
  raise ValueError("No valid source provided. Paste text, set a URL, or upload a PDF.")
40
 
41
  def run(company_name, brand_voice, region, language, tone, event_type, announce_date,
@@ -44,7 +60,7 @@ def run(company_name, brand_voice, region, language, tone, event_type, announce_
44
  goals = [g.strip() for g in (goals_text or "").split("\n") if g.strip()]
45
  chosen_text, ingest_log = choose_source_text(source_text, url, pdf_file)
46
 
47
- # Cap input length for safety
48
  MAX_CHARS = int(os.environ.get("MAX_SOURCE_CHARS", "20000"))
49
  chosen_text = chosen_text[:MAX_CHARS]
50
 
@@ -63,22 +79,17 @@ def run(company_name, brand_voice, region, language, tone, event_type, announce_
63
  include_esg=include_esg,
64
  include_ir=include_ir,
65
  )
 
66
  narrative = generate_narrative(seed)
67
  outputs = render_all(env, narrative, seed.company)
68
 
69
- # create zip for download
70
  ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
71
  zip_path = os.path.join(tempfile.gettempdir(), f"onepass_outputs_{ts}.zip")
72
  make_zip_from_outputs(outputs, zip_path)
73
 
74
- notion_result = None
75
- gdocs_result = None
76
- if do_notion:
77
- notion_result = export_to_notion(outputs)
78
- if do_gdocs:
79
- gdocs_result = export_to_google_docs(outputs)
80
 
81
- # Return individual markdowns + zip + exporter results
82
  press = outputs.get("press_release.md", "")
83
  lp = outputs.get("lp_section.md", "")
84
  sns = outputs.get("sns_thread.md", "")
@@ -95,8 +106,11 @@ def run(company_name, brand_voice, region, language, tone, event_type, announce_
95
  export_msg += f"Google Docs: {gdocs_result}\n"
96
 
97
  return press, lp, sns, pitch, esg, ir, zip_path, export_msg, chosen_text
 
98
  except Exception as e:
99
- return (f"Error: {e}",) + ("",)*5 + (None, f"") + ("",)
 
 
100
 
101
  with gr.Blocks(title="One-Pass PR/IR Generator") as demo:
102
  gr.Markdown(DESCRIPTION)
 
17
  **LLM: OpenAI**(環境変数 `OPENAI_API_KEY` を設定してください)
18
  """
19
 
20
+ def _pdf_path_from_input(pdf_file):
21
+ """gr.File(type='filepath') は str、場合により file-like を返すことがあるので両対応"""
22
+ if pdf_file is None:
23
+ return None
24
+ if isinstance(pdf_file, str):
25
+ return pdf_file
26
+ return getattr(pdf_file, "name", None) or getattr(pdf_file, "path", None)
27
+
28
  def choose_source_text(source_text, url, pdf_file):
29
  log = ""
30
  text = (source_text or "").strip()
31
+
32
+ # 1) PDF優先
33
+ pdf_path = _pdf_path_from_input(pdf_file)
34
+ if pdf_path:
35
  try:
36
+ text, lg = extract_pdf_file(pdf_path)
37
  log = f"[PDF] {lg}"
38
  return text, log
39
  except Exception as e:
40
  log = f"[PDF] Error: {e}"
41
+
42
+ # 2) URL
43
  if url and url.strip():
44
  try:
45
  text, lg = fetch_url_text(url.strip())
 
47
  return text, log
48
  except Exception as e:
49
  log = f"[URL] Error: {e}"
50
+
51
+ # 3) テキスト貼り付け
52
  if text:
53
  return text, "[TEXT] Using pasted text."
54
+
55
  raise ValueError("No valid source provided. Paste text, set a URL, or upload a PDF.")
56
 
57
  def run(company_name, brand_voice, region, language, tone, event_type, announce_date,
 
60
  goals = [g.strip() for g in (goals_text or "").split("\n") if g.strip()]
61
  chosen_text, ingest_log = choose_source_text(source_text, url, pdf_file)
62
 
63
+ # 入力長の安全上限
64
  MAX_CHARS = int(os.environ.get("MAX_SOURCE_CHARS", "20000"))
65
  chosen_text = chosen_text[:MAX_CHARS]
66
 
 
79
  include_esg=include_esg,
80
  include_ir=include_ir,
81
  )
82
+
83
  narrative = generate_narrative(seed)
84
  outputs = render_all(env, narrative, seed.company)
85
 
 
86
  ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
87
  zip_path = os.path.join(tempfile.gettempdir(), f"onepass_outputs_{ts}.zip")
88
  make_zip_from_outputs(outputs, zip_path)
89
 
90
+ notion_result = export_to_notion(outputs) if do_notion else None
91
+ gdocs_result = export_to_google_docs(outputs) if do_gdocs else None
 
 
 
 
92
 
 
93
  press = outputs.get("press_release.md", "")
94
  lp = outputs.get("lp_section.md", "")
95
  sns = outputs.get("sns_thread.md", "")
 
106
  export_msg += f"Google Docs: {gdocs_result}\n"
107
 
108
  return press, lp, sns, pitch, esg, ir, zip_path, export_msg, chosen_text
109
+
110
  except Exception as e:
111
+ # ここでスタックの要約を返して原因を掴みやすく
112
+ err = f"Error: {type(e).__name__}: {e}"
113
+ return (err,) + ("",)*5 + (None, err) + ("",)
114
 
115
  with gr.Blocks(title="One-Pass PR/IR Generator") as demo:
116
  gr.Markdown(DESCRIPTION)