eagle0504 commited on
Commit
4cc22a3
·
1 Parent(s): 72f1085

app updated

Browse files
Files changed (3) hide show
  1. app.py +16 -13
  2. utils/frontend/helper.py +16 -9
  3. utils/frontend/utils/helper.py +10 -17
app.py CHANGED
@@ -95,19 +95,6 @@ with st.sidebar:
95
  with open("config.yaml", "w") as file:
96
  yaml.dump(config, file, default_flow_style=False)
97
 
98
- # Credit: Time
99
- def current_year():
100
- now = datetime.now()
101
- return now.year
102
-
103
- # Credit:
104
- current_year = current_year() # This will print the current year
105
- st.markdown(
106
- f"""
107
- <h6 style='text-align: left;'>Copyright © 2010-{current_year} Present Yiqiao Yin</h6>
108
- """,
109
- unsafe_allow_html=True,
110
- )
111
 
112
  # Main
113
  if st.session_state["authentication_status"]:
@@ -142,3 +129,19 @@ if st.session_state["authentication_status"]:
142
  st.error("Username/password is incorrect")
143
  elif st.session_state["authentication_status"] is None:
144
  st.warning("Please enter your username and password")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  with open("config.yaml", "w") as file:
96
  yaml.dump(config, file, default_flow_style=False)
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  # Main
100
  if st.session_state["authentication_status"]:
 
129
  st.error("Username/password is incorrect")
130
  elif st.session_state["authentication_status"] is None:
131
  st.warning("Please enter your username and password")
132
+
133
+
134
+ with st.sidebar:
135
+ # Credit: Time
136
+ def current_year():
137
+ now = datetime.now()
138
+ return now.year
139
+
140
+ # Credit:
141
+ current_year = current_year() # This will print the current year
142
+ st.markdown(
143
+ f"""
144
+ <h6 style='text-align: left;'>Copyright © 2010-{current_year} Present Yiqiao Yin</h6>
145
+ """,
146
+ unsafe_allow_html=True,
147
+ )
utils/frontend/helper.py CHANGED
@@ -1,17 +1,18 @@
1
- import streamlit as st
2
  import os
 
 
3
  from together import Together
4
 
5
  from utils.frontend.utils.helper import *
6
 
7
- def main_chatbot_page():
8
 
 
9
  st.title("Meta Llama3 🦙")
10
 
11
-
12
  with st.sidebar:
13
  with st.expander("Instruction Manual"):
14
- st.markdown("""
 
15
  ## Meta Llama3 🦙 Chatbot
16
 
17
  This Streamlit app allows you to chat with Meta's Llama3 model.
@@ -25,36 +26,42 @@ def main_chatbot_page():
25
  - **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/meta-llama) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)
26
 
27
  Enjoy chatting with Meta's Llama3 model!
28
- """)
 
29
 
30
  # Add a button to clear the session state
31
  if st.button("Clear Session"):
32
  st.session_state.messages = []
33
  st.experimental_rerun()
34
 
35
-
36
  # Initialize chat history
37
  if "messages" not in st.session_state:
38
  st.session_state.messages = []
39
 
40
-
41
  # Display chat messages from history on app rerun
42
  for message in st.session_state.messages:
43
  with st.chat_message(message["role"]):
44
  st.markdown(message["content"])
45
 
 
 
46
 
47
  # React to user input
48
  if prompt := st.chat_input("What is up?"):
 
 
 
49
  # Display user message in chat message container
50
  st.chat_message("user").markdown(prompt)
51
  # Add user message to chat history
52
  st.session_state.messages.append({"role": "user", "content": prompt})
53
 
54
- response = call_llama(prompt)
 
 
55
 
56
  # Display assistant response in chat message container
57
  with st.chat_message("assistant"):
58
  st.markdown(response)
59
  # Add assistant response to chat history
60
- st.session_state.messages.append({"role": "assistant", "content": response})
 
 
1
  import os
2
+
3
+ import streamlit as st
4
  from together import Together
5
 
