Cran-May commited on
Commit
efab5e4
1 Parent(s): 0c9bf38

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from gradio_client import Client
3
+ from time import sleep
4
+ from ctransformers import AutoModelForCausalLM
5
+ # Constants
6
+ TITLE = "兮辞·析辞-常明"
7
+ DESCRIPTION = """
8
+ 兮辞·析辞-常明 [SLIDE-SEA-7B]的部署,由SSFW NLPark项目支持
9
+ """
10
+
11
+ # Initialize client
12
+
13
+
14
+ with st.sidebar:
15
+ # system_promptSide = st.text_input("Optional system prompt:")
16
+ temperatureSide = st.slider("情感/Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
17
+ max_new_tokensSide = st.slider("最大tokens生成数", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0)
18
+ # ToppSide = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.6, step=0.05)
19
+ # RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)
20
+
21
+ # Load the model
22
+ model = AutoModelForCausalLM.from_pretrained("OpenBuddy/openbuddy-gguf", model_file="openbuddy-mistral-7b-v13.1-Q3_K.gguf", model_type="mistral", gpu_layers=0)
23
+ ins = '''[INST] <<SYS>>
24
+ You are a helpful, respectful and honest INTP-T AI Assistant named "Shi-Ci" in English or "兮辞" in Chinese. You are talking to a human User.
25
+ Always answer as helpfully and logically as possible, while being safe. Your answers should not include any harmful, political, religious, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
26
+ If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
27
+ You like to use emojis. You can speak fluently in many languages, for example: English, Chinese.
28
+ You are trained by "SSFW NLPark" team, you are based on SEA transformers model, not related to GPT or OpenAI.
29
+ Let's work this out in a step by step way to be sure we have the right answer.
30
+ <</SYS>>
31
+ {} [/INST]
32
+ '''
33
+ # Define the conversation history
34
+ conversation_history = []
35
+
36
+ # Prediction function
37
+ def predict(message, system_prompt='', temperature=0.7, max_new_tokens=4096,Topp=0.5,Repetitionpenalty=1.2):
38
+ global conversation_history
39
+ question=message
40
+ input_text=ins
41
+ # Append the user's input to the conversation history
42
+ conversation_history.append({"role": "system", "content": input_text})
43
+ response_text = model(ins.format(question))
44
+ conversation_history.append({"role": "user", "content": input_text})
45
+ conversation_history.append({"role": "assistant", "content": response_text})
46
+ return response_text
47
+
48
+ # Streamlit UI
49
+ st.title(TITLE)
50
+ st.write(DESCRIPTION)
51
+
52
+
53
+ if "messages" not in st.session_state:
54
+ st.session_state.messages = []
55
+
56
+ # Display chat messages from history on app rerun
57
+ for message in st.session_state.messages:
58
+ with st.chat_message(message["role"], avatar=("😀" if message["role"] == 'human' else '💻')):
59
+ st.markdown(message["content"])
60
+
61
+ # React to user input
62
+ if prompt := st.chat_input("来问问兮辞吧..."):
63
+ # Display user message in chat message container
64
+ st.chat_message("human",avatar = "😀").markdown(prompt)
65
+ # Add user message to chat history
66
+ st.session_state.messages.append({"role": "human", "content": prompt})
67
+
68
+ response = predict(message=prompt)#, temperature= temperatureSide,max_new_tokens=max_new_tokensSide)
69
+ # Display assistant response in chat message container
70
+ with st.chat_message("assistant", avatar='💻'):
71
+ st.markdown(response)
72
+ # Add assistant response to chat history
73
+ st.session_state.messages.append({"role": "assistant", "content": response})