app added
Browse files- app.py +144 -0
- config.yaml +21 -0
- requirements.txt +4 -0
- utils/frontend/helper.py +60 -0
- utils/frontend/utils/helper.py +34 -0
app.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import streamlit_authenticator as stauth
|
| 5 |
+
import yaml
|
| 6 |
+
from yaml.loader import SafeLoader
|
| 7 |
+
|
| 8 |
+
from utils.frontend.helper import *
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="SAAS Template App", page_icon="🧊", layout="wide")
|
| 11 |
+
|
| 12 |
+
with open("config.yaml") as file:
|
| 13 |
+
config = yaml.load(file, Loader=SafeLoader)
|
| 14 |
+
|
| 15 |
+
authenticator = stauth.Authenticate(
|
| 16 |
+
config["credentials"],
|
| 17 |
+
config["cookie"]["name"],
|
| 18 |
+
config["cookie"]["key"],
|
| 19 |
+
config["cookie"]["expiry_days"],
|
| 20 |
+
config["preauthorized"],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
with st.sidebar:
|
| 24 |
+
if not st.session_state["authentication_status"]:
|
| 25 |
+
st.markdown(
|
| 26 |
+
"""
|
| 27 |
+
# Welcome to **Name of the App**
|
| 28 |
+
|
| 29 |
+
"""
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Login
|
| 33 |
+
with st.expander("Login 🔒", expanded=True):
|
| 34 |
+
authenticator.login("Login", "main")
|
| 35 |
+
|
| 36 |
+
if st.session_state["authentication_status"]:
|
| 37 |
+
authenticator.logout("Logout", "main", key="unique_key")
|
| 38 |
+
st.write(f'Welcome *{st.session_state["name"]}*')
|
| 39 |
+
st.title(
|
| 40 |
+
"You logged in! You can choose a session in the bottom of the sidebar and view content."
|
| 41 |
+
)
|
| 42 |
+
elif st.session_state["authentication_status"] is False:
|
| 43 |
+
st.error("Username/password is incorrect")
|
| 44 |
+
elif st.session_state["authentication_status"] is None:
|
| 45 |
+
st.warning("Please enter your username and password")
|
| 46 |
+
|
| 47 |
+
# Reset Password
|
| 48 |
+
with st.expander("Reset password 😕"):
|
| 49 |
+
if st.session_state["authentication_status"]:
|
| 50 |
+
try:
|
| 51 |
+
if authenticator.reset_password(
|
| 52 |
+
st.session_state["username"], "Reset password"
|
| 53 |
+
):
|
| 54 |
+
st.success("Password modified successfully")
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(e)
|
| 57 |
+
|
| 58 |
+
# Forgot Password
|
| 59 |
+
with st.expander("Forgot password 😕"):
|
| 60 |
+
try:
|
| 61 |
+
(
|
| 62 |
+
username_of_forgotten_password,
|
| 63 |
+
email_of_forgotten_password,
|
| 64 |
+
new_random_password,
|
| 65 |
+
) = authenticator.forgot_password("Forgot password")
|
| 66 |
+
if username_of_forgotten_password:
|
| 67 |
+
st.success("New password to be sent securely")
|
| 68 |
+
# Random password should be transferred to user securely
|
| 69 |
+
else:
|
| 70 |
+
st.error("Username not found")
|
| 71 |
+
except Exception as e:
|
| 72 |
+
st.error(e)
|
| 73 |
+
|
| 74 |
+
# Update User
|
| 75 |
+
with st.expander("Update user 😋"):
|
| 76 |
+
if st.session_state["authentication_status"]:
|
| 77 |
+
try:
|
| 78 |
+
if authenticator.update_user_details(
|
| 79 |
+
st.session_state["username"], "Update user details"
|
| 80 |
+
):
|
| 81 |
+
st.success("Entries updated successfully")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
st.error(e)
|
| 84 |
+
|
| 85 |
+
# Register User
|
| 86 |
+
with st.expander("Register user 😋", expanded=False):
|
| 87 |
+
try:
|
| 88 |
+
# payment_key = st.text_input("Enter Payment Key:")
|
| 89 |
+
if authenticator.register_user("Register user", preauthorization=False):
|
| 90 |
+
# if register_user(payment_key):
|
| 91 |
+
st.success("User registered successfully")
|
| 92 |
+
except Exception as e:
|
| 93 |
+
st.error(e)
|
| 94 |
+
|
| 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"]:
|
| 114 |
+
st.markdown(
|
| 115 |
+
"""
|
| 116 |
+
# Content of the App
|
| 117 |
+
|
| 118 |
+
Welcome!
|
| 119 |
+
"""
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Add Main Content Here
|
| 123 |
+
# TODO
|
| 124 |
+
main_chatbot_page()
|
| 125 |
+
|
| 126 |
+
# Log out
|
| 127 |
+
with st.sidebar:
|
| 128 |
+
with st.expander("Login 🔒", expanded=True):
|
| 129 |
+
authenticator.login("Login", "main")
|
| 130 |
+
|
| 131 |
+
if st.session_state["authentication_status"]:
|
| 132 |
+
authenticator.logout("Logout", "main", key="unique_key")
|
| 133 |
+
st.write(f'Welcome *{st.session_state["name"]}*')
|
| 134 |
+
st.title(
|
| 135 |
+
"You logged in! You can choose a session in the bottom of the sidebar and view content."
|
| 136 |
+
)
|
| 137 |
+
st.markdown(
|
| 138 |
+
"### 🖱️Press "
|
| 139 |
+
R" on your keyboard⌨️ to rerun the app for faster loading 🔄."
|
| 140 |
+
)
|
| 141 |
+
elif st.session_state["authentication_status"] is False:
|
| 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")
|
config.yaml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cookie:
|
| 2 |
+
expiry_days: 30
|
| 3 |
+
key: some_signature_key
|
| 4 |
+
name: some_cookie_name
|
| 5 |
+
credentials:
|
| 6 |
+
usernames:
|
| 7 |
+
eagle0504:
|
| 8 |
+
email: eagle0504@gmail.com
|
| 9 |
+
name: Yiqiao
|
| 10 |
+
password: $2b$12$xTz9hrdI3QbE7IwPMrCYGeqKNYrkJeQ/dz22278xLyMIzb2SZj4Im
|
| 11 |
+
jsmith:
|
| 12 |
+
email: jsmith@gmail.com
|
| 13 |
+
name: John Smith
|
| 14 |
+
password: $2b$12$y7OJocq.1O9.wk08Qj9qT.aHiatbIzn.8RKs9Yw2Aac/baNtkcqeu
|
| 15 |
+
rbriggs:
|
| 16 |
+
email: rbriggs@gmail.com
|
| 17 |
+
name: Rebecca Briggs
|
| 18 |
+
password: $2b$12$d6VtLvzfiMv7HBIF2kDhMeeyiyG/LQIUBhZA7e70zKsA/l5bVpQjK
|
| 19 |
+
preauthorized:
|
| 20 |
+
emails:
|
| 21 |
+
- melsby@gmail.com
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
streamlit-authenticator==0.2.3
|
| 3 |
+
pyyaml
|
| 4 |
+
together
|
utils/frontend/helper.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from together import Together
|
| 4 |
+
|
| 5 |
+
from utils.helper import *
|
| 6 |
+
|
| 7 |
+
def main_chatbot_page():
|
| 8 |
+
st.set_page_config(layout="wide")
|
| 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.
|
| 18 |
+
|
| 19 |
+
### How to Use:
|
| 20 |
+
1. **Input**: Type your prompt into the chat input box labeled "What is up?".
|
| 21 |
+
2. **Response**: The app will display a response from Llama3.
|
| 22 |
+
3. **Chat History**: Previous conversations will be shown on the app.
|
| 23 |
+
|
| 24 |
+
### Credits:
|
| 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})
|
utils/frontend/utils/helper.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|