6
  from utils.frontend.utils.helper import *
7
 
 
8
 
9
+ def main_chatbot_page():
10
  st.title("Meta Llama3 🦙")
11
 
 
12
  with st.sidebar:
13
  with st.expander("Instruction Manual"):
14
+ st.markdown(
15
+ """
16
  ## Meta Llama3 🦙 Chatbot
17
 
18
  This Streamlit app allows you to chat with Meta's Llama3 model.
 
26
  - **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/meta-llama) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)
27
 
28
  Enjoy chatting with Meta's Llama3 model!
29
+ """
30
+ )
31
 
32
  # Add a button to clear the session state
33
  if st.button("Clear Session"):
34
  st.session_state.messages = []
35
  st.experimental_rerun()
36
 
 
37
  # Initialize chat history
38
  if "messages" not in st.session_state:
39
  st.session_state.messages = []
40
 
 
41
  # Display chat messages from history on app rerun
42
  for message in st.session_state.messages:
43
  with st.chat_message(message["role"]):
44
  st.markdown(message["content"])
45
 
46
+ # Define the prompt as a user message
47
+ input_prompt_object = [{"role": "user", "content": "You are a helpful assistant."}]
48
 
49
  # React to user input
50
  if prompt := st.chat_input("What is up?"):
51
+ # Define the prompt as a user message
52
+ input_prompt_object.append({"role": "user", "content": prompt})
53
+
54
  # Display user message in chat message container
55
  st.chat_message("user").markdown(prompt)
56
  # Add user message to chat history
57
  st.session_state.messages.append({"role": "user", "content": prompt})
58
 
59
+ # Call Llama3 model to generate response
60
+ response = call_llama(input_prompt_object)
61
+ input_prompt_object.append({"role": "assistant", "content": response})
62
 
63
  # Display assistant response in chat message container
64
  with st.chat_message("assistant"):
65
  st.markdown(response)
66
  # Add assistant response to chat history
67
+ st.session_state.messages.append({"role": "assistant", "content": response})
utils/frontend/utils/helper.py CHANGED
@@ -1,34 +1,27 @@
1
- import streamlit as st
2
  import os
3
- from together import Together
4
 
 
 
5
 
6
  client = Together(api_key=os.environ["TOGETHER_API_KEY"])
7
 
8
 
9
- def call_llama(prompt: str) -> str:
10
  """
11
- Send a prompt to the Llama model and return the response.
12
- Args:
13
- prompt (str): The input prompt to send to the Llama model.
14
- Returns:
15
- str: The response from the Llama model.
16
  """
17
 
18
  # Create a completion request with the prompt
19
  response = client.chat.completions.create(
20
-
21
  # Use the Llama-3-8b-chat-hf model
22
  model="meta-llama/Llama-3-8b-chat-hf",
23
-
24
  # Define the prompt as a user message
25
- messages=[
26
- {
27
- "role": "user",
28
- "content": prompt # Use the input prompt
29
- }
30
- ],
31
  )
32
 
33
  # Return the content of the first response message
34
- return response.choices[0].message.content
 
 
1
  import os
 
2
 
3
+ import streamlit as st
4
+ from together import Together
5
 
6
  client = Together(api_key=os.environ["TOGETHER_API_KEY"])
7
 
8
 
9
+ def call_llama(prompt_object: list[dict]) -> str:
10
  """
11
+ Send a prompt to the Llama model and return the response.
12
+ Args:
13
+ prompt (str): The input prompt to send to the Llama model.
14
+ Returns:
15
+ str: The response from the Llama model.
16
  """
17
 
18
  # Create a completion request with the prompt
19
  response = client.chat.completions.create(
 
20
  # Use the Llama-3-8b-chat-hf model
21
  model="meta-llama/Llama-3-8b-chat-hf",
 
22
  # Define the prompt as a user message
23
+ messages=prompt_object,
 
 
 
 
 
24
  )
25
 
26
  # Return the content of the first response message
27
+ return response.choices[0].message.content