blogmaker / app.py
kijeoung's picture
Update app.py
8b5a323 verified
import gradio as gr
import os
from huggingface_hub import InferenceClient
#############################
# [๋ธ”๋กœ๊ทธ ์ƒ์„ฑ๊ธฐ - Cohere Command R+]
#############################
# Cohere Command R+ ๋ชจ๋ธ ID ์ •์˜
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
SYSTEM_MESSAGE = "๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”."
MAX_TOKENS = 4000
TEMPERATURE = 0.7
TOP_P = 0.95
def get_client(hf_token):
"""
HuggingFace InferenceClient ์ƒ์„ฑ.
"""
if not hf_token:
raise ValueError("HuggingFace API ํ† ํฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
return InferenceClient(COHERE_MODEL, token=hf_token)
def respond_cohere_qna(question, system_message, max_tokens, temperature, top_p, hf_token):
"""
Cohere ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ์‘๋‹ต ์ƒ์„ฑ.
"""
client = get_client(hf_token)
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": question}
]
response = client.chat_completion(
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
return response.choices[0].message.content
# ๋ธ”๋กœ๊ทธ ์ƒ์„ฑ ํ•จ์ˆ˜ ์ •์˜
def generate_blog(tone, ref1, ref2, ref3):
"""
๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
HuggingFace ํ† ํฐ์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์ง์ ‘ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
"""
# HuggingFace ํ† ํฐ ๊ฐ€์ ธ์˜ค๊ธฐ
hf_token_value = os.getenv("HF_TOKEN")
if not hf_token_value:
return "HuggingFace ํ† ํฐ์ด ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."
# ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ
prompt = f"๋งํˆฌ: {tone}\n์ฐธ์กฐ๋ฌธ 1: {ref1}\n์ฐธ์กฐ๋ฌธ 2: {ref2}\n์ฐธ์กฐ๋ฌธ 3: {ref3}"
# ์‘๋‹ต ์ƒ์„ฑ
return respond_cohere_qna(
question=prompt,
system_message=SYSTEM_MESSAGE,
max_tokens=MAX_TOKENS,
temperature=TEMPERATURE,
top_p=TOP_P,
hf_token=hf_token_value
)
# Gradio UI ๊ตฌ์„ฑ
with gr.Blocks() as demo:
gr.Markdown("# ๋ธ”๋กœ๊ทธ ์ƒ์„ฑ๊ธฐ")
# HuggingFace ํ† ํฐ (secrets ์‚ฌ์šฉ)
hf_token = os.getenv("HF_TOKEN")
# ์ž…๋ ฅ ํ•„๋“œ
with gr.Row():
tone = gr.Radio(
choices=["์นœ๊ทผํ•˜๊ฒŒ", "์ผ๋ฐ˜์ ์ธ", "์ „๋ฌธ์ ์ธ"],
label="๋งํˆฌ ๋ฐ”๊พธ๊ธฐ",
value="์ผ๋ฐ˜์ ์ธ"
)
ref1 = gr.Textbox(label="์ฐธ์กฐ๋ฌธ 1", lines=3)
ref2 = gr.Textbox(label="์ฐธ์กฐ๋ฌธ 2", lines=3)
ref3 = gr.Textbox(label="์ฐธ์กฐ๋ฌธ 3", lines=3)
# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
answer_output = gr.Textbox(label="์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€", lines=10, interactive=False)
# ๋ฒ„ํŠผ ๋ฐฐ์น˜
submit_button = gr.Button("์ƒ์„ฑ")
submit_button.click(
fn=generate_blog,
inputs=[tone, ref1, ref2, ref3],
outputs=answer_output
)
# ์•ฑ ์‹คํ–‰
if __name__ == "__main__":
demo.launch()