Update app.py
Browse filesFallback to Gradio
app.py
CHANGED
@@ -1,125 +1,71 @@
|
|
1 |
-
import
|
2 |
import os
|
3 |
|
4 |
-
from typing import Iterator
|
5 |
from huggingface_hub import InferenceClient
|
6 |
|
|
|
|
|
|
|
|
|
7 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
8 |
|
9 |
-
# Configure page settings
|
10 |
-
st.set_page_config(
|
11 |
-
page_title="LLM Taiwan Chat",
|
12 |
-
page_icon="💬",
|
13 |
-
layout="centered"
|
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 |
stream=True,
|
43 |
-
temperature=
|
44 |
-
top_p=
|
45 |
-
)
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
# Advanced options in expander
|
67 |
-
with st.expander("進階選項 ⚙️", expanded=False):
|
68 |
-
# System prompt input
|
69 |
-
system_prompt = st.text_area(
|
70 |
-
"System Prompt 設定:",
|
71 |
-
value=st.session_state.system_prompt,
|
72 |
-
help="設定 system prompt 來定義 AI 助理的行為和角色。開始對話後將無法修改。",
|
73 |
-
height=100,
|
74 |
-
disabled=len(st.session_state.messages) > 0 # 當有對話時設為唯讀
|
75 |
-
)
|
76 |
-
if not st.session_state.messages and system_prompt != st.session_state.system_prompt:
|
77 |
-
st.session_state.system_prompt = system_prompt
|
78 |
-
|
79 |
-
st.session_state.temperature = st.slider(
|
80 |
-
"Temperature",
|
81 |
-
min_value=0.0,
|
82 |
-
max_value=2.0,
|
83 |
-
value=st.session_state.temperature,
|
84 |
-
step=0.1,
|
85 |
-
help="較高的值會使輸出更加隨機,較低的值會使其更加集中和確定。"
|
86 |
-
)
|
87 |
-
st.session_state.top_p = st.slider(
|
88 |
-
"Top P",
|
89 |
-
min_value=0.1,
|
90 |
-
max_value=1.0,
|
91 |
-
value=st.session_state.top_p,
|
92 |
step=0.05,
|
93 |
-
|
94 |
-
)
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
with st.chat_message(message["role"]):
|
99 |
-
st.write(message["content"])
|
100 |
-
|
101 |
-
# Chat input
|
102 |
-
if prompt := st.chat_input("輸入您的訊息..."):
|
103 |
-
# Add user message to chat history
|
104 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
105 |
-
|
106 |
-
# Display user message
|
107 |
-
with st.chat_message("user"):
|
108 |
-
st.write(prompt)
|
109 |
-
|
110 |
-
# Display assistant response with streaming
|
111 |
-
with st.chat_message("assistant"):
|
112 |
-
response_placeholder = st.empty()
|
113 |
-
full_response = ""
|
114 |
-
|
115 |
-
# Stream the response
|
116 |
-
for response_chunk in stream_chat(prompt):
|
117 |
-
full_response += response_chunk
|
118 |
-
response_placeholder.markdown(full_response + "▌")
|
119 |
-
response_placeholder.markdown(full_response)
|
120 |
-
|
121 |
-
# Add assistant response to chat history
|
122 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import os
|
3 |
|
|
|
4 |
from huggingface_hub import InferenceClient
|
5 |
|
6 |
+
"""
|
7 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
8 |
+
"""
|
9 |
+
|
10 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
### Model used changes from HuggingFaceH4/zephyr-7b-beta to meta-llama/Llama-3.2-3B-Instruct
|
14 |
+
client = InferenceClient("lianghsun/Llama-3.2-Taiwan-3B", timeout=30, token=HF_TOKEN)
|
15 |
+
|
16 |
+
|
17 |
+
def respond(
|
18 |
+
message,
|
19 |
+
history: list[tuple[str, str]],
|
20 |
+
system_message,
|
21 |
+
max_tokens,
|
22 |
+
temperature,
|
23 |
+
top_p,
|
24 |
+
):
|
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 |
+
|
46 |
+
response += token
|
47 |
+
yield response
|
48 |
+
|
49 |
+
|
50 |
+
"""
|
51 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
52 |
+
"""
|
53 |
+
demo = gr.ChatInterface(
|
54 |
+
respond,
|
55 |
+
additional_inputs=[
|
56 |
+
gr.Textbox(value="你是一個產自台灣的聊天機械人, 你以台灣本地人的身份, 使用正體中文回答問題.", label="System message"),
|
57 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
58 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
59 |
+
gr.Slider(
|
60 |
+
minimum=0.1,
|
61 |
+
maximum=1.0,
|
62 |
+
value=0.95,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
step=0.05,
|
64 |
+
label="Top-p (nucleus sampling)",
|
65 |
+
),
|
66 |
+
],
|
67 |
+
)
|
68 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
+
demo.launch()
|