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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from typing import Optional
|
| 5 |
+
import requests
|
| 6 |
+
from bs4 import BeautifulSoup
|
| 7 |
+
|
| 8 |
+
#############################
|
| 9 |
+
|
| 10 |
+
# OpenAI API ํด๋ผ์ด์ธํธ ์ค์
|
| 11 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
if not openai.api_key:
|
| 13 |
+
raise ValueError("OpenAI API ํ ํฐ(OPENAI_API_KEY)์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 14 |
+
|
| 15 |
+
def call_openai_api(
|
| 16 |
+
content: str,
|
| 17 |
+
system_message: str,
|
| 18 |
+
max_tokens: int,
|
| 19 |
+
temperature: float,
|
| 20 |
+
top_p: float
|
| 21 |
+
) -> str:
|
| 22 |
+
"""
|
| 23 |
+
OpenAI์ GPT-4o-mini ๋ชจ๋ธ์ ์ด์ฉํด ํ ๋ฒ์ ์ง๋ฌธ(content)์ ๋ํ ๋ต๋ณ์ ๋ฐํํ๋ ํจ์.
|
| 24 |
+
"""
|
| 25 |
+
try:
|
| 26 |
+
response = openai.ChatCompletion.create(
|
| 27 |
+
model="gpt-4o-mini",
|
| 28 |
+
messages=[
|
| 29 |
+
{"role": "system", "content": system_message},
|
| 30 |
+
{"role": "user", "content": content},
|
| 31 |
+
],
|
| 32 |
+
max_tokens=max_tokens,
|
| 33 |
+
temperature=temperature,
|
| 34 |
+
top_p=top_p,
|
| 35 |
+
)
|
| 36 |
+
assistant_message = response.choices[0].message['content']
|
| 37 |
+
return assistant_message
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 40 |
+
|
| 41 |
+
#############################
|
| 42 |
+
# ๊ณ ๊ธ ์ค์ (OpenAI) - ์ฝ๋์์๋ง ์ ์ (UI์ ๋
ธ์ถ ๊ธ์ง)
|
| 43 |
+
#############################
|
| 44 |
+
|
| 45 |
+
OPENAI_SYSTEM_MESSAGE = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.
|
| 46 |
+
๋๋ ์ต๊ณ ์ ๋น์์ด๋ค.
|
| 47 |
+
๋ด๊ฐ ์๊ตฌํ๋ ๊ฒ๋ค์ ์ต๋ํ ์์ธํ๊ณ ์ ํํ๊ฒ ๋ต๋ณํ๋ผ.
|
| 48 |
+
##[๊ธฐ๋ณธ๊ท์น]
|
| 49 |
+
1. ๋ฐ๋์ ํ๊ตญ์ด(ํ๊ธ)๋ก ์์ฑํ๋ผ.
|
| 50 |
+
2. ๋๋ ๊ฐ์ฅ ์ฃผ๋ชฉ๋ฐ๋ ๋ง์ผํฐ์ด๋ฉฐ ๋ธ๋ก๊ทธ ๋ง์ผํ
์ ๋ฌธ๊ฐ์ด๋ค.
|
| 51 |
+
3. ํนํ ๋๋ '์ ๋ณด์ฑ(Informative)' ์ ๋ฌธ ๋ธ๋ก๊ทธ ๋ง์ผํ
์ ๋ฌธ๊ฐ์ด๋ค.
|
| 52 |
+
4. ์ ๋ณด ์ ๊ณต์ ์ด์ ์ ๋ง์ถ์ด ์์ฑํ๋ค.
|
| 53 |
+
##[ํ
์คํธ ์์ฑ ๊ท์น]
|
| 54 |
+
1. ์์ฃผ์ ๋ฅผ 5๊ฐ๋ก ๊ตฌ๋ถํ์ฌ 2000์ ์ด์๋๋๋ก ์์ฑํ๋ผ.
|
| 55 |
+
2. ์ ์ฒด ๋งฅ๋ฝ์ ์ดํดํ๊ณ ๋ฌธ์ฅ์ ์ผ๊ด์ฑ์ ์ ์งํ๋ผ.
|
| 56 |
+
3. ์ ๋๋ก ์ฐธ๊ณ ๊ธ์ ํ๋ฌธ์ฅ ์ด์ ๊ทธ๋๋ก ์ถ๋ ฅํ์ง ๋ง ๊ฒ.
|
| 57 |
+
4. ์ฃผ์ ์ ์ํฉ์ ๋ง๋ ์ ์ ํ ์ดํ๋ฅผ ์ ํํ๋ผ.
|
| 58 |
+
5. ํ๊ธ ์ดํ์ ๋์ด๋๋ ์ฝ๊ฒ ์์ฑํ๋ผ.
|
| 59 |
+
6. ์ ๋ ๋ฌธ์ฅ์ ๋์ '๋ต๋๋ค'๋ฅผ ์ฌ์ฉํ์ง ๋ง ๊ฒ.
|
| 60 |
+
###[์ ๋ณด์ฑ ๋ธ๋ก๊ทธ ์์ฑ ๊ท์น]
|
| 61 |
+
1. ๋
์๊ฐ ์ป๊ณ ์ ํ๋ ์ ์ฉํ ์ ๋ณด์ ํฅ๋ฏธ๋ก์ด ์ ๋ณด๋ฅผ ์ ๊ณตํ๋๋ก ์์ฑํ๋ผ.
|
| 62 |
+
2. ๋
์์ ๊ณต๊ฐ์ ์ด๋์ด๋ด๊ณ ๊ถ๊ธ์ฆ์ ํด๊ฒฐํ๋๋ก ์์ฑํ๋ผ.
|
| 63 |
+
3. ๋
์์ ๊ด์ฌ์ฌ๋ฅผ ์ถฉ์กฑ์ํค๋๋ก ์์ฑํ๋ผ.
|
| 64 |
+
4. ๋
์์๊ฒ ์ด๋์ด ๋๋ ์ ๋ณด๋ฅผ ์์ฑํ๋ผ.
|
| 65 |
+
##[์ ์ธ ๊ท์น]
|
| 66 |
+
1. ๋ฐ๋์ ๋น์์ด ๋ฐ ์์ค(expletive, abusive language, slang)์ ์ ์ธํ๋ผ.
|
| 67 |
+
2. ๋ฐ๋์ ์ฐธ๊ณ ๊ธ์ ๋งํฌ(URL)๋ ์ ์ธํ๋ผ.
|
| 68 |
+
3. ์ฐธ๊ณ ๊ธ์์ '๋งํฌ๋ฅผ ํ์ธํด์ฃผ์ธ์'์ ๊ฐ์ ๋งํฌ ์ด๋์ ๋ฌธ๊ตฌ๋ ์ ์ธํ๋ผ.
|
| 69 |
+
4. ์ฐธ๊ณ ๊ธ์ ์๋ ์์ฑ์, ํ์, ์ ํ๋ฒ, ๊ธฐ์์ ์ด๋ฆ, ์ ์นญ, ๋๋ค์์ ๋ฐ๋์ ์ ์ธํ๋ผ.
|
| 70 |
+
5. ๋ฐ๋์ ๋ฌธ์ฅ์ ๋๋ถ๋ถ์ด ์ด์ํ ํ๊ตญ์ด ํํ์ ์ ์ธํ๋ผ('์์', '๋ต๋๋ค', 'ํด์', 'ํด์ฃผ์ฃ ', '๋์ฃ ', '๋์ด์', '๊ณ ์' ๋ฑ.)
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
OPENAI_MAX_TOKENS = 4000
|
| 74 |
+
OPENAI_TEMPERATURE = 0.7
|
| 75 |
+
OPENAI_TOP_P = 0.95
|
| 76 |
+
|
| 77 |
+
#############################
|
| 78 |
+
# UI - ๋ธ๋ก๊ทธ ์์ฑ๊ธฐ
|
| 79 |
+
#############################
|
| 80 |
+
def blog_generator():
|
| 81 |
+
with gr.Blocks() as blog_tab:
|
| 82 |
+
gr.Markdown("# ๋ธ๋ก๊ทธ ์์ฑ๊ธฐ")
|
| 83 |
+
|
| 84 |
+
# ๋งํฌ๋ฐ๊พธ๊ธฐ (๋ผ๋์ค ๋ฒํผ)
|
| 85 |
+
tone_radio = gr.Radio(
|
| 86 |
+
label="๋งํฌ๋ฐ๊พธ๊ธฐ",
|
| 87 |
+
choices=["์น๊ทผํ๊ฒ", "์ผ๋ฐ์ ์ธ", "์ ๋ฌธ์ ์ธ"],
|
| 88 |
+
value="์ผ๋ฐ์ ์ธ" # ๊ธฐ๋ณธ ์ ํ
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# ์ฐธ์กฐ๊ธ ์
๋ ฅ (3๊ฐ)
|
| 92 |
+
ref1 = gr.Textbox(label="์ฐธ์กฐ๊ธ 1")
|
| 93 |
+
ref2 = gr.Textbox(label="์ฐธ์กฐ๊ธ 2")
|
| 94 |
+
ref3 = gr.Textbox(label="์ฐธ์กฐ๊ธ 3")
|
| 95 |
+
|
| 96 |
+
output_box = gr.Textbox(label="๊ฒฐ๊ณผ", lines=20, interactive=False)
|
| 97 |
+
|
| 98 |
+
def generate_blog(tone_value: str, ref1_value: str, ref2_value: str, ref3_value: str) -> str:
|
| 99 |
+
# ํ๋กฌํํธ ์์ฑ
|
| 100 |
+
question = (
|
| 101 |
+
f"๋งํฌ: {tone_value}\n"
|
| 102 |
+
f"์ฐธ์กฐ๊ธ1: {ref1_value}\n"
|
| 103 |
+
f"์ฐธ์กฐ๊ธ2: {ref2_value}\n"
|
| 104 |
+
f"์ฐธ์กฐ๊ธ3: {ref3_value}\n"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# OpenAI GPT-4o-mini ๋ชจ๋ธ ํธ์ถ
|
| 108 |
+
response = call_openai_api(
|
| 109 |
+
content=question,
|
| 110 |
+
system_message=OPENAI_SYSTEM_MESSAGE,
|
| 111 |
+
max_tokens=OPENAI_MAX_TOKENS,
|
| 112 |
+
temperature=OPENAI_TEMPERATURE,
|
| 113 |
+
top_p=OPENAI_TOP_P
|
| 114 |
+
)
|
| 115 |
+
return response
|
| 116 |
+
|
| 117 |
+
generate_button = gr.Button("์์ฑํ๊ธฐ")
|
| 118 |
+
generate_button.click(
|
| 119 |
+
fn=generate_blog,
|
| 120 |
+
inputs=[tone_radio, ref1, ref2, ref3],
|
| 121 |
+
outputs=output_box
|
| 122 |
+
)
|
| 123 |
+
return blog_tab
|
| 124 |
+
|
| 125 |
+
#############################
|
| 126 |
+
# ์ถ๊ฐ ๊ธฐ๋ฅ - ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํ
|
| 127 |
+
#############################
|
| 128 |
+
|
| 129 |
+
def convert_to_mobile_url(url):
|
| 130 |
+
"""
|
| 131 |
+
PC URL์ ๋ชจ๋ฐ์ผ URL๋ก ๋ณํ.
|
| 132 |
+
"""
|
| 133 |
+
if "m.blog.naver.com" not in url:
|
| 134 |
+
if "blog.naver.com" in url:
|
| 135 |
+
url_parts = url.split("/")
|
| 136 |
+
if len(url_parts) >= 5:
|
| 137 |
+
user_id = url_parts[3]
|
| 138 |
+
post_id = url_parts[4]
|
| 139 |
+
return f"https://m.blog.naver.com/{user_id}/{post_id}"
|
| 140 |
+
return url
|
| 141 |
+
|
| 142 |
+
def scrape_naver_blog(url):
|
| 143 |
+
"""
|
| 144 |
+
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ์ ์ ๋ชฉ๊ณผ ๋ด์ฉ(ํ
์คํธ๋ง) ์คํฌ๋ํ.
|
| 145 |
+
"""
|
| 146 |
+
try:
|
| 147 |
+
# ๋ชจ๋ฐ์ผ URL ๋ณํ
|
| 148 |
+
mobile_url = convert_to_mobile_url(url)
|
| 149 |
+
print(f"Converted Mobile URL: {mobile_url}")
|
| 150 |
+
|
| 151 |
+
response = requests.get(mobile_url)
|
| 152 |
+
response.raise_for_status()
|
| 153 |
+
|
| 154 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 155 |
+
|
| 156 |
+
# ์ ๋ชฉ ์คํฌ๋ํ
|
| 157 |
+
title_element = soup.find("div", class_="se-module se-module-text se-title-text")
|
| 158 |
+
title = title_element.get_text(strip=True) if title_element else "์ ๋ชฉ์ ์ฐพ์ ์ ์์"
|
| 159 |
+
|
| 160 |
+
# ๋ณธ๋ฌธ ๋ด์ฉ ์คํฌ๋ํ
|
| 161 |
+
content_elements = soup.find_all("div", class_="se-module se-module-text")
|
| 162 |
+
content = "\n".join(
|
| 163 |
+
elem.get_text(strip=True) for elem in content_elements
|
| 164 |
+
) if content_elements else "๋ด์ฉ์ ์ฐพ์ ์ ์์"
|
| 165 |
+
|
| 166 |
+
# ๋๋ฒ๊น
๋ฉ์์ง ์ถ๋ ฅ
|
| 167 |
+
print(f"Scraped Title: {title}")
|
| 168 |
+
print(f"Scraped Content: {content}")
|
| 169 |
+
|
| 170 |
+
# ๊ฒฐ๊ณผ ๋ฐํ
|
| 171 |
+
result = f"์ ๋ชฉ: {title}\n\n๋ด์ฉ: {content}"
|
| 172 |
+
return result
|
| 173 |
+
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f"Error: {e}")
|
| 176 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 177 |
+
|
| 178 |
+
# Gradio ์ธํฐํ์ด์ค ์ ์
|
| 179 |
+
def run_scraper(url):
|
| 180 |
+
return scrape_naver_blog(url)
|
| 181 |
+
|
| 182 |
+
def naver_blog_scraper():
|
| 183 |
+
with gr.Blocks() as scraper_tab:
|
| 184 |
+
gr.Markdown("# ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํ")
|
| 185 |
+
|
| 186 |
+
url_input = gr.Textbox(label="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL")
|
| 187 |
+
output_box = gr.Textbox(label="์คํฌ๋ํ ๊ฒฐ๊ณผ", lines=20, interactive=False)
|
| 188 |
+
scrape_button = gr.Button("์คํฌ๋ํํ๊ธฐ")
|
| 189 |
+
|
| 190 |
+
scrape_button.click(
|
| 191 |
+
fn=run_scraper,
|
| 192 |
+
inputs=url_input,
|
| 193 |
+
outputs=output_box
|
| 194 |
+
)
|
| 195 |
+
return scraper_tab
|
| 196 |
+
|
| 197 |
+
#############################
|
| 198 |
+
# ๋ฉ์ธ UI - ํญ ๊ตฌ์ฑ
|
| 199 |
+
#############################
|
| 200 |
+
with gr.Blocks() as demo:
|
| 201 |
+
gr.Markdown("# ํตํฉ Gradio ์ ํ๋ฆฌ์ผ์ด์
")
|
| 202 |
+
|
| 203 |
+
with gr.Tab("๋ธ๋ก๊ทธ ์์ฑ๊ธฐ"):
|
| 204 |
+
blog_tab = blog_generator()
|
| 205 |
+
|
| 206 |
+
with gr.Tab("๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํ"):
|
| 207 |
+
scraper_tab = naver_blog_scraper()
|
| 208 |
+
|
| 209 |
+
if __name__ == "__main__":
|
| 210 |
+
demo.launch()
|