Spaces:
Sleeping
Sleeping
| # https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps | |
| import os | |
| import time | |
| import openai | |
| import requests | |
| import streamlit as st | |
| from models import bloom | |
| from utils.util import * | |
| # from streamlit_chat import message | |
| st.title("Welcome to RegBotBeta 2.0") | |
| st.header("a prototype regulation chatbot!") | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| index = None | |
| #api_key = st.text_input("Enter your OpenAI API key here:", type="password") | |
| #from dotenv import load_dotenv, find_dotenv | |
| #_ = load_dotenv(find_dotenv()) # read local .env file | |
| api_key = os.environ['OPENAI_API_KEY'] | |
| #from huggingface_hub import secrets | |
| # Replace "OPENAI_API_KEY" with the actual name of your secret | |
| #api_key = secrets.get("OPENAI_API_KEY") | |
| if api_key: | |
| resp = validate(api_key) | |
| if "error" in resp.json(): | |
| st.info("Invalid Token! Try again.") | |
| else: | |
| #st.info("Success") | |
| os.environ["OPENAI_API_KEY"] = api_key | |
| openai.api_key = api_key | |
| with st.spinner("Initializing vector index ..."): | |
| index = create_index(bloom) | |
| st.write("---") | |
| if index: | |
| # Display chat messages from history on app rerun | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| if prompt := st.chat_input("Ask your question"): | |
| # Display user message in chat message container | |
| st.chat_message("user").markdown(prompt) | |
| # Add user message to chat history | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| with st.spinner("Processing your query..."): | |
| bot_response = get_response(index, prompt) | |
| print("bot: ", bot_response) | |
| # Display assistant response in chat message container | |
| with st.chat_message("assistant"): | |
| message_placeholder = st.empty() | |
| full_response = "" | |
| # simulate the chatbot "thinking" before responding | |
| # (or stream its response) | |
| for chunk in bot_response.split(): | |
| full_response += chunk + " " | |
| time.sleep(0.05) | |
| # add a blinking cursor to simulate typing | |
| message_placeholder.markdown(full_response + "▌") | |
| message_placeholder.markdown(full_response) | |
| # st.markdown(response) | |
| # Add assistant response to chat history | |
| st.session_state.messages.append( | |
| {"role": "assistant", "content": full_response} | |
| ) | |
| # Scroll to the bottom of the chat container | |
| # st.markdown( | |
| # """ | |
| # <script> | |
| # const chatContainer = document.getElementsByClassName("css-1n76uvr")[0]; | |
| # chatContainer.scrollTop = chatContainer.scrollHeight; | |
| # </script> | |
| # """, | |
| # unsafe_allow_html=True, | |
| # ) | |