Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,104 +6,69 @@ from threading import Event
|
|
6 |
hf_token = os.getenv("HF_TOKEN")
|
7 |
stop_event = Event()
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
try:
|
11 |
-
response =
|
12 |
-
|
13 |
-
|
|
|
14 |
temperature=temperature,
|
15 |
top_p=top_p,
|
16 |
stream=True
|
17 |
-
)
|
18 |
-
full_response = ""
|
19 |
-
for message in response:
|
20 |
if stop_event.is_set():
|
21 |
break
|
22 |
-
if
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
if token:
|
27 |
-
full_response += token
|
28 |
-
yield full_response
|
29 |
-
except Exception as e:
|
30 |
-
yield f"모델 추론 실패: {str(e)}"
|
31 |
-
|
32 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
|
33 |
-
stop_event.clear()
|
34 |
-
try:
|
35 |
-
client = InferenceClient(model=selected_model, token=hf_token)
|
36 |
-
|
37 |
-
messages = [{"role": "system", "content": system_message}]
|
38 |
-
messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in history for i, m in enumerate(h) if m])
|
39 |
-
messages.append({"role": "user", "content": message})
|
40 |
-
|
41 |
-
history.append((message, ""))
|
42 |
-
for response in get_model_response(client, messages, max_tokens, temperature, top_p):
|
43 |
-
history[-1] = (message, response)
|
44 |
-
yield "", history
|
45 |
-
|
46 |
except Exception as e:
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
def continue_writing(
|
51 |
-
if not
|
52 |
-
return "",
|
53 |
-
last_user_message =
|
54 |
-
last_assistant_message =
|
55 |
-
|
56 |
prompt = f"이전 대화를 계속 이어서 작성해주세요. 이전 응답: {last_assistant_message}"
|
57 |
-
|
58 |
-
try:
|
59 |
-
client = InferenceClient(model=model, token=hf_token)
|
60 |
-
messages = [{"role": "system", "content": system_message}]
|
61 |
-
messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in history for i, m in enumerate(h)])
|
62 |
-
messages.append({"role": "user", "content": prompt})
|
63 |
-
|
64 |
-
for response in get_model_response(client, messages, max_tokens, temperature, top_p):
|
65 |
-
continued_response = last_assistant_message + " " + response
|
66 |
-
history[-1] = (last_user_message, continued_response)
|
67 |
-
yield "", history
|
68 |
-
except Exception as e:
|
69 |
-
history.append(("시스템", f"계속 작성 중 오류 발생: {str(e)}"))
|
70 |
-
yield "", history
|
71 |
|
72 |
def stop_generation():
|
73 |
stop_event.set()
|
74 |
return "생성이 중단되었습니다."
|
75 |
|
76 |
-
def regenerate(
|
77 |
-
if not
|
78 |
-
return "",
|
79 |
-
last_user_message =
|
80 |
-
|
81 |
-
for _, updated_history in respond(last_user_message, new_history, system_message, max_tokens, temperature, top_p, model):
|
82 |
-
yield "", updated_history
|
83 |
-
|
84 |
-
models = {
|
85 |
-
"deepseek-ai/DeepSeek-Coder-V2-Instruct": "(한국회사)DeepSeek-Coder-V2-Instruct",
|
86 |
-
"meta-llama/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
|
87 |
-
"mistralai/Mixtral-8x7B-Instruct-v0.1": "Mixtral-8x7B-Instruct-v0.1",
|
88 |
-
"CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus"
|
89 |
-
}
|
90 |
|
91 |
with gr.Blocks() as demo:
|
92 |
-
chatbot = gr.Chatbot(
|
93 |
-
|
94 |
-
with gr.Row():
|
95 |
-
msg = gr.Textbox(
|
96 |
-
scale=4,
|
97 |
-
label="메시지 입력",
|
98 |
-
lines=3,
|
99 |
-
placeholder="메시지를 입력하세요. Enter로 줄바꿈, Shift+Enter로 전송"
|
100 |
-
)
|
101 |
-
|
102 |
-
with gr.Row():
|
103 |
-
send = gr.Button("전송", scale=1)
|
104 |
-
continue_btn = gr.Button("계속 작성", scale=1)
|
105 |
|
106 |
with gr.Row():
|
|
|
|
|
107 |
regenerate_btn = gr.Button("🔄 재생성")
|
108 |
stop = gr.Button("🛑 생성 중단")
|
109 |
clear = gr.Button("🗑️ 대화 내역 지우기")
|
@@ -112,35 +77,20 @@ with gr.Blocks() as demo:
|
|
112 |
system_message = gr.Textbox(
|
113 |
value="너는 나의 최고의 비서이다.\n내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.\n반드시 한글로 답변할것.",
|
114 |
label="시스템 메시지",
|
115 |
-
lines=
|
116 |
)
|
117 |
max_tokens = gr.Slider(minimum=1, maximum=2000, value=500, step=100, label="최대 새 토큰 수")
|
118 |
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="온도")
|
119 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.90, step=0.05, label="Top-p (핵 샘플링)")
|
120 |
model = gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
e.preventDefault();
|
130 |
-
const sendButton = document.getElementById('component-5');
|
131 |
-
if (sendButton) sendButton.click();
|
132 |
-
}
|
133 |
-
});
|
134 |
-
return x;
|
135 |
-
}
|
136 |
-
"""
|
137 |
-
|
138 |
-
send.click(respond, inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
139 |
-
msg.submit(respond, inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
140 |
-
continue_btn.click(continue_writing, inputs=[chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
141 |
-
regenerate_btn.click(regenerate, inputs=[chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
142 |
-
stop.click(stop_generation, inputs=[], outputs=[msg])
|
143 |
-
clear.click(lambda: (None, None), outputs=[msg, chatbot])
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
if not hf_token:
|
|
|
6 |
hf_token = os.getenv("HF_TOKEN")
|
7 |
stop_event = Event()
|
8 |
|
9 |
+
models = {
|
10 |
+
"deepseek-ai/DeepSeek-Coder-V2-Instruct": "(한국회사)DeepSeek-Coder-V2-Instruct",
|
11 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
|
12 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1": "Mixtral-8x7B-Instruct-v0.1",
|
13 |
+
"CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus"
|
14 |
+
}
|
15 |
+
|
16 |
+
def get_client(model):
|
17 |
+
return InferenceClient(model=model, token=hf_token)
|
18 |
+
|
19 |
+
def respond(message, chat_history, system_message, max_tokens, temperature, top_p, selected_model):
|
20 |
+
stop_event.clear()
|
21 |
+
client = get_client(selected_model)
|
22 |
+
|
23 |
+
messages = [{"role": "system", "content": system_message}]
|
24 |
+
messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in chat_history for i, m in enumerate(h) if m])
|
25 |
+
messages.append({"role": "user", "content": message})
|
26 |
+
|
27 |
try:
|
28 |
+
response = ""
|
29 |
+
for chunk in client.text_generation(
|
30 |
+
prompt="\n".join([f"{m['role']}: {m['content']}" for m in messages]),
|
31 |
+
max_new_tokens=max_tokens,
|
32 |
temperature=temperature,
|
33 |
top_p=top_p,
|
34 |
stream=True
|
35 |
+
):
|
|
|
|
|
36 |
if stop_event.is_set():
|
37 |
break
|
38 |
+
if chunk:
|
39 |
+
response += chunk
|
40 |
+
chat_history.append((message, response))
|
41 |
+
yield chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except Exception as e:
|
43 |
+
error_message = f"오류 발생: {str(e)}"
|
44 |
+
chat_history.append((message, error_message))
|
45 |
+
yield chat_history
|
46 |
|
47 |
+
def continue_writing(message, chat_history, system_message, max_tokens, temperature, top_p, selected_model):
|
48 |
+
if not chat_history:
|
49 |
+
return [("시스템", "대화 내역이 없습니다.")]
|
50 |
+
last_user_message = chat_history[-1][0]
|
51 |
+
last_assistant_message = chat_history[-1][1]
|
|
|
52 |
prompt = f"이전 대화를 계속 이어서 작성해주세요. 이전 응답: {last_assistant_message}"
|
53 |
+
return respond(prompt, chat_history, system_message, max_tokens, temperature, top_p, selected_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
def stop_generation():
|
56 |
stop_event.set()
|
57 |
return "생성이 중단되었습니다."
|
58 |
|
59 |
+
def regenerate(chat_history, system_message, max_tokens, temperature, top_p, selected_model):
|
60 |
+
if not chat_history:
|
61 |
+
return [("시스템", "대화 내역이 없습니다.")]
|
62 |
+
last_user_message = chat_history[-1][0]
|
63 |
+
return respond(last_user_message, chat_history[:-1], system_message, max_tokens, temperature, top_p, selected_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
with gr.Blocks() as demo:
|
66 |
+
chatbot = gr.Chatbot()
|
67 |
+
msg = gr.Textbox(label="메시지 입력", placeholder="메시지를 입력하세요. Enter로 전송, Shift+Enter로 줄바꿈")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
with gr.Row():
|
70 |
+
send = gr.Button("전송")
|
71 |
+
continue_btn = gr.Button("계속 작성")
|
72 |
regenerate_btn = gr.Button("🔄 재생성")
|
73 |
stop = gr.Button("🛑 생성 중단")
|
74 |
clear = gr.Button("🗑️ 대화 내역 지우기")
|
|
|
77 |
system_message = gr.Textbox(
|
78 |
value="너는 나의 최고의 비서이다.\n내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.\n반드시 한글로 답변할것.",
|
79 |
label="시스템 메시지",
|
80 |
+
lines=5
|
81 |
)
|
82 |
max_tokens = gr.Slider(minimum=1, maximum=2000, value=500, step=100, label="최대 새 토큰 수")
|
83 |
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="온도")
|
84 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.90, step=0.05, label="Top-p (핵 샘플링)")
|
85 |
model = gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
|
86 |
|
87 |
+
# Event handlers
|
88 |
+
msg.submit(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p, model], [chatbot])
|
89 |
+
send.click(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p, model], [chatbot])
|
90 |
+
continue_btn.click(continue_writing, [msg, chatbot, system_message, max_tokens, temperature, top_p, model], [chatbot])
|
91 |
+
regenerate_btn.click(regenerate, [chatbot, system_message, max_tokens, temperature, top_p, model], [chatbot])
|
92 |
+
stop.click(stop_generation, outputs=[msg])
|
93 |
+
clear.click(lambda: None, outputs=[chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
if not hf_token:
|