Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="ChatXu-Int4")
|
| 6 |
+
st.title("ChatXu-Int4")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def init_model():
|
| 11 |
+
model_id = 'xuqinyang/chatxu-ggml'
|
| 12 |
+
from huggingface_hub import snapshot_download,hf_hub_download
|
| 13 |
+
#旧
|
| 14 |
+
#snapshot_download(model_id, local_dir="./",revision="7f71a8abefa7b2eede3f74ce0564abe5fbe6874a")
|
| 15 |
+
snapshot_download(model_id, local_dir="./")
|
| 16 |
+
from llama_cpp import Llama
|
| 17 |
+
llm = Llama(model_path="./ggml-model-q4_0.bin", n_ctx=4096,seed=-1)
|
| 18 |
+
|
| 19 |
+
return llm
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def clear_chat_history():
|
| 23 |
+
del st.session_state.messages
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def init_chat_history():
|
| 27 |
+
with st.chat_message("assistant", avatar='🤖'):
|
| 28 |
+
st.markdown("你好,我是ChatXu。")
|
| 29 |
+
|
| 30 |
+
if "messages" in st.session_state:
|
| 31 |
+
for message in st.session_state.messages:
|
| 32 |
+
avatar = '🧑💻' if message["role"] == "user" else '🤖'
|
| 33 |
+
with st.chat_message(message["role"], avatar=avatar):
|
| 34 |
+
st.markdown(message["content"])
|
| 35 |
+
else:
|
| 36 |
+
st.session_state.messages = []
|
| 37 |
+
|
| 38 |
+
return st.session_state.messages
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def main():
|
| 42 |
+
llm = init_model()
|
| 43 |
+
messages = init_chat_history()
|
| 44 |
+
|
| 45 |
+
if prompt := st.chat_input("Shift + Enter 换行, Enter 发送"):
|
| 46 |
+
with st.chat_message("user", avatar='🧑💻'):
|
| 47 |
+
st.markdown(prompt)
|
| 48 |
+
messages.append({"role": "user", "content": prompt})
|
| 49 |
+
print(f"[user] {prompt}", flush=True)
|
| 50 |
+
with st.chat_message("assistant", avatar='🤖'):
|
| 51 |
+
placeholder = st.empty()
|
| 52 |
+
response = ''
|
| 53 |
+
for responses in llm.create_chat_completion(messages,stop=["</s>"],stream=True,max_tokens=-1,temperature=0.3,top_k=5,top_p=0.85,repeat_penalty=1.1):
|
| 54 |
+
if "content" in responses["choices"][0]["delta"]:
|
| 55 |
+
response+=responses["choices"][0]["delta"]["content"]
|
| 56 |
+
placeholder.markdown(response + "▌")
|
| 57 |
+
placeholder.markdown(response)
|
| 58 |
+
messages.append({"role": "assistant", "content": response})
|
| 59 |
+
print(json.dumps(messages, ensure_ascii=False), flush=True)
|
| 60 |
+
|
| 61 |
+
st.button("清空对话", on_click=clear_chat_history)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|