Spaces:
Sleeping
Sleeping
Update article_generator.py
Browse files- article_generator.py +55 -5
article_generator.py
CHANGED
|
@@ -63,7 +63,7 @@ def save_state(state):
|
|
| 63 |
# ็ถๆ
ใใญใผใใใ้ขๆฐ
|
| 64 |
def load_state():
|
| 65 |
if os.path.exists(state_file):
|
| 66 |
-
with open(state_file, "r", encoding="utf-8") as f:
|
| 67 |
return json.load(f)
|
| 68 |
return None
|
| 69 |
|
|
@@ -125,6 +125,53 @@ def setup_plan_and_execute_agent():
|
|
| 125 |
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
|
| 126 |
return agent
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# ่จไบใ็ๆใใ้ขๆฐ
|
| 129 |
def generate_article(editable_output2):
|
| 130 |
# ้ไธญใใๅ้ใใๅ ดๅใฎใใใซ็ถๆ
ใ่ชญใฟ่พผใฟ
|
|
@@ -237,16 +284,19 @@ def generate_article(editable_output2):
|
|
| 237 |
|
| 238 |
final_result = "\n".join(results)
|
| 239 |
|
|
|
|
|
|
|
|
|
|
| 240 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 241 |
-
f.write(
|
| 242 |
|
| 243 |
-
print(
|
| 244 |
|
| 245 |
# ็ๆใๅฎไบใใใ็ถๆ
ใใกใคใซใๅ้ค
|
| 246 |
if os.path.exists("state.json"):
|
| 247 |
os.remove("state.json")
|
| 248 |
|
| 249 |
-
return
|
| 250 |
|
| 251 |
def continue_generate_article():
|
| 252 |
state = load_state()
|
|
@@ -301,4 +351,4 @@ def continue_generate_article():
|
|
| 301 |
if os.path.exists("state.json"):
|
| 302 |
os.remove("state.json")
|
| 303 |
|
| 304 |
-
return final_result
|
|
|
|
| 63 |
# ็ถๆ
ใใญใผใใใ้ขๆฐ
|
| 64 |
def load_state():
|
| 65 |
if os.path.exists(state_file):
|
| 66 |
+
with open(state_file, "r", encoding="utf-8") as f):
|
| 67 |
return json.load(f)
|
| 68 |
return None
|
| 69 |
|
|
|
|
| 125 |
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
|
| 126 |
return agent
|
| 127 |
|
| 128 |
+
# ่จไบใฎใปใฏใทใงใณใGPT-4ใงๆกๅผตใใ้ขๆฐ
|
| 129 |
+
def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
|
| 130 |
+
prompts = []
|
| 131 |
+
for h3_text in h3_texts:
|
| 132 |
+
key = f"{h2_text} {h3_text}"
|
| 133 |
+
if key in preloaded_data:
|
| 134 |
+
context = preloaded_data[key]
|
| 135 |
+
prompt = f"ใใฎใใผใใ{h3_text}ใใซใคใใฆใใใใซ่ฉณ็ดฐใชๆ
ๅ ฑใๅ ใใฆใใ ใใใไปฅไธใฏๅๆใฎๆ
ๅ ฑใงใ๏ผ\n{context}"
|
| 136 |
+
prompts.append(prompt)
|
| 137 |
+
else:
|
| 138 |
+
prompts.append(f"ใใฎใใผใใ{h3_text}ใใซ้ขใใ่ฉณ็ดฐๆ
ๅ ฑใๅ ใใฆใใ ใใใ")
|
| 139 |
+
|
| 140 |
+
expanded_texts = []
|
| 141 |
+
for prompt in prompts:
|
| 142 |
+
try:
|
| 143 |
+
response = openai.Completion.create(
|
| 144 |
+
model="gpt-4-turbo",
|
| 145 |
+
prompt=prompt,
|
| 146 |
+
max_tokens=2000,
|
| 147 |
+
temperature=0.7
|
| 148 |
+
)
|
| 149 |
+
expanded_text = response.choices[0].text.strip()
|
| 150 |
+
expanded_texts.append(expanded_text)
|
| 151 |
+
except Exception as e:
|
| 152 |
+
print(f"Error in generating text for {prompt}: {str(e)}")
|
| 153 |
+
expanded_texts.append("Error in text generation.")
|
| 154 |
+
|
| 155 |
+
return expanded_texts
|
| 156 |
+
|
| 157 |
+
# ่จไบใๆกๅผตใใ้ขๆฐ
|
| 158 |
+
def generate_expanded_article(article_html):
|
| 159 |
+
soup = BeautifulSoup(article_html, 'html.parser')
|
| 160 |
+
h2_elements = soup.find_all('h2')
|
| 161 |
+
preloaded_data = load_preloaded_tavily_data()
|
| 162 |
+
|
| 163 |
+
for h2 in h2_elements:
|
| 164 |
+
h3_texts = [h3.get_text() for h3 in h2.find_next_siblings('h3', limit=2)]
|
| 165 |
+
expanded_texts = expand_section_with_gpt4(h2.get_text(), h3_texts, preloaded_data)
|
| 166 |
+
|
| 167 |
+
for h3, expanded_text in zip(h3_texts, expanded_texts):
|
| 168 |
+
h3_tag = soup.find('h3', text=h3)
|
| 169 |
+
new_paragraph = soup.new_tag('p')
|
| 170 |
+
new_paragraph.string = expanded_text
|
| 171 |
+
h3_tag.insert_after(new_paragraph)
|
| 172 |
+
|
| 173 |
+
return str(soup)
|
| 174 |
+
|
| 175 |
# ่จไบใ็ๆใใ้ขๆฐ
|
| 176 |
def generate_article(editable_output2):
|
| 177 |
# ้ไธญใใๅ้ใใๅ ดๅใฎใใใซ็ถๆ
ใ่ชญใฟ่พผใฟ
|
|
|
|
| 284 |
|
| 285 |
final_result = "\n".join(results)
|
| 286 |
|
| 287 |
+
# ็ๆใใใๅๆ่จไบใๆกๅผต
|
| 288 |
+
expanded_article = generate_expanded_article(final_result)
|
| 289 |
+
|
| 290 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 291 |
+
f.write(expanded_article)
|
| 292 |
|
| 293 |
+
print(expanded_article) # ใญใฐใซๆ็ต็ตๆใๅบๅ
|
| 294 |
|
| 295 |
# ็ๆใๅฎไบใใใ็ถๆ
ใใกใคใซใๅ้ค
|
| 296 |
if os.path.exists("state.json"):
|
| 297 |
os.remove("state.json")
|
| 298 |
|
| 299 |
+
return expanded_article
|
| 300 |
|
| 301 |
def continue_generate_article():
|
| 302 |
state = load_state()
|
|
|
|
| 351 |
if os.path.exists("state.json"):
|
| 352 |
os.remove("state.json")
|
| 353 |
|
| 354 |
+
return final_result
|