hamxea commited on
Commit
652ff18
Β·
1 Parent(s): 741c70b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -79
app.py CHANGED
@@ -1,80 +1,55 @@
1
- import wget
2
-
3
- def bar_custom(current, total, width=80):
4
- print("Downloading %d%% [%d / %d] bytes" % (current / total * 100, current, total))
5
-
6
- #model_url = "https://huggingface.co/hamxea/Llama-2-7b-chat-hf-activity-fine-tuned-v2"
7
- #wget.download(model_url, bar=bar_custom)
8
-
9
- import streamlit as st
10
- from llama_index import (
11
- SimpleDirectoryReader,
12
- VectorStoreIndex,
13
- ServiceContext,
14
- )
15
- from llama_index.llms import LlamaCPP
16
- from llama_index.llms.llama_utils import (
17
- messages_to_prompt,
18
- completion_to_prompt,
19
- )
20
- from langchain.schema import(SystemMessage, HumanMessage, AIMessage)
21
-
22
- def init_page() -> None:
23
- st.set_page_config(
24
- page_title="Personal Chatbot"
25
- )
26
- st.header("Persoanl Chatbot")
27
- st.sidebar.title("Options")
28
-
29
- def select_llm() -> LlamaCPP:
30
- return LlamaCPP(
31
- model_path="/hamxea/Llama-2-7b-chat-hf-activity-fine-tuned-v2",
32
- temperature=0.1,
33
- max_new_tokens=500,
34
- context_window=3900,
35
- generate_kwargs={},
36
- model_kwargs={"n_gpu_layers":1},
37
- messages_to_prompt=messages_to_prompt,
38
- completion_to_prompt=completion_to_prompt,
39
- verbose=True,
40
- )
41
-
42
- def init_messages() -> None:
43
- clear_button = st.sidebar.button("Clear Conversation", key="clear")
44
- if clear_button or "messages" not in st.session_state:
45
- st.session_state.messages = [
46
- SystemMessage(
47
- content="you are a helpful AI assistant. Reply your answer in markdown format."
48
- )
49
- ]
50
-
51
- def get_answer(llm, messages) -> str:
52
- response = llm.complete(messages)
53
- return response.text
54
-
55
- def main() -> None:
56
- init_page()
57
- llm = select_llm()
58
- init_messages()
59
-
60
- if user_input := st.chat_input("Input your question!"):
61
- st.session_state.messages.append(HumanMessage(content=user_input))
62
- with st.spinner("Bot is typing ..."):
63
- answer = get_answer(llm, user_input)
64
- print(answer)
65
- st.session_state.messages.append(AIMessage(content=answer))
66
 
67
-
68
- messages = st.session_state.get("messages", [])
69
- for message in messages:
70
- if isinstance(message, AIMessage):
71
- with st.chat_message("assistant"):
72
- st.markdown(message.content)
73
- elif isinstance(message, HumanMessage):
74
- with st.chat_message("user"):
75
- st.markdown(message.content)
76
-
77
- if __name__ == "__main__":
78
- main()
79
-
80
- # !streamlit run app.py & npx localtunnel --port 8501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from hugchat import hugchat
3
+ from hugchat.login import Login
4
+
5
+ # App title
6
+ st.set_page_config(page_title="πŸ€—πŸ’¬ HugChat")
7
+
8
+ # Hugging Face Credentials
9
+ with st.sidebar:
10
+ st.title('πŸ€—πŸ’¬ HugChat')
11
+ if ('EMAIL' in st.secrets) and ('PASS' in st.secrets):
12
+ st.success('HuggingFace Login credentials already provided!', icon='βœ…')
13
+ hf_email = st.secrets['EMAIL']
14
+ hf_pass = st.secrets['PASS']
15
+ else:
16
+ hf_email = st.text_input('Enter E-mail:', type='password')
17
+ hf_pass = st.text_input('Enter password:', type='password')
18
+ if not (hf_email and hf_pass):
19
+ st.warning('Please enter your credentials!', icon='⚠️')
20
+ else:
21
+ st.success('Proceed to entering your prompt message!', icon='πŸ‘‰')
22
+ st.markdown('πŸ“– Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-an-llm-powered-chatbot-with-streamlit/)!')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Store LLM generated responses
25
+ if "messages" not in st.session_state.keys():
26
+ st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
27
+
28
+ # Display chat messages
29
+ for message in st.session_state.messages:
30
+ with st.chat_message(message["role"]):
31
+ st.write(message["content"])
32
+
33
+ # Function for generating LLM response
34
+ def generate_response(prompt_input, email, passwd):
35
+ # Hugging Face Login
36
+ sign = Login(email, passwd)
37
+ cookies = sign.login()
38
+ # Create ChatBot
39
+ chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
40
+ return chatbot.chat(prompt_input)
41
+
42
+ # User-provided prompt
43
+ if prompt := st.chat_input(disabled=not (hf_email and hf_pass)):
44
+ st.session_state.messages.append({"role": "user", "content": prompt})
45
+ with st.chat_message("user"):
46
+ st.write(prompt)
47
+
48
+ # Generate a new response if last message is not from assistant
49
+ if st.session_state.messages[-1]["role"] != "assistant":
50
+ with st.chat_message("assistant"):
51
+ with st.spinner("Thinking..."):
52
+ response = generate_response(prompt, hf_email, hf_pass)
53
+ st.write(response)
54
+ message = {"role": "assistant", "content": response}
55
+ st.session_state.messages.append(message)