Spaces:
Sleeping
Sleeping
File size: 5,187 Bytes
9c880cb 5bdf9aa b4dff1d 5bdf9aa b4dff1d 32957d4 48a4d38 871126f 32957d4 871126f 32957d4 48a4d38 871126f 48a4d38 32957d4 a5db718 871126f a5db718 b4dff1d 32957d4 b4dff1d 871126f 48a4d38 871126f 5bdf9aa 48a4d38 9c880cb 844d195 48a4d38 844d195 48a4d38 b16cf8b 844d195 c1e9c0c 01c8295 32957d4 9a9e197 32957d4 1e06dbb 6c72519 32957d4 6c72519 93b41fc 32957d4 960780c b4dff1d 93b41fc 6c72519 32957d4 6c72519 c04ac55 6c72519 32957d4 b16cf8b 32957d4 9c880cb 5bdf9aa 960780c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import gradio as gr
from huggingface_hub import InferenceClient
import os
from threading import Event
hf_token = os.getenv("HF_TOKEN")
stop_event = Event()
models = {
"deepseek-ai/DeepSeek-Coder-V2-Instruct": "(한국회사)DeepSeek-Coder-V2-Instruct",
"meta-llama/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
"mistralai/Mixtral-8x7B-Instruct-v0.1": "Mixtral-8x7B-Instruct-v0.1",
"CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus"
}
def get_client(model):
return InferenceClient(model=model, token=hf_token)
MAX_HISTORY_LENGTH = 5 # 히스토리에 유지할 최대 대화 수
def truncate_history(history):
return history[-MAX_HISTORY_LENGTH:] if len(history) > MAX_HISTORY_LENGTH else history
def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
stop_event.clear()
client = InferenceClient(model=selected_model, token=hf_token)
truncated_history = truncate_history(history)
messages = [{"role": "system", "content": system_message + "\n사용자의 입력에만 직접적으로 답변하세요. 추가 질문을 생성하거나 사용자의 입력을 확장하지 마세요."}]
messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in truncated_history for i, m in enumerate(h) if m])
messages.append({"role": "user", "content": message})
try:
response = ""
for chunk in client.text_generation(
prompt="\n".join([f"{m['role']}: {m['content']}" for m in messages]),
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
stream=True
):
if stop_event.is_set():
break
if chunk:
response += chunk
if response.startswith(message):
response = response[len(message):].lstrip()
yield truncated_history + [(message, response)]
except Exception as e:
yield truncated_history + [(message, f"오류 발생: {str(e)}")]
def continue_writing(message, history, system_message, max_tokens, temperature, top_p, selected_model):
if not history:
yield [("시스템", "대화 내역이 없습니다.")]
return
truncated_history = truncate_history(history)
last_assistant_message = truncated_history[-1][1]
prompt = f"이전 대화를 간단히 요약하고 이어서 작성해주세요. 마지막 응답: {last_assistant_message[:100]}..."
for response in respond(prompt, truncated_history[:-1], system_message, max_tokens, temperature, top_p, selected_model):
yield response
def stop_generation():
stop_event.set()
return "생성이 중단되었습니다."
def regenerate(chat_history, system_message, max_tokens, temperature, top_p, selected_model):
if not chat_history:
return "대화 내역이 없습니다."
last_user_message = chat_history[-1][0]
return respond(last_user_message, chat_history[:-1], system_message, max_tokens, temperature, top_p, selected_model)
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox(label="메시지 입력", placeholder="메시지를 입력하세요. Enter로 전송, Shift+Enter로 줄바꿈")
with gr.Row():
send = gr.Button("전송")
continue_btn = gr.Button("계속 작성")
regenerate_btn = gr.Button("🔄 재생성")
stop = gr.Button("🛑 생성 중단")
clear = gr.Button("🗑️ 대화 내역 지우기")
with gr.Accordion("추가 설정", open=True):
system_message = gr.Textbox(
value="너는 나의 최고의 비서이다.\n내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.\n반드시 한글로 답변할것.",
label="시스템 메시지",
lines=5
)
max_tokens = gr.Slider(minimum=1, maximum=2000, value=500, step=100, label="최대 새 토큰 수")
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="온도")
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.90, step=0.05, label="Top-p (핵 샘플링)")
model = gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
# Event handlers
msg.submit(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p, model], [chatbot])
send.click(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p, model], [chatbot])
continue_btn.click(continue_writing,
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, model],
outputs=[chatbot])
regenerate_btn.click(regenerate, [chatbot, system_message, max_tokens, temperature, top_p, model], [chatbot])
stop.click(stop_generation, outputs=[msg])
clear.click(lambda: None, outputs=[chatbot])
if __name__ == "__main__":
if not hf_token:
print("경고: HF_TOKEN 환경 변수가 설정되지 않았습니다. 일부 모델에 접근할 수 없을 수 있습니다.")
demo.launch() |