Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,23 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Cohere Command R+ 모델 ID 정의
|
7 |
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
|
8 |
|
9 |
-
def get_client(model_name):
|
10 |
"""
|
11 |
모델 이름에 맞춰 InferenceClient 생성.
|
12 |
토큰은 환경 변수에서 가져옴.
|
13 |
"""
|
14 |
hf_token = os.getenv("HF_TOKEN")
|
15 |
if not hf_token:
|
16 |
-
raise ValueError("HuggingFace API
|
17 |
|
18 |
if model_name == "Cohere Command R+":
|
19 |
model_id = COHERE_MODEL
|
@@ -22,17 +26,14 @@ def get_client(model_name):
|
|
22 |
return InferenceClient(model_id, token=hf_token)
|
23 |
|
24 |
def respond_cohere_qna(
|
25 |
-
|
26 |
-
reference1: str,
|
27 |
-
reference2: str,
|
28 |
-
reference3: str,
|
29 |
system_message: str,
|
30 |
max_tokens: int,
|
31 |
temperature: float,
|
32 |
top_p: float
|
33 |
):
|
34 |
"""
|
35 |
-
Cohere Command R+ 모델을 이용해
|
36 |
"""
|
37 |
model_name = "Cohere Command R+"
|
38 |
try:
|
@@ -40,14 +41,14 @@ def respond_cohere_qna(
|
|
40 |
except ValueError as e:
|
41 |
return f"오류: {str(e)}"
|
42 |
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
try:
|
46 |
response_full = client.chat_completion(
|
47 |
-
|
48 |
-
{"role": "system", "content": system_message},
|
49 |
-
{"role": "user", "content": question}
|
50 |
-
],
|
51 |
max_tokens=max_tokens,
|
52 |
temperature=temperature,
|
53 |
top_p=top_p,
|
@@ -57,39 +58,63 @@ def respond_cohere_qna(
|
|
57 |
except Exception as e:
|
58 |
return f"오류가 발생했습니다: {str(e)}"
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
with gr.Blocks() as demo:
|
62 |
gr.Markdown("# 블로그 생성기")
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
)
|
83 |
-
max_tokens = gr.Slider(minimum=100, maximum=5000, value=2000, step=100, label="Max Tokens")
|
84 |
-
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
85 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
86 |
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
|
|
89 |
generate_button.click(
|
90 |
-
fn=
|
91 |
-
inputs=[
|
92 |
-
outputs=
|
93 |
)
|
94 |
|
95 |
if __name__ == "__main__":
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
+
from typing import Optional
|
5 |
+
|
6 |
+
#############################
|
7 |
+
# [기본코드] - Cohere 관련 부분만 남김
|
8 |
+
#############################
|
9 |
|
10 |
# Cohere Command R+ 모델 ID 정의
|
11 |
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
|
12 |
|
13 |
+
def get_client(model_name: str):
|
14 |
"""
|
15 |
모델 이름에 맞춰 InferenceClient 생성.
|
16 |
토큰은 환경 변수에서 가져옴.
|
17 |
"""
|
18 |
hf_token = os.getenv("HF_TOKEN")
|
19 |
if not hf_token:
|
20 |
+
raise ValueError("HuggingFace API 토큰(HF_TOKEN)이 설정되지 않았습니다.")
|
21 |
|
22 |
if model_name == "Cohere Command R+":
|
23 |
model_id = COHERE_MODEL
|
|
|
26 |
return InferenceClient(model_id, token=hf_token)
|
27 |
|
28 |
def respond_cohere_qna(
|
29 |
+
question: str,
|
|
|
|
|
|
|
30 |
system_message: str,
|
31 |
max_tokens: int,
|
32 |
temperature: float,
|
33 |
top_p: float
|
34 |
):
|
35 |
"""
|
36 |
+
Cohere Command R+ 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
37 |
"""
|
38 |
model_name = "Cohere Command R+"
|
39 |
try:
|
|
|
41 |
except ValueError as e:
|
42 |
return f"오류: {str(e)}"
|
43 |
|
44 |
+
messages = [
|
45 |
+
{"role": "system", "content": system_message},
|
46 |
+
{"role": "user", "content": question}
|
47 |
+
]
|
48 |
|
49 |
try:
|
50 |
response_full = client.chat_completion(
|
51 |
+
messages,
|
|
|
|
|
|
|
52 |
max_tokens=max_tokens,
|
53 |
temperature=temperature,
|
54 |
top_p=top_p,
|
|
|
58 |
except Exception as e:
|
59 |
return f"오류가 발생했습니다: {str(e)}"
|
60 |
|
61 |
+
#############################
|
62 |
+
# 고급 설정 (Cohere) - 코드에서만 정의 (UI에 노출 금지)
|
63 |
+
#############################
|
64 |
+
COHERE_SYSTEM_MESSAGE = """반드시 한글로 답변할 것.
|
65 |
+
너는 최고의 비서이다.
|
66 |
+
내가 요구하는 것들을 최대한 자세하고 정확하게 답변하라.
|
67 |
+
"""
|
68 |
+
COHERE_MAX_TOKENS = 4000
|
69 |
+
COHERE_TEMPERATURE = 0.7
|
70 |
+
COHERE_TOP_P = 0.95
|
71 |
+
|
72 |
+
#############################
|
73 |
+
# UI - 블로그 생성기
|
74 |
+
#############################
|
75 |
with gr.Blocks() as demo:
|
76 |
gr.Markdown("# 블로그 생성기")
|
77 |
|
78 |
+
# 말투바꾸기 (라디오 버튼)
|
79 |
+
tone_radio = gr.Radio(
|
80 |
+
label="말투바꾸기",
|
81 |
+
choices=["친근하게", "일반적인", "전문적인"],
|
82 |
+
value="일반적인" # 기본 선택
|
83 |
+
)
|
84 |
|
85 |
+
# 참조글 입력 (3개)
|
86 |
+
ref1 = gr.Textbox(label="참조글 1")
|
87 |
+
ref2 = gr.Textbox(label="참조글 2")
|
88 |
+
ref3 = gr.Textbox(label="참조글 3")
|
89 |
|
90 |
+
output_box = gr.Textbox(label="결과", lines=8, interactive=False)
|
91 |
|
92 |
+
def generate_blog(tone_value, ref1_value, ref2_value, ref3_value):
|
93 |
+
# 프롬프트: “~~”
|
94 |
+
# 말투와 참조글들을 하나로 합쳐 질문(프롬프트) 형식으로 구성
|
95 |
+
question = (
|
96 |
+
f"~~\n"
|
97 |
+
f"말투: {tone_value}\n"
|
98 |
+
f"참조글1: {ref1_value}\n"
|
99 |
+
f"참조글2: {ref2_value}\n"
|
100 |
+
f"참조글3: {ref3_value}\n"
|
101 |
)
|
|
|
|
|
|
|
102 |
|
103 |
+
# Cohere Command R+ 모델 호출
|
104 |
+
response = respond_cohere_qna(
|
105 |
+
question=question,
|
106 |
+
system_message=COHERE_SYSTEM_MESSAGE,
|
107 |
+
max_tokens=COHERE_MAX_TOKENS,
|
108 |
+
temperature=COHERE_TEMPERATURE,
|
109 |
+
top_p=COHERE_TOP_P
|
110 |
+
)
|
111 |
+
return response
|
112 |
|
113 |
+
generate_button = gr.Button("생성하기")
|
114 |
generate_button.click(
|
115 |
+
fn=generate_blog,
|
116 |
+
inputs=[tone_radio, ref1, ref2, ref3],
|
117 |
+
outputs=output_box
|
118 |
)
|
119 |
|
120 |
if __name__ == "__main__":
|