Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,10 @@ import gradio as gr
|
|
12 |
from fpdf import FPDF as FPDF2
|
13 |
from datetime import datetime
|
14 |
from zoneinfo import ZoneInfo
|
|
|
|
|
|
|
|
|
15 |
import sys
|
16 |
|
17 |
# API ํค ์ค์
|
@@ -130,71 +134,57 @@ def fetch_crawl_results(query):
|
|
130 |
|
131 |
def generate_blog_post(query, prompt_template):
|
132 |
try:
|
133 |
-
target_length =
|
|
|
134 |
|
135 |
references = fetch_references(query)
|
136 |
ref1, ref2, ref3 = references
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
user_message = f"""์ฃผ์ : {query}
|
141 |
-
|
142 |
-
์ฐธ๊ณ ๊ธ1:
|
143 |
-
{ref1}
|
144 |
-
|
145 |
-
์ฐธ๊ณ ๊ธ2:
|
146 |
-
{ref2}
|
147 |
-
|
148 |
-
์ฐธ๊ณ ๊ธ3:
|
149 |
-
{ref3}
|
150 |
-
"""
|
151 |
-
messages = [
|
152 |
-
{"role": "system", "content": prompt_template},
|
153 |
-
{"role": "user", "content": user_message}
|
154 |
-
]
|
155 |
-
|
156 |
-
response = openai.ChatCompletion.create(
|
157 |
-
model="gpt-4o-mini",
|
158 |
-
messages=messages,
|
159 |
-
max_tokens=10000,
|
160 |
temperature=0.85,
|
|
|
161 |
top_p=0.9,
|
162 |
frequency_penalty=0.5,
|
163 |
-
|
|
|
|
|
164 |
)
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
unwanted_patterns = [
|
170 |
r'\b์ฌ๋ฌ๋ถ[,.]?\s*',
|
171 |
r'\b(๋ง์ง๋ง์ผ๋ก|๊ฒฐ๋ก ์ ์ผ๋ก|๊ฒฐ๊ตญ|์ข
ํฉ์ ์ผ๋ก|๋ฐ๋ผ์|๋ง๋ฌด๋ฆฌ|์์ฝ)[,.]?\s*'
|
172 |
]
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
additional_length = target_length - len(generated_post)
|
178 |
-
reason = f"ํ์ฌ ๊ธ์์๋ {len(generated_post)}์ ์ด๋ค. ์ฝ {additional_length}์๋ฅผ ์ถ๊ฐ๋ก ์์ฑํ์ฌ ์ด {target_length}์ ์ด์์ด ๋๋๋ก ์์ฑํ๋ผ"
|
179 |
-
else:
|
180 |
-
reason = "์์น ์๋ ํํ์ด ํฌํจ๋์ด ์์ต๋๋ค. ํด๋น ํํ์ ์ ๊ฑฐํ๊ณ ์์ฐ์ค๋ฝ๊ฒ ๊ธ์ ๋ค์ ์์ฑํด์ฃผ์ธ์."
|
181 |
-
|
182 |
-
additional_message = f"{reason} ์ด์ ๋ด์ฉ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ฐ๊ฒฐ๋๋๋ก ์์ฑํด์ฃผ์ธ์. ์์ ์ธ๊ธํ ํํ๋ค(์ฌ๋ฌ๋ถ, ๋ง์ง๋ง์ผ๋ก, ๊ฒฐ๋ก ์ ์ผ๋ก, ๊ฒฐ๊ตญ, ์ข
ํฉ์ ์ผ๋ก, ๋ฐ๋ผ์, ๋ง๋ฌด๋ฆฌ, ์์ฝ)์ ์ฌ์ฉํ์ง ๋ง์ธ์."
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
generated_post = additional_response.choices[0].message['content']
|
198 |
|
199 |
final_post = f"์ฃผ์ : {query}\n\n{generated_post}"
|
200 |
actual_length = len(generated_post)
|
|
|
12 |
from fpdf import FPDF as FPDF2
|
13 |
from datetime import datetime
|
14 |
from zoneinfo import ZoneInfo
|
15 |
+
from langchain.chat_models import ChatOpenAI
|
16 |
+
from langchain.prompts import ChatPromptTemplate
|
17 |
+
from langchain.chains import LLMChain
|
18 |
+
from langchain.callbacks import get_openai_callback
|
19 |
import sys
|
20 |
|
21 |
# API ํค ์ค์
|
|
|
134 |
|
135 |
def generate_blog_post(query, prompt_template):
|
136 |
try:
|
137 |
+
target_length = 1500 # ๋ด๋ถ์ ์ผ๋ก ๋ชฉํ ๊ธ์์ ์ค์
|
138 |
+
max_attempts = 2 # ์ต๋ 2๋ฒ ์คํ (์ด๊ธฐ 1๋ฒ + ์ฌ์๋ 1๋ฒ)
|
139 |
|
140 |
references = fetch_references(query)
|
141 |
ref1, ref2, ref3 = references
|
142 |
|
143 |
+
chat = ChatOpenAI(
|
144 |
+
model_name="gpt-4o-mini",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
temperature=0.85,
|
146 |
+
max_tokens=10000,
|
147 |
top_p=0.9,
|
148 |
frequency_penalty=0.5,
|
149 |
+
presence_penalty=0,
|
150 |
+
n=1,
|
151 |
+
request_timeout=60
|
152 |
)
|
153 |
+
|
154 |
+
prompt = ChatPromptTemplate.from_template(
|
155 |
+
prompt_template + """
|
156 |
+
์ฃผ์ : {query}
|
157 |
+
์ฐธ๊ณ ๊ธ1: {ref1}
|
158 |
+
์ฐธ๊ณ ๊ธ2: {ref2}
|
159 |
+
์ฐธ๊ณ ๊ธ3: {ref3}
|
160 |
+
๋ค์ ํํ์ ์ฌ์ฉํ์ง ๋ง์ธ์: ์ฌ๋ฌ๋ถ, ๋ง์ง๋ง์ผ๋ก, ๊ฒฐ๋ก ์ ์ผ๋ก, ๊ฒฐ๊ตญ, ์ข
ํฉ์ ์ผ๋ก, ๋ฐ๋ผ์, ๋ง๋ฌด๋ฆฌ, ์์ฝ
|
161 |
+
์ฝ {target_length}์๋ก ์์ฑํด์ฃผ์ธ์.
|
162 |
+
"""
|
163 |
+
)
|
164 |
+
|
165 |
+
chain = LLMChain(llm=chat, prompt=prompt)
|
166 |
+
|
167 |
unwanted_patterns = [
|
168 |
r'\b์ฌ๋ฌ๋ถ[,.]?\s*',
|
169 |
r'\b(๋ง์ง๋ง์ผ๋ก|๊ฒฐ๋ก ์ ์ผ๋ก|๊ฒฐ๊ตญ|์ข
ํฉ์ ์ผ๋ก|๋ฐ๋ผ์|๋ง๋ฌด๋ฆฌ|์์ฝ)[,.]?\s*'
|
170 |
]
|
171 |
|
172 |
+
for attempt in range(max_attempts):
|
173 |
+
with get_openai_callback() as cb:
|
174 |
+
result = chain.run(query=query, ref1=ref1, ref2=ref2, ref3=ref3, target_length=target_length)
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
+
generated_post = result.strip()
|
177 |
+
|
178 |
+
# ๋ชฉํ ๊ธ์์๋ฅผ ์ถฉ์กฑํ๊ณ ์์น ์๋ ํํ์ด ์์ผ๋ฉด ๋ฃจํ ์ข
๋ฃ
|
179 |
+
if len(generated_post) >= target_length and not any(re.search(pattern, generated_post, re.IGNORECASE) for pattern in unwanted_patterns):
|
180 |
+
break
|
181 |
+
|
182 |
+
# ์ฒซ ๋ฒ์งธ ์๋ ํ ์ฌ์๋ ์ ํ๋กฌํํธ ์์
|
183 |
+
if attempt == 0:
|
184 |
+
if len(generated_post) < target_length:
|
185 |
+
prompt.template += f"\n\nํ์ฌ ๊ธ์์๋ {len(generated_post)}์์
๋๋ค. ์ฝ {target_length - len(generated_post)}์๋ฅผ ์ถ๊ฐ๋ก ์์ฑํ์ฌ ์ด {target_length}์๊ฐ ๋๋๋ก ํด์ฃผ์ธ์."
|
186 |
+
if any(re.search(pattern, generated_post, re.IGNORECASE) for pattern in unwanted_patterns):
|
187 |
+
prompt.template += "\n\n์๏ฟฝ๏ฟฝ ์๋ ํํ์ด ํฌํจ๋์ด ์์ต๋๋ค. ํด๋น ํํ์ ์ ๊ฑฐํ๊ณ ์์ฐ์ค๋ฝ๊ฒ ๊ธ์ ๋ค์ ์์ฑํด์ฃผ์ธ์."
|
|
|
|
|
188 |
|
189 |
final_post = f"์ฃผ์ : {query}\n\n{generated_post}"
|
190 |
actual_length = len(generated_post)
|