Yasu777 commited on
Commit
a060cee
1 Parent(s): 70acdf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -67,11 +67,19 @@ def process_article(new_keyword, other_keywords, action, edited_article_structur
67
 
68
  with open("output3.txt", "r", encoding="utf-8") as f:
69
  final_article_content = f.read()
70
- final_article_html = final_article_content # 本文作成の出力がMarkdownとHTMLの両方を更新すると仮定
 
 
 
 
 
71
  except Exception as e:
72
  final_article_content = f"予期せぬエラーが発生しました:{str(e)}"
73
- final_article_html = ""
74
  print(f"予期せぬエラーが発生しました:{str(e)}")
 
 
 
75
  return response_text, final_article_content, final_article_html
76
 
77
  def custom_html_to_markdown(html):
@@ -94,17 +102,16 @@ def custom_html_to_markdown(html):
94
  # 最終的なMarkdownテキストの取得
95
  return soup.get_text()
96
 
97
- def display_content(content, format):
98
- if format == "Markdown":
99
- return custom_html_to_markdown(content)
100
- else:
101
- return content
102
 
103
- def switch_display_format(format_choice, md_content, html_content):
104
- if format_choice == "HTML":
105
- return gr.update(value=html_content, visible=True), gr.update(visible=False)
106
- else:
107
- return gr.update(visible=True), gr.update(value=md_content, visible=False)
108
 
109
  # Gradioアプリの設定
110
  with gr.Blocks(css='''
@@ -121,8 +128,8 @@ with gr.Blocks(css='''
121
  create_structure_button = gr.Button("構成作成")
122
  editable_output2 = gr.Textbox(label="編集可能な記事構成", lines=10, placeholder="記事構成がここに表示されます")
123
  create_article_button = gr.Button("本文作成")
124
- formatted_final_article_md = gr.Markdown(label="最終的な記事本文")
125
- formatted_final_article_html = gr.HTML(label="最終的な記事本文", visible=False)
126
 
127
  create_structure_button.click(
128
  fn=process_article,
@@ -133,17 +140,10 @@ with gr.Blocks(css='''
133
  create_article_button.click(
134
  fn=process_article,
135
  inputs=[new_keyword, other_keywords, gr.State(value="本文作成"), editable_output2],
136
- outputs=[editable_output2, formatted_final_article_md, formatted_final_article_html]
137
  )
138
 
139
- # 表示形式の選択を追加
140
- display_format = gr.Radio(label="表示形式", choices=["Markdown", "HTML"], value="Markdown", elem_classes="right-align")
141
-
142
- display_format.change(
143
- fn=switch_display_format,
144
- inputs=[display_format, formatted_final_article_md, formatted_final_article_html],
145
- outputs=[formatted_final_article_md, formatted_final_article_html]
146
- )
147
 
148
  with gr.Column():
149
  gr.Markdown(" ", elem_id="spacer") # 空白を作るための見えないMarkdown
@@ -163,4 +163,4 @@ with gr.Blocks(css='''
163
  search_button.click(tavily_search_interface, inputs=[selected_text, domain_filter], outputs=[search_results_md, search_results_html])
164
  search_display_format.change(switch_search_display_format, inputs=[search_display_format], outputs=[search_results_md, search_results_html])
165
 
166
- app.launch()
 
67
 
68
  with open("output3.txt", "r", encoding="utf-8") as f:
69
  final_article_content = f.read()
70
+ # final_article_htmlをgr.HTMLオブジェクトとして再初期化
71
+ final_article_html = gr.HTML(value=final_article_content, visible=True)
72
+
73
+ # デバッグ出力追加
74
+ print("After updating from output3.txt - final_article_html value:", final_article_html)
75
+ print("After updating from output3.txt - final_article_html type:", type(final_article_html))
76
  except Exception as e:
77
  final_article_content = f"予期せぬエラーが発生しました:{str(e)}"
78
+ final_article_html = gr.HTML(visible=False) # ここでも例外が発生した場合に備えて適切なオブジェクトを設定
79
  print(f"予期せぬエラーが発生しました:{str(e)}")
80
+ print("final_article_html after error:", final_article_html)
81
+ print("final_article_html type after error:", type(final_article_html))
82
+
83
  return response_text, final_article_content, final_article_html
84
 
85
  def custom_html_to_markdown(html):
 
102
  # 最終的なMarkdownテキストの取得
103
  return soup.get_text()
104
 
105
+ def display_content(content, formatted_final_article_md, formatted_final_article_html):
106
+ print("Type of formatted_final_article_html before processing in display_content:", type(formatted_final_article_html))
107
+ if type(formatted_final_article_html) is not gr.components.HTML:
108
+ print("Error: formatted_final_article_html is not a gr.HTML object")
109
+ return formatted_final_article_md, formatted_final_article_html # エラーがあれば早期リターン
110
 
111
+ markdown_content = custom_html_to_markdown(content)
112
+ formatted_final_article_md.update(value=markdown_content, visible=True)
113
+ formatted_final_article_html.update(visible=False) # HTMLを非表示にする
114
+ return formatted_final_article_md, formatted_final_article_html
 
115
 
116
  # Gradioアプリの設定
117
  with gr.Blocks(css='''
 
128
  create_structure_button = gr.Button("構成作成")
129
  editable_output2 = gr.Textbox(label="編集可能な記事構成", lines=10, placeholder="記事構成がここに表示されます")
130
  create_article_button = gr.Button("本文作成")
131
+ formatted_final_article_md = gr.Markdown(value="", visible=True, label="最終的な記事本文")
132
+ formatted_final_article_html = gr.HTML(value="", visible=False, label="最終的な記事本文(HTML)")
133
 
134
  create_structure_button.click(
135
  fn=process_article,
 
140
  create_article_button.click(
141
  fn=process_article,
142
  inputs=[new_keyword, other_keywords, gr.State(value="本文作成"), editable_output2],
143
+ outputs=[editable_output2, formatted_final_article_md]
144
  )
145
 
146
+ print("Setup change event for display_format with MD and HTML components")
 
 
 
 
 
 
 
147
 
148
  with gr.Column():
149
  gr.Markdown(" ", elem_id="spacer") # 空白を作るための見えないMarkdown
 
163
  search_button.click(tavily_search_interface, inputs=[selected_text, domain_filter], outputs=[search_results_md, search_results_html])
164
  search_display_format.change(switch_search_display_format, inputs=[search_display_format], outputs=[search_results_md, search_results_html])
165
 
166
+ app.launch()