Spaces:
Sleeping
Sleeping
File size: 5,729 Bytes
1fbeb24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import gradio as gr
import openai
import os
import requests
from bs4 import BeautifulSoup
# OpenAI API ํด๋ผ์ด์ธํธ ์ค์
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
raise ValueError("OpenAI API ํ ํฐ(OPENAI_API_KEY)์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
#############################
# OpenAI API ํธ์ถ ํจ์
#############################
def call_openai_api(content: str, system_message: str, max_tokens: int, temperature: float, top_p: float) -> str:
try:
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": content},
],
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
return response.choices[0].message['content']
except Exception as e:
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
#############################
# OpenAI ์ค์
#############################
OPENAI_SYSTEM_MESSAGE = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.
๋๋ ์ต๊ณ ์ ๋น์์ด๋ค.
๋ด๊ฐ ์๊ตฌํ๋ ๊ฒ๋ค์ ์ต๋ํ ์์ธํ๊ณ ์ ํํ๊ฒ ๋ต๋ณํ๋ผ.
##[๊ธฐ๋ณธ๊ท์น]
1. ๋ฐ๋์ ํ๊ตญ์ด(ํ๊ธ)๋ก ์์ฑํ๋ผ.
2. ๋๋ ๊ฐ์ฅ ์ฃผ๋ชฉ๋ฐ๋ ๋ง์ผํฐ์ด๋ฉฐ ๋ธ๋ก๊ทธ ๋ง์ผํ
์ ๋ฌธ๊ฐ์ด๋ค.
3. ํนํ ๋๋ '์ ๋ณด์ฑ(Informative)' ์ ๋ฌธ ๋ธ๋ก๊ทธ ๋ง์ผํ
์ ๋ฌธ๊ฐ์ด๋ค.
4. ์ ๋ณด ์ ๊ณต์ ์ด์ ์ ๋ง์ถ์ด ์์ฑํ๋ค.
##[ํ
์คํธ ์์ฑ ๊ท์น]
1. ์์ฃผ์ ๋ฅผ 5๊ฐ๋ก ๊ตฌ๋ถํ์ฌ 2000์ ์ด์๋๋๋ก ์์ฑํ๋ผ.
2. ์ ์ฒด ๋งฅ๋ฝ์ ์ดํดํ๊ณ ๋ฌธ์ฅ์ ์ผ๊ด์ฑ์ ์ ์งํ๋ผ.
3. ์ ๋๋ก ์ฐธ๊ณ ๊ธ์ ํ๋ฌธ์ฅ ์ด์ ๊ทธ๋๋ก ์ถ๋ ฅํ์ง ๋ง ๊ฒ.
4. ์ฃผ์ ์ ์ํฉ์ ๋ง๋ ์ ์ ํ ์ดํ๋ฅผ ์ ํํ๋ผ.
5. ํ๊ธ ์ดํ์ ๋์ด๋๋ ์ฝ๊ฒ ์์ฑํ๋ผ.
6. ์ ๋ ๋ฌธ์ฅ์ ๋์ '๋ต๋๋ค'๋ฅผ ์ฌ์ฉํ์ง ๋ง ๊ฒ.
###[์ ๋ณด์ฑ ๋ธ๋ก๊ทธ ์์ฑ ๊ท์น]
1. ๋
์๊ฐ ์ป๊ณ ์ ํ๋ ์ ์ฉํ ์ ๋ณด์ ํฅ๋ฏธ๋ก์ด ์ ๋ณด๋ฅผ ์ ๊ณตํ๋๋ก ์์ฑํ๋ผ.
2. ๋
์์ ๊ณต๊ฐ์ ์ด๋์ด๋ด๊ณ ๊ถ๊ธ์ฆ์ ํด๊ฒฐํ๋๋ก ์์ฑํ๋ผ.
3. ๋
์์ ๊ด์ฌ์ฌ๋ฅผ ์ถฉ์กฑ์ํค๋๋ก ์์ฑํ๋ผ.
4. ๋
์์๊ฒ ์ด๋์ด ๋๋ ์ ๋ณด๋ฅผ ์์ฑํ๋ผ.
##[์ ์ธ ๊ท์น]
1. ๋ฐ๋์ ๋น์์ด ๋ฐ ์์ค(expletive, abusive language, slang)์ ์ ์ธํ๋ผ.
2. ๋ฐ๋์ ์ฐธ๊ณ ๊ธ์ ๋งํฌ(URL)๋ ์ ์ธํ๋ผ.
3. ์ฐธ๊ณ ๊ธ์์ '๋งํฌ๋ฅผ ํ์ธํด์ฃผ์ธ์'์ ๊ฐ์ ๋งํฌ ์ด๋์ ๋ฌธ๊ตฌ๋ ์ ์ธํ๋ผ.
4. ์ฐธ๊ณ ๊ธ์ ์๋ ์์ฑ์, ํ์, ์ ํ๋ฒ, ๊ธฐ์์ ์ด๋ฆ, ์ ์นญ, ๋๋ค์์ ๋ฐ๋์ ์ ์ธํ๋ผ.
5. ๋ฐ๋์ ๋ฌธ์ฅ์ ๋๋ถ๋ถ์ด ์ด์ํ ํ๊ตญ์ด ํํ์ ์ ์ธํ๋ผ('์์', '๋ต๋๋ค', 'ํด์', 'ํด์ฃผ์ฃ ', '๋์ฃ ', '๋์ด์', '๊ณ ์' ๋ฑ.)
"""
OPENAI_MAX_TOKENS = 4000
OPENAI_TEMPERATURE = 0.7
OPENAI_TOP_P = 0.95
#############################
# ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํ
#############################
def convert_to_mobile_url(url):
if "m.blog.naver.com" not in url:
if "blog.naver.com" in url:
url_parts = url.split("/")
if len(url_parts) >= 5:
user_id = url_parts[3]
post_id = url_parts[4]
return f"https://m.blog.naver.com/{user_id}/{post_id}"
return url
def scrape_naver_blog(url):
try:
mobile_url = convert_to_mobile_url(url)
response = requests.get(mobile_url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
title_element = soup.find("div", class_="se-module se-module-text se-title-text")
title = title_element.get_text(strip=True) if title_element else "์ ๋ชฉ์ ์ฐพ์ ์ ์์"
content_elements = soup.find_all("div", class_="se-module se-module-text")
content = "\n".join(
elem.get_text(strip=True) for elem in content_elements
) if content_elements else "๋ด์ฉ์ ์ฐพ์ ์ ์์"
return f"์ ๋ชฉ: {title}\n\n๋ด์ฉ: {content}"
except Exception as e:
return f"Error: {e}"
#############################
# Gradio UI ๊ตฌ์ฑ (ํญ ๊ตฌ์กฐ ์ ์ฉ)
#############################
with gr.Blocks() as demo:
gr.Markdown("# ๋ค๊ธฐ๋ฅ ๋๊ตฌ")
with gr.Tabs():
with gr.Tab("๋ธ๋ก๊ทธ ์์ฑ๊ธฐ"):
tone_radio = gr.Radio(
label="๋งํฌ๋ฐ๊พธ๊ธฐ",
choices=["์น๊ทผํ๊ฒ", "์ผ๋ฐ์ ์ธ", "์ ๋ฌธ์ ์ธ"],
value="์ผ๋ฐ์ ์ธ"
)
ref1 = gr.Textbox(label="์ฐธ์กฐ๊ธ 1")
ref2 = gr.Textbox(label="์ฐธ์กฐ๊ธ 2")
ref3 = gr.Textbox(label="์ฐธ์กฐ๊ธ 3")
output_box = gr.Textbox(label="๊ฒฐ๊ณผ", lines=20, interactive=False)
generate_button = gr.Button("์์ฑํ๊ธฐ")
generate_button.click(
fn=lambda t, r1, r2, r3: call_openai_api(
f"๋งํฌ: {t}\n์ฐธ์กฐ๊ธ1: {r1}\n์ฐธ์กฐ๊ธ2: {r2}\n์ฐธ์กฐ๊ธ3: {r3}\n",
OPENAI_SYSTEM_MESSAGE, OPENAI_MAX_TOKENS, OPENAI_TEMPERATURE, OPENAI_TOP_P
),
inputs=[tone_radio, ref1, ref2, ref3],
outputs=output_box
)
with gr.Tab("๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํ"):
blog_url = gr.Textbox(label="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL")
scrape_output = gr.Textbox(label="์คํฌ๋ํ ๊ฒฐ๊ณผ", lines=10, interactive=False)
scrape_button = gr.Button("์คํฌ๋ํํ๊ธฐ")
scrape_button.click(
fn=scrape_naver_blog,
inputs=blog_url,
outputs=scrape_output
)
if __name__ == "__main__":
demo.launch()
|