Spaces:
Runtime error
Runtime error
Initial setup
Browse filesInitial file set up
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
| 6 |
+
from dotenv import load_dotenv, dotenv_values
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
if 'key' not in st.session_state:
|
| 11 |
+
st.session_state['key'] = 'value'
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# initialize the client but point it to TGI
|
| 17 |
+
client = OpenAI(
|
| 18 |
+
base_url="https://api-inference.huggingface.co/v1",
|
| 19 |
+
#api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN')#"hf_xxx" # Replace with your token
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
#Create supported models
|
| 26 |
+
model_links ={
|
| 27 |
+
"Mistral":"mistralai/Mistral-7B-Instruct-v0.2",
|
| 28 |
+
"Gemma":"google/gemma-7b-it"
|
| 29 |
+
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Define the available models
|
| 35 |
+
# models = ["Mistral", "Gemma"]
|
| 36 |
+
models =[key for key in model_links.keys()]
|
| 37 |
+
|
| 38 |
+
# Create the sidebar with the dropdown for model selection
|
| 39 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
#Pull in the model we want to use
|
| 44 |
+
repo_id = model_links[selected_model]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
st.title(f'ChatBot Using {selected_model}')
|
| 49 |
+
|
| 50 |
+
# Set a default model
|
| 51 |
+
if selected_model not in st.session_state:
|
| 52 |
+
st.session_state[selected_model] = model_links[selected_model] #"google/gemma-7b-it"
|
| 53 |
+
|
| 54 |
+
# Initialize chat history
|
| 55 |
+
if "messages" not in st.session_state:
|
| 56 |
+
st.session_state.messages = []
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Display chat messages from history on app rerun
|
| 60 |
+
for message in st.session_state.messages:
|
| 61 |
+
with st.chat_message(message["role"]):
|
| 62 |
+
st.markdown(message["content"])
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# Accept user input
|
| 67 |
+
if prompt := st.chat_input("What is up?"):
|
| 68 |
+
|
| 69 |
+
# Display user message in chat message container
|
| 70 |
+
with st.chat_message("user"):
|
| 71 |
+
st.markdown(prompt)
|
| 72 |
+
# Add user message to chat history
|
| 73 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# Display assistant response in chat message container
|
| 77 |
+
with st.chat_message("assistant"):
|
| 78 |
+
st_callback = StreamlitCallbackHandler(st.container())
|
| 79 |
+
# st_callback =stream_handler
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# stream = client.completions.create(
|
| 83 |
+
# model="google/gemma-7b-it",
|
| 84 |
+
# prompt="You are a helpful agent in a question answer exhange. Give you best answer to the questions. {prompt}",
|
| 85 |
+
# # messages=[
|
| 86 |
+
# # {"role": m["role"], "content": m["content"]}
|
| 87 |
+
# # for m in st.session_state.messages
|
| 88 |
+
# # ],
|
| 89 |
+
# temperature=0.5,
|
| 90 |
+
# stream=True,
|
| 91 |
+
# max_tokens=3000
|
| 92 |
+
# )
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
stream = client.chat.completions.create(
|
| 96 |
+
model=model_links[selected_model],#"google/gemma-7b-it",
|
| 97 |
+
messages=[
|
| 98 |
+
{"role": m["role"], "content": m["content"]}
|
| 99 |
+
for m in st.session_state.messages
|
| 100 |
+
],
|
| 101 |
+
temperature=0.5,
|
| 102 |
+
stream=True,
|
| 103 |
+
max_tokens=3000,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
response = st.write_stream(stream)
|
| 107 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|