affine commited on
Commit
2e2c01e
1 Parent(s): 8db8330

Local chatgpt app file add

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.llms import HuggingFacePipeline, LlamaCpp,CTransformers
2
+ # from langchain.callbacks.base import BaseCallbackHandler
3
+ import streamlit as st
4
+ from streamlit.components.v1 import html
5
+ import streamlit.components.v1 as components
6
+ from streamlit_chat import message
7
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
8
+ from langchain.callbacks import StreamlitCallbackHandler
9
+ # st_callback = StreamlitCallbackHandler(st.container())
10
+ import textwrap
11
+
12
+ st.title("Affine-LocalGPT")
13
+
14
+ history=[]
15
+
16
+ # Default Sys Prompt
17
+ DEFAULT_SYSTEM_PROMPT = "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. 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."
18
+
19
+ with st.sidebar:
20
+ model_name=st.selectbox("Select Model :-",['Llama 7B','Llama 13B'])
21
+ temperature=st.slider("Temperature :-",0.0,1.0,0.1)
22
+ top_p=st.slider("top_p :-",0.0,1.0,0.95)
23
+ top_k=st.slider("top_k :- ",0,100,50)
24
+ DEFAULT_SYSTEM_PROMPT=st.text_area("System Prompt :-",f"{DEFAULT_SYSTEM_PROMPT}",height=400)
25
+
26
+ # Load the selected model
27
+ if model_name=="Llama 7B":
28
+ print("Llama 7B model Loading")
29
+ model_path='llama-2-7b-chat.ggmlv3.q4_0.bin'
30
+ else:
31
+ print("Llama 13B model Loading")
32
+ model_path="llama-2-13b-chat.ggmlv3.q2_K.bin"
33
+
34
+ # prompt special tokens
35
+ B_INST, E_INST = "[INST]", "[/INST]"
36
+ B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
37
+
38
+
39
+ # create the custom prompt
40
+ def get_prompt(
41
+ message: str, chat_history: list[tuple[str, str]], system_prompt: str
42
+ ) -> str:
43
+ texts = [f"[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n"]
44
+ for user_input, response in chat_history:
45
+ texts.append(f"{user_input.strip()} [/INST] {response.strip()} </s><s> [INST] ")
46
+ texts.append(f"{message.strip()} [/INST]")
47
+ return "".join(texts)
48
+
49
+ ## Load the Local Llama 2 model
50
+ def llama_model(model_path=None,model_type=None,max_new_tokens=None,temperature=None):
51
+ llm = CTransformers(
52
+ model = model_path,
53
+ model_type="llama",
54
+ max_new_tokens =1024,
55
+ temperature = temperature,
56
+ streaming=True,
57
+ callbacks=[StreamingStdOutCallbackHandler()]
58
+ )
59
+ return llm
60
+
61
+ print(f"{model_name} Model Loading start")
62
+ model=llama_model(model_path=model_path,temperature=temperature)
63
+ print(f"{model_name}Load Model Successfully.")
64
+
65
+ # if 'prompts' not in st.session_state:
66
+ # st.session_state.prompts = []
67
+ # if 'responses' not in st.session_state:
68
+ # st.session_state.responses = []
69
+
70
+ if "messages" not in st.session_state:
71
+ st.session_state.messages = []
72
+
73
+ for message in st.session_state.messages:
74
+ with st.chat_message(message["role"]):
75
+ st.markdown(message["content"])
76
+
77
+ if prompt := st.chat_input("What is up?"):
78
+ st.session_state.messages.append({"role": "user", "content": prompt})
79
+ with st.chat_message("user"):
80
+ st.markdown(prompt)
81
+ final_prompt=get_prompt(prompt,history,DEFAULT_SYSTEM_PROMPT)
82
+
83
+ with st.chat_message("assistant"):
84
+ message_placeholder = st.empty()
85
+ full_response = ""
86
+ for response in model(final_prompt):
87
+ full_response += response
88
+ message_placeholder.markdown(response + "▌")
89
+ wrapped_text = textwrap.fill(full_response, width=100)
90
+ message_placeholder.markdown(wrapped_text)
91
+ st.session_state.messages.append(
92
+ {"role": "assistant", "content": full_response}
93
+ )
94
+
95
+
96
+ # while True:
97
+ # message=input("Enter your query here...")
98
+ # prompt=get_prompt(message,history,DEFAULT_SYSTEM_PROMPT)
99
+ # ans=model.predict(prompt)
100
+ # print(ans)
101
+
102
+
103
+
104
+ # def send_click():
105
+ # print("Start..")
106
+ # if st.session_state.user != '':
107
+ # message = st.session_state.user
108
+ # if message:
109
+ # prompt=get_prompt(message,history,DEFAULT_SYSTEM_PROMPT)
110
+ # response=model.predict(prompt)
111
+ # st.session_state.prompts.append(message)
112
+ # st.session_state.responses.append(response)
113
+
114
+ # st.text_input("Ask your query here :", key="user")
115
+ # st.button("Send", on_click=send_click)
116
+
117
+ # if st.session_state.prompts:
118
+ # for i in range(len(st.session_state.responses)-1, -1, -1):
119
+ # message(st.session_state.responses[i], key=str(i), seed='Milo')
120
+ # message(st.session_state.prompts[i], is_user=True, key=str(i) + '_user', seed=83)
121
+
122
+ # if st.session_state.messages[-1]["role"] != "assistant":
123
+ # with st.chat_message("assistant"):
124
+ # with st.spinner("Thinking..."):
125
+ # response = generate_response(prompt)
126
+ # st.write(response)
127
+ # message = {"role": "assistant", "content": response}
128
+ # st.session_state.messages.append(message)