Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,2 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
MODELS = {
|
| 6 |
-
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
| 7 |
-
"DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
|
| 8 |
-
"Meta Llama 3.1 8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 9 |
-
"Mixtral 8x7B": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 10 |
-
"Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
def get_client(model_name):
|
| 14 |
-
model_id = MODELS[model_name]
|
| 15 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 16 |
-
if not hf_token:
|
| 17 |
-
raise ValueError("HF_TOKEN environment variable is required")
|
| 18 |
-
return InferenceClient(model_id, token=hf_token)
|
| 19 |
-
|
| 20 |
-
def respond(
|
| 21 |
-
message,
|
| 22 |
-
chat_history,
|
| 23 |
-
model_name,
|
| 24 |
-
max_tokens,
|
| 25 |
-
temperature,
|
| 26 |
-
top_p,
|
| 27 |
-
system_message,
|
| 28 |
-
):
|
| 29 |
-
try:
|
| 30 |
-
client = get_client(model_name)
|
| 31 |
-
except ValueError as e:
|
| 32 |
-
chat_history.append((message, str(e)))
|
| 33 |
-
return chat_history
|
| 34 |
-
|
| 35 |
-
messages = [{"role": "system", "content": system_message}]
|
| 36 |
-
for human, assistant in chat_history:
|
| 37 |
-
messages.append({"role": "user", "content": human})
|
| 38 |
-
messages.append({"role": "assistant", "content": assistant})
|
| 39 |
-
messages.append({"role": "user", "content": message})
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
if "Cohere" in model_name:
|
| 43 |
-
# Cohere 모델을 위한 비스트리밍 처리
|
| 44 |
-
response = client.chat_completion(
|
| 45 |
-
messages,
|
| 46 |
-
max_tokens=max_tokens,
|
| 47 |
-
temperature=temperature,
|
| 48 |
-
top_p=top_p,
|
| 49 |
-
)
|
| 50 |
-
assistant_message = response.choices[0].message.content
|
| 51 |
-
chat_history.append((message, assistant_message))
|
| 52 |
-
yield chat_history
|
| 53 |
-
else:
|
| 54 |
-
# 다른 모델들을 위한 스트리밍 처리
|
| 55 |
-
stream = client.chat_completion(
|
| 56 |
-
messages,
|
| 57 |
-
max_tokens=max_tokens,
|
| 58 |
-
temperature=temperature,
|
| 59 |
-
top_p=top_p,
|
| 60 |
-
stream=True,
|
| 61 |
-
)
|
| 62 |
-
partial_message = ""
|
| 63 |
-
for response in stream:
|
| 64 |
-
if response.choices[0].delta.content is not None:
|
| 65 |
-
partial_message += response.choices[0].delta.content
|
| 66 |
-
if len(chat_history) > 0 and chat_history[-1][0] == message:
|
| 67 |
-
chat_history[-1] = (message, partial_message)
|
| 68 |
-
else:
|
| 69 |
-
chat_history.append((message, partial_message))
|
| 70 |
-
yield chat_history
|
| 71 |
-
except Exception as e:
|
| 72 |
-
error_message = f"An error occurred: {str(e)}"
|
| 73 |
-
chat_history.append((message, error_message))
|
| 74 |
-
yield chat_history
|
| 75 |
-
|
| 76 |
-
def clear_conversation():
|
| 77 |
-
return []
|
| 78 |
-
|
| 79 |
-
with gr.Blocks() as demo:
|
| 80 |
-
gr.Markdown("# Prompting AI Chatbot")
|
| 81 |
-
gr.Markdown("언어모델별 프롬프트 테스트 챗봇입니다.")
|
| 82 |
-
|
| 83 |
-
with gr.Row():
|
| 84 |
-
with gr.Column(scale=1):
|
| 85 |
-
model_name = gr.Radio(
|
| 86 |
-
choices=list(MODELS.keys()),
|
| 87 |
-
label="Language Model",
|
| 88 |
-
value="Zephyr 7B Beta"
|
| 89 |
-
)
|
| 90 |
-
max_tokens = gr.Slider(minimum=0, maximum=2000, value=500, step=100, label="Max Tokens")
|
| 91 |
-
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.05, label="Temperature")
|
| 92 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
| 93 |
-
system_message = gr.Textbox(
|
| 94 |
-
value="""반드시 한글로 답변할 것.
|
| 95 |
-
너는 최고의 비서이다.
|
| 96 |
-
내가 요구하는것들을 최대한 자세하고 정확하게 답변하라.
|
| 97 |
-
""",
|
| 98 |
-
label="System Message",
|
| 99 |
-
lines=3
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
with gr.Column(scale=2):
|
| 103 |
-
chatbot = gr.Chatbot()
|
| 104 |
-
msg = gr.Textbox(label="메세지를 입력하세요")
|
| 105 |
-
with gr.Row():
|
| 106 |
-
submit_button = gr.Button("전송")
|
| 107 |
-
clear_button = gr.Button("대화 내역 지우기")
|
| 108 |
-
|
| 109 |
-
msg.submit(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
|
| 110 |
-
submit_button.click(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
|
| 111 |
-
clear_button.click(clear_conversation, outputs=chatbot, queue=False)
|
| 112 |
-
|
| 113 |
-
if __name__ == "__main__":
|
| 114 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|