Spaces:
Runtime error
Runtime error
File size: 7,896 Bytes
4e3761c c777df6 4e3761c d5ad31b 0acb494 30a39b4 0acb494 4e3761c d5ad31b 4e3761c 121b238 4e3761c d5ad31b 0acb494 d5ad31b 30a39b4 d5ad31b 30a39b4 0acb494 30a39b4 0acb494 30a39b4 d5ad31b 30a39b4 d5ad31b 30a39b4 0acb494 d5ad31b 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 0acb494 30a39b4 4e3761c |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
import gradio as gr
import openai
import os
from typing import Optional
from gradio_client import Client
#############################
# ๊ธฐ๋ณธ์ฝ๋
#############################
# OpenAI API ํด๋ผ์ด์ธํธ ์ค์
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
raise ValueError("OpenAI API ํ ํฐ(OPENAI_API_KEY)์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
def call_openai_api(
content: str,
system_message: str,
max_tokens: int,
temperature: float,
top_p: float
) -> str:
"""
OpenAI์ GPT-4o-mini ๋ชจ๋ธ์ ์ด์ฉํด ํ ๋ฒ์ ์ง๋ฌธ(content)์ ๋ํ ๋ต๋ณ์ ๋ฐํํ๋ ํจ์.
"""
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,
)
assistant_message = response.choices[0].message['content']
return assistant_message
except Exception as e:
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
#############################
# ๊ณ ๊ธ ์ค์ (UI์ ๋
ธ์ถ๋์ง ์์)
#############################
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
#############################
# API ํด๋ผ์ด์ธํธ ์ค์ (ํ๊น
ํ์ด์ค ์คํ์ด์ค)
#############################
client = Client("Kims12/blog")
#############################
# UI - ๋ธ๋ก๊ทธ ์์ฑ๊ธฐ (๊ธฐ๋ณธ์ฝ๋ ๊ทธ๋๋ก)
#############################
with gr.Blocks() as demo:
gr.Markdown("# ๋ธ๋ก๊ทธ ์์ฑ๊ธฐ")
# ๋งํฌ๋ฐ๊พธ๊ธฐ (๋ผ๋์ค ๋ฒํผ)
tone_radio = gr.Radio(
label="๋งํฌ๋ฐ๊พธ๊ธฐ",
choices=["์น๊ทผํ๊ฒ", "์ผ๋ฐ์ ์ธ", "์ ๋ฌธ์ ์ธ"],
value="์ผ๋ฐ์ ์ธ" # ๊ธฐ๋ณธ ์ ํ
)
# ์ฐธ์กฐ๊ธ ์
๋ ฅ (3๊ฐ)์ ๊ฐ API ์คํ ๋ฒํผ
with gr.Row():
with gr.Column():
ref1 = gr.Textbox(label="์ฐธ์กฐ๊ธ 1")
fetch_button1 = gr.Button("API ์คํ1")
with gr.Column():
ref2 = gr.Textbox(label="์ฐธ์กฐ๊ธ 2")
fetch_button2 = gr.Button("API ์คํ2")
with gr.Column():
ref3 = gr.Textbox(label="์ฐธ์กฐ๊ธ 3 (์ ํ๋ธ URL)")
fetch_button3 = gr.Button("API ์คํ3")
output_box = gr.Textbox(label="๊ฒฐ๊ณผ", lines=20, interactive=False)
# ์ฐธ์กฐ๊ธ1 API ์คํ ํจ์ (๋ธ๋ก๊ทธ API ์ฌ์ฉ)
def fetch_ref1(url: str) -> str:
if not url:
return "URL์ ์
๋ ฅํด์ฃผ์ธ์."
try:
result = client.predict(
url=url,
api_name="/predict"
)
return result
except Exception as e:
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
# ์ฐธ์กฐ๊ธ2 API ์คํ ํจ์ (๋ธ๋ก๊ทธ API ์ฌ์ฉ)
def fetch_ref2(url: str) -> str:
if not url:
return "URL์ ์
๋ ฅํด์ฃผ์ธ์."
try:
result = client.predict(
url=url,
api_name="/predict"
)
return result
except Exception as e:
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
# ์ฐธ์กฐ๊ธ3 API ์คํ ํจ์ (์ ํ๋ธ API ์ฌ์ฉ)
def fetch_ref3(url: str) -> str:
if not url:
return "URL์ ์
๋ ฅํด์ฃผ์ธ์."
try:
# ์ ํ๋ธ API๋ ๋งค๊ฐ๋ณ์๋ช
์ด youtube_url์ด์ด์ผ ํจ
youtube_client = Client("Kims12/you")
result = youtube_client.predict(
youtube_url=url,
api_name="/predict"
)
return result
except Exception as e:
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
# ๊ฐ ๋ฒํผ ํด๋ฆญ ์ API ์คํ
fetch_button1.click(fn=fetch_ref1, inputs=ref1, outputs=ref1)
fetch_button2.click(fn=fetch_ref2, inputs=ref2, outputs=ref2)
fetch_button3.click(fn=fetch_ref3, inputs=ref3, outputs=ref3)
# ๊ธฐ์กด ๋ธ๋ก๊ทธ ์์ฑ ํจ์ (๊ธฐ๋ณธ)
def generate_blog(tone_value: str, ref1_value: str, ref2_value: str, ref3_value: str) -> str:
# ํ๋กฌํํธ ์์ฑ
question = (
f"๋งํฌ: {tone_value}\n"
f"์ฐธ์กฐ๊ธ1: {ref1_value}\n"
f"์ฐธ์กฐ๊ธ2: {ref2_value}\n"
f"์ฐธ์กฐ๊ธ3: {ref3_value}\n"
)
# OpenAI GPT-4o-mini ๋ชจ๋ธ ํธ์ถ
response = call_openai_api(
content=question,
system_message=OPENAI_SYSTEM_MESSAGE,
max_tokens=OPENAI_MAX_TOKENS,
temperature=OPENAI_TEMPERATURE,
top_p=OPENAI_TOP_P
)
return response
generate_button = gr.Button("์์ฑํ๊ธฐ")
generate_button.click(fn=generate_blog,
inputs=[tone_radio, ref1, ref2, ref3],
outputs=output_box)
# ์ถ๊ฐ ๊ธฐ๋ฅ: API๋ก ๊ฐ์ ธ์จ ๊ธ๋ค์ ๋ชจ๋ ์ข
ํฉํ์ฌ ์ต์ข
๊ฒฐ๊ณผ ๊ธ ์์ฑ
def generate_final_blog(ref1_value: str, ref2_value: str, ref3_value: str) -> str:
prompt = (
"๋ค์์ ๊ฐ๊ฐ์ API ์คํ์ ํตํด ๊ฐ์ ธ์จ ์๋ฃ๋ค์
๋๋ค.\n"
f"์๋ฃ 1: {ref1_value}\n"
f"์๋ฃ 2: {ref2_value}\n"
f"์๋ฃ 3: {ref3_value}\n"
"์ด ์๋ฃ๋ค์ ์ข
ํฉํ์ฌ ์๋ก์ด ๋ธ๋ก๊ทธ ๊ธ์ ์์ฑํด์ค."
)
result = call_openai_api(
content=prompt,
system_message=OPENAI_SYSTEM_MESSAGE,
max_tokens=OPENAI_MAX_TOKENS,
temperature=OPENAI_TEMPERATURE,
top_p=OPENAI_TOP_P
)
return result
final_generate_button = gr.Button("์ต์ข
๊ฒฐ๊ณผ ์์ฑ")
final_output = gr.Textbox(label="์ต์ข
๊ฒฐ๊ณผ", lines=20, interactive=False)
final_generate_button.click(fn=generate_final_blog,
inputs=[ref1, ref2, ref3],
outputs=final_output)
if __name__ == "__main__":
demo.launch()
|