Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import openai
|
4 |
-
import os
|
5 |
|
6 |
# 제거할 모델들을 MODELS 사전에서 제외
|
7 |
MODELS = {
|
@@ -17,7 +16,6 @@ MODELS = {
|
|
17 |
# Cohere Command R+ 모델 ID 정의
|
18 |
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
|
19 |
|
20 |
-
|
21 |
def get_client(model_name, hf_token):
|
22 |
"""
|
23 |
모델 이름에 맞춰 InferenceClient 생성.
|
@@ -35,91 +33,68 @@ def get_client(model_name, hf_token):
|
|
35 |
return InferenceClient(model_id, token=hf_token)
|
36 |
|
37 |
|
38 |
-
def
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
hf_token, # HF 토큰을 추가로 받음
|
47 |
):
|
|
|
|
|
|
|
48 |
try:
|
49 |
client = get_client(model_name, hf_token)
|
50 |
except ValueError as e:
|
51 |
-
|
52 |
-
return chat_history
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
try:
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
else:
|
73 |
-
# 다른 모델들을 위한 스트리밍 처리
|
74 |
-
stream = client.chat_completion(
|
75 |
-
messages,
|
76 |
-
max_tokens=max_tokens,
|
77 |
-
temperature=temperature,
|
78 |
-
top_p=top_p,
|
79 |
-
stream=True,
|
80 |
-
)
|
81 |
-
partial_message = ""
|
82 |
-
for response in stream:
|
83 |
-
if response.choices[0].delta.content is not None:
|
84 |
-
partial_message += response.choices[0].delta.content
|
85 |
-
if len(chat_history) > 0 and chat_history[-1][0] == message:
|
86 |
-
chat_history[-1] = (message, partial_message)
|
87 |
-
else:
|
88 |
-
chat_history.append((message, partial_message))
|
89 |
-
yield chat_history
|
90 |
except Exception as e:
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
temperature,
|
102 |
-
top_p,
|
103 |
-
hf_token, # HF 토큰 추가
|
104 |
):
|
|
|
|
|
|
|
105 |
model_name = "Cohere Command R+"
|
106 |
try:
|
107 |
client = get_client(model_name, hf_token)
|
108 |
except ValueError as e:
|
109 |
-
|
110 |
-
return chat_history
|
111 |
-
|
112 |
-
messages = [{"role": "system", "content": system_message}]
|
113 |
-
for human, assistant in chat_history:
|
114 |
-
if human:
|
115 |
-
messages.append({"role": "user", "content": human})
|
116 |
-
if assistant:
|
117 |
-
messages.append({"role": "assistant", "content": assistant})
|
118 |
|
119 |
-
messages
|
|
|
|
|
|
|
120 |
|
121 |
try:
|
122 |
-
# Cohere Command R+ 모델을 위한 비스트리밍 처리
|
123 |
response_full = client.chat_completion(
|
124 |
messages,
|
125 |
max_tokens=max_tokens,
|
@@ -127,65 +102,50 @@ def cohere_respond(
|
|
127 |
top_p=top_p,
|
128 |
)
|
129 |
assistant_message = response_full.choices[0].message.content
|
130 |
-
|
131 |
-
return chat_history
|
132 |
except Exception as e:
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
temperature,
|
144 |
-
top_p,
|
145 |
-
openai_token, # openai 토큰 추가
|
146 |
):
|
147 |
"""
|
148 |
-
|
149 |
"""
|
150 |
if not openai_token:
|
151 |
-
|
152 |
-
return chat_history
|
153 |
|
154 |
-
|
155 |
-
openai.api_key = openai_token # UI에서 받은 토큰 사용
|
156 |
|
157 |
-
messages = [
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
messages.append({"role": "user", "content": message})
|
162 |
|
163 |
try:
|
164 |
response = openai.ChatCompletion.create(
|
165 |
-
model="gpt-4o-mini", #
|
166 |
messages=messages,
|
167 |
max_tokens=max_tokens,
|
168 |
temperature=temperature,
|
169 |
top_p=top_p,
|
170 |
)
|
171 |
assistant_message = response.choices[0].message['content']
|
172 |
-
|
173 |
-
return chat_history
|
174 |
except Exception as e:
|
175 |
-
|
176 |
-
chat_history.append((message, error_message))
|
177 |
-
return chat_history
|
178 |
-
|
179 |
-
|
180 |
-
def clear_conversation():
|
181 |
-
return []
|
182 |
|
183 |
|
184 |
with gr.Blocks() as demo:
|
185 |
-
gr.Markdown("# Prompting AI
|
186 |
-
gr.Markdown("언어모델별
|
187 |
|
188 |
-
# --- 토큰 입력 UI 추가 ---
|
189 |
with gr.Row():
|
190 |
hf_token_box = gr.Textbox(
|
191 |
label="HuggingFace 토큰 (비공개)",
|
@@ -198,127 +158,108 @@ with gr.Blocks() as demo:
|
|
198 |
placeholder="OpenAI API 토큰을 입력하세요..."
|
199 |
)
|
200 |
|
|
|
201 |
with gr.Tab("일반 모델"):
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
system_message = gr.Textbox(
|
213 |
-
value="""반드시 한글로 답변할 것.
|
214 |
너는 최고의 비서이다.
|
215 |
내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
|
216 |
""",
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
with gr.Tab("Cohere Command R+"):
|
244 |
-
|
245 |
-
|
246 |
-
value="""반드시 한글로 답변할 것.
|
247 |
너는 최고의 비서이다.
|
248 |
내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
|
249 |
""",
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
cohere_top_p,
|
277 |
-
hf_token_box
|
278 |
-
]
|
279 |
-
cohere_msg.submit(cohere_respond, inputs_for_cohere, cohere_chatbot)
|
280 |
-
cohere_submit_button.click(cohere_respond, inputs_for_cohere, cohere_chatbot)
|
281 |
-
cohere_clear_button.click(clear_conversation, outputs=cohere_chatbot, queue=False)
|
282 |
-
|
283 |
with gr.Tab("ChatGPT"):
|
284 |
-
|
285 |
-
|
286 |
-
value="""반드시 한글로 답변할 것.
|
287 |
너는 ChatGPT, OpenAI에서 개발한 언어 모델이다.
|
288 |
내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
|
289 |
""",
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
chatgpt_max_tokens,
|
315 |
-
chatgpt_temperature,
|
316 |
-
chatgpt_top_p,
|
317 |
-
openai_token_box
|
318 |
-
]
|
319 |
-
chatgpt_msg.submit(chatgpt_respond, inputs_for_chatgpt, chatgpt_chatbot)
|
320 |
-
chatgpt_submit_button.click(chatgpt_respond, inputs_for_chatgpt, chatgpt_chatbot)
|
321 |
-
chatgpt_clear_button.click(clear_conversation, outputs=chatgpt_chatbot, queue=False)
|
322 |
|
323 |
if __name__ == "__main__":
|
324 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import openai
|
|
|
4 |
|
5 |
# 제거할 모델들을 MODELS 사전에서 제외
|
6 |
MODELS = {
|
|
|
16 |
# Cohere Command R+ 모델 ID 정의
|
17 |
COHERE_MODEL = "CohereForAI/c4ai-command-r-plus-08-2024"
|
18 |
|
|
|
19 |
def get_client(model_name, hf_token):
|
20 |
"""
|
21 |
모델 이름에 맞춰 InferenceClient 생성.
|
|
|
33 |
return InferenceClient(model_id, token=hf_token)
|
34 |
|
35 |
|
36 |
+
def respond_hf_qna(
|
37 |
+
question: str,
|
38 |
+
model_name: str,
|
39 |
+
max_tokens: int,
|
40 |
+
temperature: float,
|
41 |
+
top_p: float,
|
42 |
+
system_message: str,
|
43 |
+
hf_token: str
|
|
|
44 |
):
|
45 |
+
"""
|
46 |
+
HuggingFace 모델(Zephyr 등)에 대해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
47 |
+
"""
|
48 |
try:
|
49 |
client = get_client(model_name, hf_token)
|
50 |
except ValueError as e:
|
51 |
+
return f"오류: {str(e)}"
|
|
|
52 |
|
53 |
+
# 시스템 메시지 + 유저 질문을 한 번만 전달
|
54 |
+
messages = [
|
55 |
+
{"role": "system", "content": system_message},
|
56 |
+
{"role": "user", "content": question}
|
57 |
+
]
|
58 |
|
59 |
try:
|
60 |
+
# 스트리밍 대신 전체 답변(비스트리밍) 호출
|
61 |
+
response = client.chat_completion(
|
62 |
+
messages,
|
63 |
+
max_tokens=max_tokens,
|
64 |
+
temperature=temperature,
|
65 |
+
top_p=top_p,
|
66 |
+
stream=False, # 스트리밍 비활성화
|
67 |
+
)
|
68 |
+
assistant_message = response.choices[0].message.content
|
69 |
+
return assistant_message
|
70 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
except Exception as e:
|
72 |
+
return f"오류가 발생했습니다: {str(e)}"
|
73 |
+
|
74 |
+
|
75 |
+
def respond_cohere_qna(
|
76 |
+
question: str,
|
77 |
+
system_message: str,
|
78 |
+
max_tokens: int,
|
79 |
+
temperature: float,
|
80 |
+
top_p: float,
|
81 |
+
hf_token: str
|
|
|
|
|
|
|
82 |
):
|
83 |
+
"""
|
84 |
+
Cohere Command R+ 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
85 |
+
"""
|
86 |
model_name = "Cohere Command R+"
|
87 |
try:
|
88 |
client = get_client(model_name, hf_token)
|
89 |
except ValueError as e:
|
90 |
+
return f"오류: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
messages = [
|
93 |
+
{"role": "system", "content": system_message},
|
94 |
+
{"role": "user", "content": question}
|
95 |
+
]
|
96 |
|
97 |
try:
|
|
|
98 |
response_full = client.chat_completion(
|
99 |
messages,
|
100 |
max_tokens=max_tokens,
|
|
|
102 |
top_p=top_p,
|
103 |
)
|
104 |
assistant_message = response_full.choices[0].message.content
|
105 |
+
return assistant_message
|
|
|
106 |
except Exception as e:
|
107 |
+
return f"오류가 발생했습니다: {str(e)}"
|
108 |
+
|
109 |
+
|
110 |
+
def respond_chatgpt_qna(
|
111 |
+
question: str,
|
112 |
+
system_message: str,
|
113 |
+
max_tokens: int,
|
114 |
+
temperature: float,
|
115 |
+
top_p: float,
|
116 |
+
openai_token: str
|
|
|
|
|
|
|
117 |
):
|
118 |
"""
|
119 |
+
ChatGPT(OpenAI) 모델을 이용해 한 번의 질문(question)에 대한 답변을 반환하는 함수.
|
120 |
"""
|
121 |
if not openai_token:
|
122 |
+
return "OpenAI API 토큰이 필요합니다."
|
|
|
123 |
|
124 |
+
openai.api_key = openai_token
|
|
|
125 |
|
126 |
+
messages = [
|
127 |
+
{"role": "system", "content": system_message},
|
128 |
+
{"role": "user", "content": question}
|
129 |
+
]
|
|
|
130 |
|
131 |
try:
|
132 |
response = openai.ChatCompletion.create(
|
133 |
+
model="gpt-4o-mini", # 필요한 경우 변경
|
134 |
messages=messages,
|
135 |
max_tokens=max_tokens,
|
136 |
temperature=temperature,
|
137 |
top_p=top_p,
|
138 |
)
|
139 |
assistant_message = response.choices[0].message['content']
|
140 |
+
return assistant_message
|
|
|
141 |
except Exception as e:
|
142 |
+
return f"오류가 발생했습니다: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
|
145 |
with gr.Blocks() as demo:
|
146 |
+
gr.Markdown("# Prompting AI - 일반 문답형 데모")
|
147 |
+
gr.Markdown("언어모델별 문답형 테스트 데모입니다. 한 번에 한 질문씩만 주고받습니다.")
|
148 |
|
|
|
149 |
with gr.Row():
|
150 |
hf_token_box = gr.Textbox(
|
151 |
label="HuggingFace 토큰 (비공개)",
|
|
|
158 |
placeholder="OpenAI API 토큰을 입력하세요..."
|
159 |
)
|
160 |
|
161 |
+
# --- Tab: 일반 모델 ---
|
162 |
with gr.Tab("일반 모델"):
|
163 |
+
model_name = gr.Radio(
|
164 |
+
choices=list(MODELS.keys()),
|
165 |
+
label="Language Model (HuggingFace)",
|
166 |
+
value="Zephyr 7B Beta"
|
167 |
+
)
|
168 |
+
max_tokens = gr.Slider(minimum=0, maximum=2000, value=500, step=100, label="Max Tokens")
|
169 |
+
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
170 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
171 |
+
system_message = gr.Textbox(
|
172 |
+
value="""반드시 한글로 답변할 것.
|
|
|
|
|
173 |
너는 최고의 비서이다.
|
174 |
내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
|
175 |
""",
|
176 |
+
label="System Message",
|
177 |
+
lines=3
|
178 |
+
)
|
179 |
+
|
180 |
+
question_input = gr.Textbox(label="질문을 입력하세요")
|
181 |
+
answer_output = gr.Textbox(label="답변", interactive=False)
|
182 |
+
|
183 |
+
# '전송' 버튼 클릭 시 HF 모델 QnA 함수 호출
|
184 |
+
submit_button = gr.Button("전송")
|
185 |
+
|
186 |
+
submit_button.click(
|
187 |
+
fn=respond_hf_qna,
|
188 |
+
inputs=[
|
189 |
+
question_input,
|
190 |
+
model_name,
|
191 |
+
max_tokens,
|
192 |
+
temperature,
|
193 |
+
top_p,
|
194 |
+
system_message,
|
195 |
+
hf_token_box
|
196 |
+
],
|
197 |
+
outputs=answer_output
|
198 |
+
)
|
199 |
+
|
200 |
+
# --- Tab: Cohere Command R+ ---
|
|
|
201 |
with gr.Tab("Cohere Command R+"):
|
202 |
+
cohere_system_message = gr.Textbox(
|
203 |
+
value="""반드시 한글로 답변할 것.
|
|
|
204 |
너는 최고의 비서이다.
|
205 |
내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
|
206 |
""",
|
207 |
+
label="System Message",
|
208 |
+
lines=3
|
209 |
+
)
|
210 |
+
cohere_max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens")
|
211 |
+
cohere_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
212 |
+
cohere_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
213 |
+
|
214 |
+
cohere_question_input = gr.Textbox(label="질문을 입력하세요")
|
215 |
+
cohere_answer_output = gr.Textbox(label="답변", interactive=False)
|
216 |
+
|
217 |
+
cohere_submit_button = gr.Button("전송")
|
218 |
+
|
219 |
+
cohere_submit_button.click(
|
220 |
+
fn=respond_cohere_qna,
|
221 |
+
inputs=[
|
222 |
+
cohere_question_input,
|
223 |
+
cohere_system_message,
|
224 |
+
cohere_max_tokens,
|
225 |
+
cohere_temperature,
|
226 |
+
cohere_top_p,
|
227 |
+
hf_token_box
|
228 |
+
],
|
229 |
+
outputs=cohere_answer_output
|
230 |
+
)
|
231 |
+
|
232 |
+
# --- Tab: ChatGPT ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
with gr.Tab("ChatGPT"):
|
234 |
+
chatgpt_system_message = gr.Textbox(
|
235 |
+
value="""반드시 한글로 답변할 것.
|
|
|
236 |
너는 ChatGPT, OpenAI에서 개발한 언어 모델이다.
|
237 |
내가 요구하는 것을 최대한 자세하고 정확하게 답변하라.
|
238 |
""",
|
239 |
+
label="System Message",
|
240 |
+
lines=3
|
241 |
+
)
|
242 |
+
chatgpt_max_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max Tokens")
|
243 |
+
chatgpt_temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
244 |
+
chatgpt_top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
245 |
+
|
246 |
+
chatgpt_question_input = gr.Textbox(label="질문을 입력하세요")
|
247 |
+
chatgpt_answer_output = gr.Textbox(label="답변", interactive=False)
|
248 |
+
|
249 |
+
chatgpt_submit_button = gr.Button("전송")
|
250 |
+
|
251 |
+
chatgpt_submit_button.click(
|
252 |
+
fn=respond_chatgpt_qna,
|
253 |
+
inputs=[
|
254 |
+
chatgpt_question_input,
|
255 |
+
chatgpt_system_message,
|
256 |
+
chatgpt_max_tokens,
|
257 |
+
chatgpt_temperature,
|
258 |
+
chatgpt_top_p,
|
259 |
+
openai_token_box
|
260 |
+
],
|
261 |
+
outputs=chatgpt_answer_output
|
262 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
|
264 |
if __name__ == "__main__":
|
265 |
demo.launch()
|