Dawoodthouseef commited on
Commit
e7703db
β€’
1 Parent(s): 7933d4c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from gradio_client import Client
3
+ from time import sleep
4
+ # Constants
5
+ TITLE = "Mistrial 7B Chatbot"
6
+ DESCRIPTION = """
7
+ This Space demonstrates model [Mistrial-7b-]
8
+ """
9
+
10
+ # Initialize client
11
+
12
+
13
+ with st.sidebar:
14
+ # system_promptSide = st.text_input("Optional system prompt:")
15
+ temperatureSide = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
16
+ max_new_tokensSide = st.slider("Max new tokens", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0)
17
+ # ToppSide = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.6, step=0.05)
18
+ # RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)
19
+
20
+ # Load the model
21
+ model = AutoModelForCausalLM.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-GGUF", model_file="mistral-7b-instruct-v0.1.Q5_K_S.gguf", model_type="mistral", gpu_layers=0)
22
+ ins = '''[INST] <<SYS>>
23
+ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
24
+ 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.
25
+ <</SYS>>
26
+ {} [/INST]
27
+ '''
28
+ # Define the conversation history
29
+ conversation_history = []
30
+
31
+ # Prediction function
32
+ def predict(message, system_prompt='', temperature=0.7, max_new_tokens=4096,Topp=0.5,Repetitionpenalty=1.2):
33
+ with st.status("Starting client"):
34
+ sleep(2)
35
+ st.write("Requesting client")
36
+ with st.status("Requesting LLama-2"):
37
+ st.write("Requesting API")
38
+ global conversation_history
39
+ # Append the user's input to the conversation history
40
+ conversation_history.append({"role": "system", "content": input_text})
41
+ response_text = model(ins.format(question))
42
+ conversation_history.append({"role": "user", "content": input_text})
43
+ conversation_history.append({"role": "assistant", "content": response_text})
44
+ return response_text
45
+
46
+ # Streamlit UI
47
+ st.title(TITLE)
48
+ st.write(DESCRIPTION)
49
+
50
+
51
+ if "messages" not in st.session_state:
52
+ st.session_state.messages = []
53
+
54
+ # Display chat messages from history on app rerun
55
+ for message in st.session_state.messages:
56
+ with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else 'πŸ¦™')):
57
+ st.markdown(message["content"])
58
+
59
+ # React to user input
60
+ if prompt := st.chat_input("Ask Mistril-7b anything..."):
61
+ # Display user message in chat message container
62
+ st.chat_message("human",avatar = "πŸ§‘β€πŸ’»").markdown(prompt)
63
+ # Add user message to chat history
64
+ st.session_state.messages.append({"role": "human", "content": prompt})
65
+
66
+ response = predict(message=prompt)#, temperature= temperatureSide,max_new_tokens=max_new_tokensSide)
67
+ # Display assistant response in chat message container
68
+ with st.chat_message("assistant", avatar='πŸ¦™'):
69
+ st.markdown(response)
70
+ # Add assistant response to chat history
71
+ st.session_state.messages.append({"role": "assistant", "content": response})