Spaces:
Sleeping
Sleeping
Restore
Browse files
app.py
CHANGED
|
@@ -1,164 +1,85 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
-
from langchain.schema import Document
|
| 8 |
-
from langchain_community.llms import HuggingFaceHub
|
| 9 |
-
from langchain.chains import RetrievalQA
|
| 10 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 11 |
-
from langchain_community.vectorstores import Chroma
|
| 12 |
-
from tqdm import tqdm
|
| 13 |
import random
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
# Constants
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
model_links = {
|
| 27 |
"Meta-Llama-3.1-8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 28 |
"Mistral-7B-Instruct-v0.3": "mistralai/Mistral-7B-Instruct-v0.3",
|
|
|
|
| 29 |
}
|
| 30 |
|
|
|
|
| 31 |
model_info = {
|
| 32 |
"Meta-Llama-3.1-8B": {
|
| 33 |
-
|
| 34 |
-
\nIt was created by the [**Meta's AI**](https://llama.meta.com/) team and has over **8 billion parameters.**\n"""
|
| 35 |
-
"logo": "llama_logo.gif",
|
| 36 |
},
|
| 37 |
"Mistral-7B-Instruct-v0.3": {
|
| 38 |
-
|
| 39 |
-
\nIt was created by
|
| 40 |
-
"logo": "https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp",
|
| 41 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
# Random dog images for error message
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
with open(file_path, "r") as file:
|
| 54 |
-
data = json.load(file)
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
]
|
| 68 |
-
|
| 69 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 70 |
-
chunk_size=CHUNK_SIZE, chunk_overlap=CHUNK_OVERLAP
|
| 71 |
-
)
|
| 72 |
-
splits = text_splitter.split_documents(doc_objects)
|
| 73 |
-
|
| 74 |
-
return splits
|
| 75 |
-
except Exception as e:
|
| 76 |
-
st.error(f"Error loading documents: {str(e)}")
|
| 77 |
-
return []
|
| 78 |
-
|
| 79 |
-
def get_vectorstore(file_path):
|
| 80 |
-
"""Get or create a vectorstore."""
|
| 81 |
-
try:
|
| 82 |
-
if os.path.exists(VECTORSTORE_PATH):
|
| 83 |
-
print("Loading existing vectorstore...")
|
| 84 |
-
return Chroma(
|
| 85 |
-
persist_directory=VECTORSTORE_PATH, embedding_function=embeddings
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
documents=batch,
|
| 97 |
-
embedding=embeddings,
|
| 98 |
-
persist_directory=VECTORSTORE_PATH,
|
| 99 |
-
)
|
| 100 |
-
else:
|
| 101 |
-
vectorstore.add_documents(documents=batch)
|
| 102 |
-
|
| 103 |
-
vectorstore.persist()
|
| 104 |
-
return vectorstore
|
| 105 |
-
except Exception as e:
|
| 106 |
-
st.error(f"Error creating vectorstore: {str(e)}")
|
| 107 |
-
return None
|
| 108 |
-
|
| 109 |
-
@st.cache_resource(hash_funcs={"builtins.tuple": lambda _: None})
|
| 110 |
-
def setup_rag_pipeline(file_path, model_name, temperature):
|
| 111 |
-
"""Set up the RAG pipeline."""
|
| 112 |
-
try:
|
| 113 |
-
vectorstore = get_vectorstore(file_path)
|
| 114 |
-
if vectorstore is None:
|
| 115 |
-
raise ValueError("Failed to create or load vectorstore.")
|
| 116 |
-
|
| 117 |
-
llm = HuggingFaceHub(
|
| 118 |
-
repo_id=model_links[model_name],
|
| 119 |
-
model_kwargs={"temperature": temperature, "max_length": 4000},
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
return RetrievalQA.from_chain_type(
|
| 123 |
-
llm=llm,
|
| 124 |
-
chain_type="stuff",
|
| 125 |
-
retriever=vectorstore.as_retriever(search_kwargs={"k": RETRIEVER_K}),
|
| 126 |
-
return_source_documents=True,
|
| 127 |
-
)
|
| 128 |
-
except Exception as e:
|
| 129 |
-
st.error(f"Error setting up RAG pipeline: {str(e)}")
|
| 130 |
-
return None
|
| 131 |
-
|
| 132 |
-
# Streamlit app
|
| 133 |
-
st.header("Liahona.AI")
|
| 134 |
-
|
| 135 |
-
# Sidebar for model selection
|
| 136 |
-
selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys()))
|
| 137 |
-
st.markdown(f"_powered_ by ***:violet[{selected_model}]***")
|
| 138 |
-
|
| 139 |
-
# Temperature slider
|
| 140 |
-
temperature = st.sidebar.slider("Select a temperature value", 0.0, 1.0, 0.5)
|
| 141 |
-
|
| 142 |
-
# Display model info
|
| 143 |
-
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
| 144 |
-
st.sidebar.markdown(model_info[selected_model]["description"])
|
| 145 |
-
st.sidebar.image(model_info[selected_model]["logo"])
|
| 146 |
-
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
| 147 |
-
|
| 148 |
-
# Initialize chat history
|
| 149 |
-
if "messages" not in st.session_state:
|
| 150 |
-
st.session_state.messages = []
|
| 151 |
-
|
| 152 |
-
# Display chat messages from history
|
| 153 |
-
for message in st.session_state.messages:
|
| 154 |
-
with st.chat_message(message["role"]):
|
| 155 |
-
st.markdown(message["content"])
|
| 156 |
-
|
| 157 |
-
# Set up advanced RAG pipeline
|
| 158 |
-
qa_chain = setup_rag_pipeline("index_training.json", selected_model, temperature)
|
| 159 |
-
|
| 160 |
-
# Chat input
|
| 161 |
-
if prompt := st.chat_input("Type message here..."):
|
| 162 |
# Display user message
|
| 163 |
with st.chat_message("user"):
|
| 164 |
st.markdown(prompt)
|
|
@@ -167,21 +88,35 @@ if prompt := st.chat_input("Type message here..."):
|
|
| 167 |
# Generate and display assistant response
|
| 168 |
with st.chat_message("assistant"):
|
| 169 |
try:
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
except Exception as e:
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
+
import numpy as np
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import random
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
# Constants
|
| 12 |
+
MAX_TOKENS = 4000
|
| 13 |
+
DEFAULT_TEMPERATURE = 0.5
|
| 14 |
+
|
| 15 |
+
# Initialize the client
|
| 16 |
+
def initialize_client():
|
| 17 |
+
api_key = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
|
| 18 |
+
if not api_key:
|
| 19 |
+
st.error("HUGGINGFACEHUB_API_TOKEN not found in environment variables.")
|
| 20 |
+
st.stop()
|
| 21 |
+
return OpenAI(
|
| 22 |
+
base_url="https://api-inference.huggingface.co/v1",
|
| 23 |
+
api_key=api_key
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Create supported models
|
| 27 |
model_links = {
|
| 28 |
"Meta-Llama-3.1-8B": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 29 |
"Mistral-7B-Instruct-v0.3": "mistralai/Mistral-7B-Instruct-v0.3",
|
| 30 |
+
"Gemma-7b-it": "google/gemma-7b-it",
|
| 31 |
}
|
| 32 |
|
| 33 |
+
# Pull info about the model to display
|
| 34 |
model_info = {
|
| 35 |
"Meta-Llama-3.1-8B": {
|
| 36 |
+
'description': """The Llama (3.1) model is a **Large Language Model (LLM)** that's able to have question and answer interactions.
|
| 37 |
+
\nIt was created by the [**Meta's AI**](https://llama.meta.com/) team and has over **8 billion parameters.**\n"""
|
|
|
|
| 38 |
},
|
| 39 |
"Mistral-7B-Instruct-v0.3": {
|
| 40 |
+
'description': """The Mistral-7B-Instruct-v0.3 is an instruct-tuned version of Mistral-7B.
|
| 41 |
+
\nIt was created by [**Mistral AI**](https://mistral.ai/) and has **7 billion parameters.**\n"""
|
|
|
|
| 42 |
},
|
| 43 |
+
"Gemma-7b-it": {
|
| 44 |
+
'description': """Gemma is a family of lightweight, state-of-the-art open models from Google.
|
| 45 |
+
\nThe 7B-it variant is instruction-tuned and has **7 billion parameters.**\n"""
|
| 46 |
+
}
|
| 47 |
}
|
| 48 |
|
| 49 |
# Random dog images for error message
|
| 50 |
+
random_dog_images = ["BlueLogoBox.jpg", "RandomDog1.jpg", "RandomDog2.jpg"]
|
| 51 |
|
| 52 |
+
def main():
|
| 53 |
+
st.header('Liahona.AI')
|
| 54 |
|
| 55 |
+
# Sidebar for model selection and temperature
|
| 56 |
+
selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys()))
|
| 57 |
+
temperature = st.sidebar.slider('Select a temperature value', 0.0, 1.0, DEFAULT_TEMPERATURE)
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
st.markdown(f'_powered_ by ***:violet[{selected_model}]***')
|
| 60 |
|
| 61 |
+
# Display model info
|
| 62 |
+
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
| 63 |
+
st.sidebar.markdown(model_info[selected_model]['description'])
|
| 64 |
+
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
| 65 |
|
| 66 |
+
# Initialize chat history
|
| 67 |
+
if "messages" not in st.session_state:
|
| 68 |
+
st.session_state.messages = []
|
| 69 |
+
|
| 70 |
+
# Display chat messages from history on app rerun
|
| 71 |
+
for message in st.session_state.messages:
|
| 72 |
+
with st.chat_message(message["role"]):
|
| 73 |
+
st.markdown(message["content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Initialize client
|
| 76 |
+
client = initialize_client()
|
| 77 |
+
|
| 78 |
+
# Chat input and response
|
| 79 |
+
if prompt := st.chat_input("Type message here..."):
|
| 80 |
+
process_user_input(client, prompt, selected_model, temperature)
|
| 81 |
+
|
| 82 |
+
def process_user_input(client, prompt, selected_model, temperature):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# Display user message
|
| 84 |
with st.chat_message("user"):
|
| 85 |
st.markdown(prompt)
|
|
|
|
| 88 |
# Generate and display assistant response
|
| 89 |
with st.chat_message("assistant"):
|
| 90 |
try:
|
| 91 |
+
stream = client.chat.completions.create(
|
| 92 |
+
model=model_links[selected_model],
|
| 93 |
+
messages=[
|
| 94 |
+
{"role": m["role"], "content": m["content"]}
|
| 95 |
+
for m in st.session_state.messages
|
| 96 |
+
],
|
| 97 |
+
temperature=temperature,
|
| 98 |
+
stream=True,
|
| 99 |
+
max_tokens=MAX_TOKENS,
|
| 100 |
+
)
|
| 101 |
+
response = st.write_stream(stream)
|
| 102 |
except Exception as e:
|
| 103 |
+
handle_error(e)
|
| 104 |
+
return
|
| 105 |
+
|
| 106 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 107 |
+
|
| 108 |
+
def handle_error(error):
|
| 109 |
+
response = """😵💫 Looks like someone unplugged something!
|
| 110 |
+
\n Either the model space is being updated or something is down.
|
| 111 |
+
\n
|
| 112 |
+
\n Try again later.
|
| 113 |
+
\n
|
| 114 |
+
\n Here's a random pic of a 🐶:"""
|
| 115 |
+
st.write(response)
|
| 116 |
+
random_dog_pick = random.choice(random_dog_images)
|
| 117 |
+
st.image(random_dog_pick)
|
| 118 |
+
st.write("This was the error message:")
|
| 119 |
+
st.write(str(error))
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
main()
|