Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
# 네이버 개발자 센터에서 발급받은 Client ID와 Client Secret
|
8 |
-
client_id = "YOUR_NAVER_CLIENT_ID" # 여기에 네이버에서 발급받은 Client ID를 입력하세요.
|
9 |
-
client_secret = "YOUR_NAVER_CLIENT_SECRET" # 여기에 네이버에서 발급받은 Client Secret을 입력하세요.
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
# 요청 데이터 (원본 언어, 목표 언어, 번역할 텍스트)
|
21 |
-
data = {
|
22 |
-
"source": source_lang,
|
23 |
-
"target": target_lang,
|
24 |
-
"text": text,
|
25 |
-
}
|
26 |
|
|
|
|
|
|
|
|
|
27 |
try:
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
except Exception as e:
|
36 |
-
# 오류 발생 시 메시지 반환
|
37 |
return f"오류가 발생했습니다: {str(e)}"
|
38 |
|
39 |
-
# Gradio
|
40 |
with gr.Blocks() as demo:
|
41 |
-
gr.Markdown("#
|
42 |
-
|
43 |
-
# 언어 선택
|
44 |
-
source_lang = gr.Radio(
|
45 |
-
choices=["ko", "en"], # 원본 언어 선택 (한국어: ko, 영어: en)
|
46 |
-
label="원본 언어",
|
47 |
-
value="ko"
|
48 |
-
)
|
49 |
-
target_lang = gr.Radio(
|
50 |
-
choices=["ko", "en"], # 목표 언어 선택 (한국어: ko, 영어: en)
|
51 |
-
label="목표 언어",
|
52 |
-
value="en"
|
53 |
-
)
|
54 |
|
55 |
# 입력 텍스트
|
56 |
-
input_text = gr.Textbox(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# 번역 결과
|
59 |
translation_output = gr.Textbox(label="번역 결과", lines=5, interactive=False)
|
@@ -63,9 +64,9 @@ with gr.Blocks() as demo:
|
|
63 |
|
64 |
# 번역 함수 연결
|
65 |
translate_button.click(
|
66 |
-
fn=
|
67 |
-
inputs=[input_text,
|
68 |
-
outputs=translation_output
|
69 |
)
|
70 |
|
71 |
# 메인 실행부
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
|
5 |
+
# Cohere Command R+ 모델 ID 정의
|
6 |
+
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
|
|
|
|
|
|
|
7 |
|
8 |
+
def get_client():
|
9 |
+
"""
|
10 |
+
Cohere Command R+ 모델을 위한 InferenceClient 생성.
|
11 |
+
토큰은 환경 변수에서 가져옴.
|
12 |
+
"""
|
13 |
+
hf_token = os.getenv("HF_TOKEN")
|
14 |
+
if not hf_token:
|
15 |
+
raise ValueError("HuggingFace API 토큰이 필요합니다.")
|
16 |
+
return InferenceClient(COHERE_MODEL, token=hf_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
def respond_cohere_qna(question, system_message, max_tokens, temperature, top_p):
|
19 |
+
"""
|
20 |
+
Cohere Command R+ 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
21 |
+
"""
|
22 |
try:
|
23 |
+
client = get_client()
|
24 |
+
messages = [
|
25 |
+
{"role": "system", "content": system_message},
|
26 |
+
{"role": "user", "content": question}
|
27 |
+
]
|
28 |
+
response = client.chat_completion(
|
29 |
+
messages,
|
30 |
+
max_tokens=max_tokens,
|
31 |
+
temperature=temperature,
|
32 |
+
top_p=top_p,
|
33 |
+
)
|
34 |
+
return response.choices[0].message.content
|
35 |
except Exception as e:
|
|
|
36 |
return f"오류가 발생했습니다: {str(e)}"
|
37 |
|
38 |
+
# Gradio UI 구성
|
39 |
with gr.Blocks() as demo:
|
40 |
+
gr.Markdown("# Cohere Command R+ 번역기")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# 입력 텍스트
|
43 |
+
input_text = gr.Textbox(label="입력 텍스트", lines=5)
|
44 |
+
|
45 |
+
# 고급 설정
|
46 |
+
with gr.Accordion("고급 설정 (Cohere)", open=False):
|
47 |
+
system_message = gr.Textbox(
|
48 |
+
value="""반드시 한글로 답변할 것.
|
49 |
+
너는 최고의 비서이다.
|
50 |
+
내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
|
51 |
+
""",
|
52 |
+
label="System Message",
|
53 |
+
lines=3
|
54 |
+
)
|
55 |
+
max_tokens = gr.Slider(minimum=100, maximum=10000, value=4000, step=100, label="Max Tokens")
|
56 |
+
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
57 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
58 |
|
59 |
# 번역 결과
|
60 |
translation_output = gr.Textbox(label="번역 결과", lines=5, interactive=False)
|
|
|
64 |
|
65 |
# 번역 함수 연결
|
66 |
translate_button.click(
|
67 |
+
fn=respond_cohere_qna,
|
68 |
+
inputs=[input_text, system_message, max_tokens, temperature, top_p],
|
69 |
+
outputs=translation_output
|
70 |
)
|
71 |
|
72 |
# 메인 실행부
|