Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
from typing import Optional
|
| 5 |
-
from gradio_client import Client
|
| 6 |
|
| 7 |
#############################
|
| 8 |
|
|
@@ -50,7 +51,7 @@ OPENAI_SYSTEM_MESSAGE = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.
|
|
| 50 |
3. ํนํ ๋๋ '์ ๋ณด์ฑ(Informative)' ์ ๋ฌธ ๋ธ๋ก๊ทธ ๋ง์ผํ
์ ๋ฌธ๊ฐ์ด๋ค.
|
| 51 |
4. ์ ๋ณด ์ ๊ณต์ ์ด์ ์ ๋ง์ถ์ด ์์ฑํ๋ค.
|
| 52 |
##[ํ
์คํธ ์์ฑ ๊ท์น]
|
| 53 |
-
1. ์์ฃผ์ ๋ฅผ 5๊ฐ
|
| 54 |
2. ์ ์ฒด ๋งฅ๋ฝ์ ์ดํดํ๊ณ ๋ฌธ์ฅ์ ์ผ๊ด์ฑ์ ์ ์งํ๋ผ.
|
| 55 |
3. ์ ๋๋ก ์ฐธ๊ณ ๊ธ์ ํ๋ฌธ์ฅ ์ด์ ๊ทธ๋๋ก ์ถ๋ ฅํ์ง ๋ง ๊ฒ.
|
| 56 |
4. ์ฃผ์ ์ ์ํฉ์ ๋ง๋ ์ ์ ํ ์ดํ๋ฅผ ์ ํํ๋ผ.
|
|
@@ -76,21 +77,17 @@ OPENAI_TOP_P = 0.95
|
|
| 76 |
#############################
|
| 77 |
# API ํด๋ผ์ด์ธํธ ์ค์
|
| 78 |
#############################
|
| 79 |
-
# API
|
| 80 |
-
|
| 81 |
-
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
| 82 |
-
|
| 83 |
-
if not API_ENDPOINT:
|
| 84 |
-
raise ValueError("API ์๋ํฌ์ธํธ URL(API_ENDPOINT_URL)์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 85 |
-
|
| 86 |
-
client = Client(API_ENDPOINT, api_key=HUGGINGFACE_TOKEN)
|
| 87 |
|
| 88 |
def fetch_blog_content(url: str) -> str:
|
| 89 |
"""
|
| 90 |
-
์ฃผ์ด์ง URL์
|
| 91 |
"""
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
-
result =
|
| 94 |
url=url,
|
| 95 |
api_name="/predict"
|
| 96 |
)
|
|
@@ -111,19 +108,16 @@ with gr.Blocks() as demo:
|
|
| 111 |
value="์ผ๋ฐ์ ์ธ" # ๊ธฐ๋ณธ ์ ํ
|
| 112 |
)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
with gr.Row():
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
ref1 = gr.Textbox(label="์ฐธ์กฐ๊ธ 1 URL", placeholder="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL ์
๋ ฅ")
|
| 118 |
-
fetch_ref1 = gr.Button("์ฐธ์กฐ๊ธ1 ๊ฐ์ ธ์ค๊ธฐ")
|
| 119 |
-
# ์ฐธ์กฐ๊ธ 2
|
| 120 |
-
with gr.Column():
|
| 121 |
-
ref2 = gr.Textbox(label="์ฐธ์กฐ๊ธ 2 URL", placeholder="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL ์
๋ ฅ")
|
| 122 |
-
fetch_ref2 = gr.Button("์ฐธ์กฐ๊ธ2 ๊ฐ์ ธ์ค๊ธฐ")
|
| 123 |
-
# ์ฐธ์กฐ๊ธ 3
|
| 124 |
-
with gr.Column():
|
| 125 |
-
ref3 = gr.Textbox(label="์ฐธ์กฐ๊ธ 3 URL", placeholder="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL ์
๋ ฅ")
|
| 126 |
-
fetch_ref3 = gr.Button("์ฐธ์กฐ๊ธ3 ๊ฐ์ ธ์ค๊ธฐ")
|
| 127 |
|
| 128 |
output_box = gr.Textbox(label="๊ฒฐ๊ณผ", lines=20, interactive=False)
|
| 129 |
|
|
@@ -146,12 +140,6 @@ with gr.Blocks() as demo:
|
|
| 146 |
)
|
| 147 |
return response
|
| 148 |
|
| 149 |
-
def fetch_ref(ref_url: str) -> str:
|
| 150 |
-
if ref_url.strip() == "":
|
| 151 |
-
return "URL์ ์
๋ ฅํด์ฃผ์ธ์."
|
| 152 |
-
return fetch_blog_content(ref_url)
|
| 153 |
-
|
| 154 |
-
# ์์ฑํ๊ธฐ ๋ฒํผ
|
| 155 |
generate_button = gr.Button("์์ฑํ๊ธฐ")
|
| 156 |
generate_button.click(
|
| 157 |
fn=generate_blog,
|
|
@@ -159,23 +147,32 @@ with gr.Blocks() as demo:
|
|
| 159 |
outputs=output_box
|
| 160 |
)
|
| 161 |
|
| 162 |
-
# ์ฐธ์กฐ๊ธ1
|
| 163 |
-
fetch_ref1
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
inputs=ref1,
|
| 166 |
outputs=ref1
|
| 167 |
)
|
| 168 |
|
| 169 |
-
# ์ฐธ์กฐ๊ธ2
|
| 170 |
-
fetch_ref2
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
| 172 |
inputs=ref2,
|
| 173 |
outputs=ref2
|
| 174 |
)
|
| 175 |
|
| 176 |
-
# ์ฐธ์กฐ๊ธ3
|
| 177 |
-
fetch_ref3
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
| 179 |
inputs=ref3,
|
| 180 |
outputs=ref3
|
| 181 |
)
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import os
|
| 5 |
from typing import Optional
|
| 6 |
+
from gradio_client import Client # gradio_client ์ถ๊ฐ
|
| 7 |
|
| 8 |
#############################
|
| 9 |
|
|
|
|
| 51 |
3. ํนํ ๋๋ '์ ๋ณด์ฑ(Informative)' ์ ๋ฌธ ๋ธ๋ก๊ทธ ๋ง์ผํ
์ ๋ฌธ๊ฐ์ด๋ค.
|
| 52 |
4. ์ ๋ณด ์ ๊ณต์ ์ด์ ์ ๋ง์ถ์ด ์์ฑํ๋ค.
|
| 53 |
##[ํ
์คํธ ์์ฑ ๊ท์น]
|
| 54 |
+
1. ์์ฃผ์ ๋ฅผ 5๊ฐ๋ก ๊ตฌ๋ถํ์ฌ 2000์ ์ด์๋๋๋ก ์์ฑํ๋ผ.
|
| 55 |
2. ์ ์ฒด ๋งฅ๋ฝ์ ์ดํดํ๊ณ ๋ฌธ์ฅ์ ์ผ๊ด์ฑ์ ์ ์งํ๋ผ.
|
| 56 |
3. ์ ๋๋ก ์ฐธ๊ณ ๊ธ์ ํ๋ฌธ์ฅ ์ด์ ๊ทธ๋๋ก ์ถ๋ ฅํ์ง ๋ง ๊ฒ.
|
| 57 |
4. ์ฃผ์ ์ ์ํฉ์ ๋ง๋ ์ ์ ํ ์ดํ๋ฅผ ์ ํํ๋ผ.
|
|
|
|
| 77 |
#############################
|
| 78 |
# API ํด๋ผ์ด์ธํธ ์ค์
|
| 79 |
#############################
|
| 80 |
+
# ์ธ๋ถ Space์ API ํด๋ผ์ด์ธํธ ์ค์
|
| 81 |
+
API_CLIENT = Client("Kims12/blog")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def fetch_blog_content(url: str) -> str:
|
| 84 |
"""
|
| 85 |
+
์ฃผ์ด์ง URL์ ์ด์ฉํด ๋ธ๋ก๊ทธ ๋ณธ๋ฌธ์ ๊ฐ์ ธ์ค๋ ํจ์.
|
| 86 |
"""
|
| 87 |
+
if not url.strip():
|
| 88 |
+
return "URL์ ์
๋ ฅํด์ฃผ์ธ์."
|
| 89 |
try:
|
| 90 |
+
result = API_CLIENT.predict(
|
| 91 |
url=url,
|
| 92 |
api_name="/predict"
|
| 93 |
)
|
|
|
|
| 108 |
value="์ผ๋ฐ์ ์ธ" # ๊ธฐ๋ณธ ์ ํ
|
| 109 |
)
|
| 110 |
|
| 111 |
+
# ์ฐธ์กฐ๊ธ ์
๋ ฅ (3๊ฐ) ๋ฐ API ์คํ ๋ฒํผ
|
| 112 |
+
with gr.Row():
|
| 113 |
+
ref1 = gr.Textbox(label="์ฐธ์กฐ๊ธ 1 (URL ์
๋ ฅ)")
|
| 114 |
+
fetch1 = gr.Button("์ฐธ์กฐ๊ธ1 API ์คํ")
|
| 115 |
+
with gr.Row():
|
| 116 |
+
ref2 = gr.Textbox(label="์ฐธ์กฐ๊ธ 2 (URL ์
๋ ฅ)")
|
| 117 |
+
fetch2 = gr.Button("์ฐธ์กฐ๊ธ2 API ์คํ")
|
| 118 |
with gr.Row():
|
| 119 |
+
ref3 = gr.Textbox(label="์ฐธ์กฐ๊ธ 3 (URL ์
๋ ฅ)")
|
| 120 |
+
fetch3 = gr.Button("์ฐธ์กฐ๊ธ3 API ์คํ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
output_box = gr.Textbox(label="๊ฒฐ๊ณผ", lines=20, interactive=False)
|
| 123 |
|
|
|
|
| 140 |
)
|
| 141 |
return response
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
generate_button = gr.Button("์์ฑํ๊ธฐ")
|
| 144 |
generate_button.click(
|
| 145 |
fn=generate_blog,
|
|
|
|
| 147 |
outputs=output_box
|
| 148 |
)
|
| 149 |
|
| 150 |
+
# ์ฐธ์กฐ๊ธ1 API ์คํ
|
| 151 |
+
def fetch_ref1(url: str) -> str:
|
| 152 |
+
return fetch_blog_content(url)
|
| 153 |
+
|
| 154 |
+
fetch1.click(
|
| 155 |
+
fn=fetch_ref1,
|
| 156 |
inputs=ref1,
|
| 157 |
outputs=ref1
|
| 158 |
)
|
| 159 |
|
| 160 |
+
# ์ฐธ์กฐ๊ธ2 API ์คํ
|
| 161 |
+
def fetch_ref2(url: str) -> str:
|
| 162 |
+
return fetch_blog_content(url)
|
| 163 |
+
|
| 164 |
+
fetch2.click(
|
| 165 |
+
fn=fetch_ref2,
|
| 166 |
inputs=ref2,
|
| 167 |
outputs=ref2
|
| 168 |
)
|
| 169 |
|
| 170 |
+
# ์ฐธ์กฐ๊ธ3 API ์คํ
|
| 171 |
+
def fetch_ref3(url: str) -> str:
|
| 172 |
+
return fetch_blog_content(url)
|
| 173 |
+
|
| 174 |
+
fetch3.click(
|
| 175 |
+
fn=fetch_ref3,
|
| 176 |
inputs=ref3,
|
| 177 |
outputs=ref3
|
| 178 |
)
|