Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,62 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import os
|
| 4 |
-
from threading import Event
|
| 5 |
-
import re
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
-
"meta-llama/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
|
| 14 |
-
"mistralai/Mixtral-8x7B-Instruct-v0.1": "Mixtral-8x7B-Instruct-v0.1",
|
| 15 |
-
"CohereForAI/c4ai-command-r-plus": "Cohere Command-R Plus"
|
| 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 |
-
def stop_generation():
|
| 71 |
-
stop_event.set()
|
| 72 |
-
|
| 73 |
-
# Gradio UI 구성
|
| 74 |
-
with gr.Blocks() as demo:
|
| 75 |
-
gr.Markdown("# 프롬프트 최적화 Playground")
|
| 76 |
-
|
| 77 |
-
gr.Markdown("""
|
| 78 |
-
**주의사항:**
|
| 79 |
-
- '전송' 버튼을 클릭하거나 입력 필드에서 Shift+Enter를 눌러 메시지를 전송할 수 있습니다.
|
| 80 |
-
- Enter 키는 줄바꿈으로 작동합니다.
|
| 81 |
-
- 입력한 내용에 대해서만 응답하도록 설정되어 있으며, 모델이 추가 질문을 만들거나 입력을 확장하지 않습니다.
|
| 82 |
-
""")
|
| 83 |
-
|
| 84 |
-
with gr.Row():
|
| 85 |
-
with gr.Column(scale=1):
|
| 86 |
-
with gr.Accordion("모델 설정", open=True):
|
| 87 |
-
model = gr.Radio(list(models.keys()), value=list(models.keys())[0], label="언어 모델 선택", info="사용할 언어 모델을 선택하세요")
|
| 88 |
-
max_tokens = gr.Slider(minimum=1, maximum=2000, value=500, step=100, label="최대 새 토큰 수")
|
| 89 |
-
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="온도")
|
| 90 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.90, step=0.05, label="Top-p (핵 샘플링)")
|
| 91 |
-
|
| 92 |
-
system_message = gr.Textbox(
|
| 93 |
-
value="당신은 정확하고 간결한 응답만을 제공하는 AI 어시스턴트입니다. 어떤 경우에도 추가 질문을 하거나 대화를 확장하지 마세요. 오직 주어진 입력에 대해서만 답변하세요.",
|
| 94 |
-
label="시스템 메시지",
|
| 95 |
-
lines=5
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
with gr.Column(scale=2):
|
| 99 |
-
chatbot = gr.Chatbot(height=400, label="대화 결과")
|
| 100 |
-
prompt = gr.Textbox(
|
| 101 |
-
label="내용 입력",
|
| 102 |
-
lines=3,
|
| 103 |
-
placeholder="메시지를 입력하세요. 전송 버튼을 클릭하거나 Shift+Enter를 눌러 전송합니다."
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
with gr.Row():
|
| 107 |
-
send = gr.Button("전송")
|
| 108 |
-
stop = gr.Button("🛑 생성 중단")
|
| 109 |
-
clear = gr.Button("🗑️ 대화 내역 지우기")
|
| 110 |
-
|
| 111 |
-
# Event handlers
|
| 112 |
-
send.click(respond, inputs=[prompt, system_message, max_tokens, temperature, top_p, model], outputs=[chatbot])
|
| 113 |
-
prompt.submit(respond, inputs=[prompt, system_message, max_tokens, temperature, top_p, model], outputs=[chatbot])
|
| 114 |
-
stop.click(stop_generation)
|
| 115 |
-
clear.click(lambda: None, outputs=[chatbot])
|
| 116 |
-
|
| 117 |
-
# UI 실행
|
| 118 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
MODELS = {
|
| 5 |
+
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
| 6 |
+
"DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
|
| 7 |
+
"Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 8 |
+
"Mixtral 8x7B": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 9 |
+
"Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
|
| 12 |
+
def get_client(model_name):
|
| 13 |
+
return InferenceClient(MODELS[model_name])
|
| 14 |
+
|
| 15 |
+
def respond(
|
| 16 |
+
message,
|
| 17 |
+
history: list[tuple[str, str]],
|
| 18 |
+
model_name,
|
| 19 |
+
system_message,
|
| 20 |
+
max_tokens,
|
| 21 |
+
temperature,
|
| 22 |
+
top_p,
|
| 23 |
+
):
|
| 24 |
+
client = get_client(model_name)
|
| 25 |
+
messages = [{"role": "system", "content": system_message}]
|
| 26 |
+
|
| 27 |
+
for val in history:
|
| 28 |
+
if val[0]:
|
| 29 |
+
messages.append({"role": "user", "content": val[0]})
|
| 30 |
+
if val[1]:
|
| 31 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 32 |
+
|
| 33 |
+
messages.append({"role": "user", "content": message})
|
| 34 |
+
|
| 35 |
+
response = ""
|
| 36 |
+
|
| 37 |
+
for message in client.chat_completion(
|
| 38 |
+
messages,
|
| 39 |
+
max_tokens=max_tokens,
|
| 40 |
+
stream=True,
|
| 41 |
+
temperature=temperature,
|
| 42 |
+
top_p=top_p,
|
| 43 |
+
):
|
| 44 |
+
token = message.choices[0].delta.content
|
| 45 |
+
response += token
|
| 46 |
+
yield response
|
| 47 |
+
|
| 48 |
+
demo = gr.ChatInterface(
|
| 49 |
+
respond,
|
| 50 |
+
additional_inputs=[
|
| 51 |
+
gr.Dropdown(choices=list(MODELS.keys()), label="Language Model", value="Zephyr 7B Beta"),
|
| 52 |
+
gr.Textbox(value="You are a friendly and helpful AI assistant.", label="System Message"),
|
| 53 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
|
| 54 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 55 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 56 |
+
],
|
| 57 |
+
title="Advanced AI Chatbot",
|
| 58 |
+
description="Chat with different language models and customize your experience!",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|