Yasu777 commited on
Commit
66fecd6
·
verified ·
1 Parent(s): d2784f3

Update article_generator.py

Browse files
Files changed (1) hide show
  1. article_generator.py +38 -20
article_generator.py CHANGED
@@ -4,13 +4,10 @@ import json
4
  import requests
5
  import gradio as gr
6
  from bs4 import BeautifulSoup
7
- import openai
8
  from langchain.chat_models import ChatOpenAI
9
  from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
10
  from langchain.llms import OpenAI
11
  from langchain.agents.tools import Tool
12
- import asyncio
13
- from datetime import timedelta
14
 
15
  # APIキーの設定
16
  openai.api_key = os.getenv("OPENAI_API_KEY")
@@ -111,29 +108,50 @@ def generate_article(editable_output2):
111
  <h3>{h3}</h3>
112
  "{h3}"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。""")
113
 
114
- user_message = {
115
- "role": "user",
116
- "content": "\n".join(instructions)
117
- }
 
 
 
 
 
118
 
119
- response = openai.ChatCompletion.create(
120
- model="gpt-4-turbo-2024-04-09",
121
- messages=[system_message, user_message],
122
- temperature=0.7,
123
- )
124
- result = response.choices[0]["message"]["content"]
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  with open("output3.txt", "w", encoding="utf-8") as f:
127
- f.write(result)
128
- return result
129
 
130
  with gr.Blocks() as app:
131
- editable_output2 = gr.Textbox(label="編集可能な記事構成", placeholder="HTML content here...", lines=10)
132
- final_article = gr.Textbox(label="最終的な記事本文", lines=20, placeholder="Generated article content will appear here.")
133
- generate_button = gr.Button("記事を生成")
134
-
 
 
 
135
  generate_button.click(
136
  fn=generate_article,
137
  inputs=[editable_output2],
138
  outputs=[final_article]
139
- )
 
 
 
4
  import requests
5
  import gradio as gr
6
  from bs4 import BeautifulSoup
 
7
  from langchain.chat_models import ChatOpenAI
8
  from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
9
  from langchain.llms import OpenAI
10
  from langchain.agents.tools import Tool
 
 
11
 
12
  # APIキーの設定
13
  openai.api_key = os.getenv("OPENAI_API_KEY")
 
108
  <h3>{h3}</h3>
109
  "{h3}"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。""")
110
 
111
+ # トークン数を制限するためにメッセージを分割
112
+ split_instructions = []
113
+ current_chunk = ""
114
+ for instruction in instructions:
115
+ if len(current_chunk + instruction) > 15000: # 適宜サイズ調整
116
+ split_instructions.append(current_chunk)
117
+ current_chunk = instruction
118
+ else:
119
+ current_chunk += instruction
120
 
121
+ if current_chunk:
122
+ split_instructions.append(current_chunk)
123
+
124
+ results = []
125
+ for i, split_instruction in enumerate(split_instructions):
126
+ user_message = {
127
+ "role": "user",
128
+ "content": f"{i+1}/{len(split_instructions)}: {split_instruction}"
129
+ }
130
+ response = openai.ChatCompletion.create(
131
+ model="gpt-4-turbo-2024-04-09",
132
+ messages=[system_message, user_message],
133
+ temperature=0.7,
134
+ )
135
+ results.append(response.choices[0]["message"]["content"])
136
+
137
+ final_result = "\n".join(results)
138
 
139
  with open("output3.txt", "w", encoding="utf-8") as f:
140
+ f.write(final_result)
141
+ return final_result
142
 
143
  with gr.Blocks() as app:
144
+ with gr.Row():
145
+ with gr.Column():
146
+ editable_output2 = gr.Textbox(label="編集可能な記事成", placeholder="HTML content here...", lines=10)
147
+ generate_button = gr.Button("記事を生成")
148
+ with gr.Column():
149
+ final_article = gr.Textbox(label="最終的な記事本文", lines=20, placeholder="Generated article content will appear here.")
150
+
151
  generate_button.click(
152
  fn=generate_article,
153
  inputs=[editable_output2],
154
  outputs=[final_article]
155
+ )
156
+
157
+ app.launch()