Zafer01 commited on
Commit
6d2b325
·
verified ·
1 Parent(s): 0277755

Create app_v2.py

Browse files
Files changed (1) hide show
  1. app_v2.py +73 -0
app_v2.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ from streamlit_extras.colored_header import colored_header
4
+ from streamlit_extras.add_vertical_space import add_vertical_space
5
+ from hugchat import hugchat
6
+ from hugchat.login import Login
7
+
8
+ st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
9
+
10
+ # Sidebar contents
11
+ with st.sidebar:
12
+ st.title('🤗💬 HugChat App')
13
+
14
+ st.header('Hugging Face Login')
15
+ hf_email = st.text_input('Enter E-mail:', type='password')
16
+ hf_pass = st.text_input('Enter password:', type='password')
17
+
18
+ st.markdown('''
19
+ ## About
20
+ This app is an LLM-powered chatbot built using:
21
+ - [Streamlit](https://streamlit.io/)
22
+ - [HugChat](https://github.com/Soulter/hugging-chat-api)
23
+ - [OpenAssistant/oasst-sft-6-llama-30b-xor](https://huggingface.co/OpenAssistant/oasst-sft-6-llama-30b-xor) LLM model
24
+
25
+ ''')
26
+ add_vertical_space(5)
27
+ st.write('Made with ❤️ by [Data Professor](https://youtube.com/dataprofessor)')
28
+
29
+ # Generate empty lists for generated and past.
30
+ ## generated stores AI generated responses
31
+ if 'generated' not in st.session_state:
32
+ st.session_state['generated'] = ["I'm HugChat, How may I help you?"]
33
+ ## past stores User's questions
34
+ if 'past' not in st.session_state:
35
+ st.session_state['past'] = ['Hi!']
36
+
37
+ # Layout of input/response containers
38
+ input_container = st.container()
39
+ colored_header(label='', description='', color_name='blue-30')
40
+ response_container = st.container()
41
+
42
+ # User input
43
+ ## Function for taking user provided prompt as input
44
+ def get_text():
45
+ input_text = st.text_input("You: ", "", key="input")
46
+ return input_text
47
+ ## Applying the user input box
48
+ with input_container:
49
+ user_input = get_text()
50
+
51
+ # Response output
52
+ ## Function for taking user prompt as input followed by producing AI generated responses
53
+ def generate_response(prompt, email, passwd):
54
+ # Hugging Face Login
55
+ sign = Login(email, passwd)
56
+ cookies = sign.login()
57
+ sign.saveCookies()
58
+ # Create ChatBot
59
+ chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
60
+ response = chatbot.chat(prompt)
61
+ return response
62
+
63
+ ## Conditional display of AI generated responses as a function of user provided prompts
64
+ with response_container:
65
+ if user_input and hf_email and hf_pass:
66
+ response = generate_response(user_input, hf_email, hf_pass)
67
+ st.session_state.past.append(user_input)
68
+ st.session_state.generated.append(response)
69
+
70
+ if st.session_state['generated']:
71
+ for i in range(len(st.session_state['generated'])):
72
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
73
+ message(st.session_state["generated"][i], key=str(i))