Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import os
|
|
| 4 |
from threading import Event
|
| 5 |
|
| 6 |
hf_token = os.getenv("HF_TOKEN")
|
| 7 |
-
|
| 8 |
stop_event = Event()
|
| 9 |
|
| 10 |
def get_model_response(client, messages, max_tokens, temperature, top_p):
|
|
@@ -24,7 +23,6 @@ def get_model_response(client, messages, max_tokens, temperature, top_p):
|
|
| 24 |
if token:
|
| 25 |
full_response += token
|
| 26 |
yield full_response
|
| 27 |
-
stop_event.clear()
|
| 28 |
except Exception as e:
|
| 29 |
yield f"모델 추론 실패: {str(e)}"
|
| 30 |
|
|
@@ -40,18 +38,16 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, se
|
|
| 40 |
response = ""
|
| 41 |
for partial_response in get_model_response(client, messages, max_tokens, temperature, top_p):
|
| 42 |
response = partial_response
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def stop_generation():
|
| 49 |
-
stop_event.set()
|
| 50 |
-
return "생성이 중단되었습니다."
|
| 51 |
|
| 52 |
def continue_writing(history, system_message, max_tokens, temperature, top_p, model):
|
| 53 |
if not history:
|
| 54 |
-
return history
|
| 55 |
last_user_message = history[-1][0]
|
| 56 |
last_assistant_message = history[-1][1]
|
| 57 |
|
|
@@ -63,13 +59,20 @@ def continue_writing(history, system_message, max_tokens, temperature, top_p, mo
|
|
| 63 |
messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in history for i, m in enumerate(h)])
|
| 64 |
messages.append({"role": "user", "content": prompt})
|
| 65 |
|
| 66 |
-
response =
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
models = {
|
| 75 |
"deepseek-ai/DeepSeek-Coder-V2-Instruct": "(한국회사)DeepSeek-Coder-V2-Instruct",
|
|
@@ -110,23 +113,27 @@ with gr.Blocks() as demo:
|
|
| 110 |
model = gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
|
| 111 |
|
| 112 |
# JavaScript를 사용하여 키 입력 동작 커스터마이즈
|
| 113 |
-
msg.
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
"""
|
| 125 |
|
| 126 |
# 이벤트 핸들러 설정
|
| 127 |
-
msg.submit(lambda x: "", inputs=msg, outputs=msg) # 엔터 키 입력 시 아무 동작 안 함
|
| 128 |
send.click(respond, inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
| 129 |
-
|
|
|
|
| 130 |
regenerate.click(lambda h, s, m, t, p, mod: respond(h[-1][0] if h else "", h[:-1], s, m, t, p, mod), inputs=[chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
| 131 |
stop.click(stop_generation, inputs=[], outputs=[msg])
|
| 132 |
clear.click(lambda: (None, None), outputs=[msg, chatbot])
|
|
|
|
| 4 |
from threading import Event
|
| 5 |
|
| 6 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
| 7 |
stop_event = Event()
|
| 8 |
|
| 9 |
def get_model_response(client, messages, max_tokens, temperature, top_p):
|
|
|
|
| 23 |
if token:
|
| 24 |
full_response += token
|
| 25 |
yield full_response
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
yield f"모델 추론 실패: {str(e)}"
|
| 28 |
|
|
|
|
| 38 |
response = ""
|
| 39 |
for partial_response in get_model_response(client, messages, max_tokens, temperature, top_p):
|
| 40 |
response = partial_response
|
| 41 |
+
history.append((message, response))
|
| 42 |
+
yield "", history
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
+
history.append((message, f"오류 발생: {str(e)}"))
|
| 46 |
+
yield "", history
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def continue_writing(history, system_message, max_tokens, temperature, top_p, model):
|
| 49 |
if not history:
|
| 50 |
+
return "", history
|
| 51 |
last_user_message = history[-1][0]
|
| 52 |
last_assistant_message = history[-1][1]
|
| 53 |
|
|
|
|
| 59 |
messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in history for i, m in enumerate(h)])
|
| 60 |
messages.append({"role": "user", "content": prompt})
|
| 61 |
|
| 62 |
+
response = ""
|
| 63 |
+
for partial_response in get_model_response(client, messages, max_tokens, temperature, top_p):
|
| 64 |
+
response = partial_response
|
| 65 |
|
| 66 |
+
continued_response = last_assistant_message + " " + response
|
| 67 |
+
history[-1] = (last_user_message, continued_response)
|
| 68 |
+
return "", history
|
| 69 |
except Exception as e:
|
| 70 |
+
history.append(("시스템", f"계속 작성 중 오류 발생: {str(e)}"))
|
| 71 |
+
return "", history
|
| 72 |
+
|
| 73 |
+
def stop_generation():
|
| 74 |
+
stop_event.set()
|
| 75 |
+
return "생성이 중단되었습니다."
|
| 76 |
|
| 77 |
models = {
|
| 78 |
"deepseek-ai/DeepSeek-Coder-V2-Instruct": "(한국회사)DeepSeek-Coder-V2-Instruct",
|
|
|
|
| 113 |
model = gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
|
| 114 |
|
| 115 |
# JavaScript를 사용하여 키 입력 동작 커스터마이즈
|
| 116 |
+
msg.javascript = """
|
| 117 |
+
(x) => {
|
| 118 |
+
const textbox = document.querySelector("#component-3");
|
| 119 |
+
textbox.addEventListener("keydown", function(e) {
|
| 120 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 121 |
+
e.preventDefault();
|
| 122 |
+
return;
|
| 123 |
+
}
|
| 124 |
+
if (e.key === 'Enter' && e.shiftKey) {
|
| 125 |
+
e.preventDefault();
|
| 126 |
+
document.querySelector("#component-5").click();
|
| 127 |
+
}
|
| 128 |
+
});
|
| 129 |
+
return x;
|
| 130 |
}
|
| 131 |
"""
|
| 132 |
|
| 133 |
# 이벤트 핸들러 설정
|
|
|
|
| 134 |
send.click(respond, inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
| 135 |
+
msg.submit(respond, inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
| 136 |
+
continue_btn.click(continue_writing, inputs=[chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
| 137 |
regenerate.click(lambda h, s, m, t, p, mod: respond(h[-1][0] if h else "", h[:-1], s, m, t, p, mod), inputs=[chatbot, system_message, max_tokens, temperature, top_p, model], outputs=[msg, chatbot])
|
| 138 |
stop.click(stop_generation, inputs=[], outputs=[msg])
|
| 139 |
clear.click(lambda: (None, None), outputs=[msg, chatbot])
|