AIRider commited on
Commit
1966659
ยท
verified ยท
1 Parent(s): c5a9055

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -51
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 = 2000 # ๋‚ด๋ถ€์ ์œผ๋กœ ๋ชฉํ‘œ ๊ธ€์ž์ˆ˜ ์„ค์ •
 
134
 
135
  references = fetch_references(query)
136
  ref1, ref2, ref3 = references
137
 
138
- random_seed = random.randint(1, sys.maxsize)
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
- seed=random_seed
 
 
164
  )
165
-
166
- generated_post = response.choices[0].message['content']
167
-
168
- # ์›์น˜ ์•Š๋Š” ํ‘œํ˜„๋“ค์„ ์ •๊ทœํ‘œํ˜„์‹ ํŒจํ„ด์œผ๋กœ ์ •์˜
 
 
 
 
 
 
 
 
 
 
169
  unwanted_patterns = [
170
  r'\b์—ฌ๋Ÿฌ๋ถ„[,.]?\s*',
171
  r'\b(๋งˆ์ง€๋ง‰์œผ๋กœ|๊ฒฐ๋ก ์ ์œผ๋กœ|๊ฒฐ๊ตญ|์ข…ํ•ฉ์ ์œผ๋กœ|๋”ฐ๋ผ์„œ|๋งˆ๋ฌด๋ฆฌ|์š”์•ฝ)[,.]?\s*'
172
  ]
173
 
174
- # ๊ธ€์ž์ˆ˜๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ ์ถ”๊ฐ€ ์ƒ์„ฑ
175
- while len(generated_post) < target_length or any(re.search(pattern, generated_post, re.IGNORECASE) for pattern in unwanted_patterns):
176
- if len(generated_post) < target_length:
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
- messages.append({"role": "assistant", "content": generated_post})
185
- messages.append({"role": "user", "content": additional_message})
186
-
187
- additional_response = openai.ChatCompletion.create(
188
- model="gpt-4o-mini",
189
- messages=messages,
190
- max_tokens=min(10000, max(additional_length * 2, 1000)),
191
- temperature=0.85,
192
- top_p=0.9,
193
- frequency_penalty=0.5,
194
- seed=random_seed
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)