CoolSnow commited on
Commit
31b3470
β€’
1 Parent(s): 1bb67b4

feat: HugChat

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +54 -39
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Test
3
  emoji: πŸŒ–
4
  colorFrom: blue
5
  colorTo: purple
 
1
  ---
2
+ title: SpaceDemo
3
  emoji: πŸŒ–
4
  colorFrom: blue
5
  colorTo: purple
app.py CHANGED
@@ -1,40 +1,55 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)