Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
|
|
|
5 |
MODELS = {
|
6 |
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
7 |
"Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
@@ -12,11 +13,19 @@ MODELS = {
|
|
12 |
"Aya-23-35B": "CohereForAI/aya-23-35B"
|
13 |
}
|
14 |
|
|
|
|
|
|
|
15 |
def get_client(model_name):
|
16 |
-
model_id = MODELS[model_name]
|
17 |
hf_token = os.getenv("HF_TOKEN")
|
18 |
if not hf_token:
|
19 |
raise ValueError("HF_TOKEN 환경 변수가 필요합니다.")
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
return InferenceClient(model_id, token=hf_token)
|
21 |
|
22 |
def respond(
|
@@ -41,8 +50,8 @@ def respond(
|
|
41 |
messages.append({"role": "user", "content": message})
|
42 |
|
43 |
try:
|
44 |
-
if "Cohere
|
45 |
-
# Cohere 모델을 위한 비스트리밍 처리
|
46 |
response = client.chat_completion(
|
47 |
messages,
|
48 |
max_tokens=max_tokens,
|
@@ -51,7 +60,7 @@ def respond(
|
|
51 |
)
|
52 |
assistant_message = response.choices[0].message.content
|
53 |
chat_history.append((message, assistant_message))
|
54 |
-
|
55 |
else:
|
56 |
# 다른 모델들을 위한 스트리밍 처리
|
57 |
stream = client.chat_completion(
|
@@ -78,57 +87,6 @@ def respond(
|
|
78 |
def clear_conversation():
|
79 |
return []
|
80 |
|
81 |
-
# Cohere Command R+ 전용 응답 함수
|
82 |
-
from openai import OpenAI
|
83 |
-
|
84 |
-
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
85 |
-
|
86 |
-
cohere_client = OpenAI(
|
87 |
-
base_url="https://api-inference.huggingface.co/v1/",
|
88 |
-
api_key=ACCESS_TOKEN,
|
89 |
-
)
|
90 |
-
|
91 |
-
def cohere_respond(
|
92 |
-
message,
|
93 |
-
chat_history,
|
94 |
-
system_message,
|
95 |
-
max_tokens,
|
96 |
-
temperature,
|
97 |
-
top_p,
|
98 |
-
):
|
99 |
-
messages = [{"role": "system", "content": system_message}]
|
100 |
-
|
101 |
-
for human, assistant in chat_history:
|
102 |
-
if human:
|
103 |
-
messages.append({"role": "user", "content": human})
|
104 |
-
if assistant:
|
105 |
-
messages.append({"role": "assistant", "content": assistant})
|
106 |
-
|
107 |
-
messages.append({"role": "user", "content": message})
|
108 |
-
|
109 |
-
response = ""
|
110 |
-
|
111 |
-
try:
|
112 |
-
for msg in cohere_client.chat.completions.create(
|
113 |
-
model="CohereForAI/c4ai-command-r-plus-08-2024",
|
114 |
-
max_tokens=max_tokens,
|
115 |
-
stream=True,
|
116 |
-
temperature=temperature,
|
117 |
-
top_p=top_p,
|
118 |
-
messages=messages,
|
119 |
-
):
|
120 |
-
token = msg.choices[0].delta.content
|
121 |
-
response += token
|
122 |
-
if len(chat_history) > 0 and chat_history[-1][0] == message:
|
123 |
-
chat_history[-1] = (message, response)
|
124 |
-
else:
|
125 |
-
chat_history.append((message, response))
|
126 |
-
yield chat_history
|
127 |
-
except Exception as e:
|
128 |
-
error_message = f"오류가 발생했습니다: {str(e)}"
|
129 |
-
chat_history.append((message, error_message))
|
130 |
-
yield chat_history
|
131 |
-
|
132 |
with gr.Blocks() as demo:
|
133 |
gr.Markdown("# Prompting AI Chatbot")
|
134 |
gr.Markdown("언어모델별 프롬프트 테스트 챗봇입니다.")
|
@@ -191,13 +149,13 @@ with gr.Blocks() as demo:
|
|
191 |
cohere_clear_button = gr.Button("대화 내역 지우기")
|
192 |
|
193 |
cohere_msg.submit(
|
194 |
-
|
195 |
-
[cohere_msg, cohere_chatbot,
|
196 |
cohere_chatbot
|
197 |
)
|
198 |
cohere_submit_button.click(
|
199 |
-
|
200 |
-
[cohere_msg, cohere_chatbot,
|
201 |
cohere_chatbot
|
202 |
)
|
203 |
cohere_clear_button.click(clear_conversation, outputs=cohere_chatbot, queue=False)
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
|
5 |
+
# 제거할 모델들을 MODELS 사전에서 제외
|
6 |
MODELS = {
|
7 |
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
8 |
"Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
|
|
13 |
"Aya-23-35B": "CohereForAI/aya-23-35B"
|
14 |
}
|
15 |
|
16 |
+
# Cohere Command R+ 모델 ID 정의
|
17 |
+
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
|
18 |
+
|
19 |
def get_client(model_name):
|
|
|
20 |
hf_token = os.getenv("HF_TOKEN")
|
21 |
if not hf_token:
|
22 |
raise ValueError("HF_TOKEN 환경 변수가 필요합니다.")
|
23 |
+
if model_name in MODELS:
|
24 |
+
model_id = MODELS[model_name]
|
25 |
+
elif model_name == "Cohere Command R+":
|
26 |
+
model_id = COHERE_MODEL
|
27 |
+
else:
|
28 |
+
raise ValueError("유효하지 않은 모델 이름입니다.")
|
29 |
return InferenceClient(model_id, token=hf_token)
|
30 |
|
31 |
def respond(
|
|
|
50 |
messages.append({"role": "user", "content": message})
|
51 |
|
52 |
try:
|
53 |
+
if model_name == "Cohere Command R+":
|
54 |
+
# Cohere Command R+ 모델을 위한 비스트리밍 처리
|
55 |
response = client.chat_completion(
|
56 |
messages,
|
57 |
max_tokens=max_tokens,
|
|
|
60 |
)
|
61 |
assistant_message = response.choices[0].message.content
|
62 |
chat_history.append((message, assistant_message))
|
63 |
+
return chat_history
|
64 |
else:
|
65 |
# 다른 모델들을 위한 스트리밍 처리
|
66 |
stream = client.chat_completion(
|
|
|
87 |
def clear_conversation():
|
88 |
return []
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
with gr.Blocks() as demo:
|
91 |
gr.Markdown("# Prompting AI Chatbot")
|
92 |
gr.Markdown("언어모델별 프롬프트 테스트 챗봇입니다.")
|
|
|
149 |
cohere_clear_button = gr.Button("대화 내역 지우기")
|
150 |
|
151 |
cohere_msg.submit(
|
152 |
+
respond,
|
153 |
+
[cohere_msg, cohere_chatbot, "Cohere Command R+", cohere_max_tokens, cohere_temperature, cohere_top_p, cohere_system_message],
|
154 |
cohere_chatbot
|
155 |
)
|
156 |
cohere_submit_button.click(
|
157 |
+
respond,
|
158 |
+
[cohere_msg, cohere_chatbot, "Cohere Command R+", cohere_max_tokens, cohere_temperature, cohere_top_p, cohere_system_message],
|
159 |
cohere_chatbot
|
160 |
)
|
161 |
cohere_clear_button.click(clear_conversation, outputs=cohere_chatbot, queue=False)
|