Spaces:
Sleeping
Sleeping
Download app.py from ManasaK/Conversational-AI-Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/ManasaK/Conversational-AI-Chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/ManasaK/Conversational-AI-Chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ManasaK/Conversational-AI-Chatbot/resolve/main/app.py
3.48 kB
| ## Conversational AI Chatbot | |
| from langchain_openai import OpenAI | |
| from dotenv import load_dotenv | |
| import streamlit as st | |
| import os | |
| #from langchain.schema import HumanMessage,SystemMessage,AIMessage | |
| from langchain_openai import ChatOpenAI | |
| from langchain.chains import ConversationChain | |
| from langchain.chains.conversation.memory import ConversationEntityMemory | |
| from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE | |
| ## Initialising Session State | |
| # Set Streamlit page configuration | |
| st.set_page_config(page_title='Conversational Chatbot', layout='wide') | |
| st.header("Hey there! Ask me anything.") | |
| # Initialize session states | |
| if "generated" not in st.session_state: | |
| st.session_state["generated"] = [] # Output | |
| if "past" not in st.session_state: | |
| st.session_state["past"] = [] #Previous states | |
| if "input" not in st.session_state: | |
| st.session_state["input"] = "" | |
| if "stored_session" not in st.session_state: | |
| st.session_state["stored_session"] = [] | |
| #User input | |
| def get_text(): | |
| ## Getting input from user | |
| input=st.text_input("Input:",key='input') | |
| #response = get_response(input) | |
| submit = st.button("Ask the question") | |
| return input | |
| # Define function to start a new chat | |
| def new_chat(): | |
| """ | |
| Clears session state and starts a new chat. | |
| """ | |
| save = [] | |
| for i in range(len(st.session_state['generated'])-1, -1, -1): | |
| save.append("User:" + st.session_state["past"][i]) | |
| save.append("Bot:" + st.session_state["generated"][i]) | |
| st.session_state["stored_session"].append(save) | |
| st.session_state["generated"] = [] | |
| st.session_state["past"] = [] | |
| st.session_state["input"] = "" | |
| st.session_state.entity_memory.entity_store = {} | |
| st.session_state.entity_memory.buffer.clear() | |
| chat = ChatOpenAI(openai_api_key=os.environ["OPENAI_API_KEY"], | |
| temperature=0.5,model='gpt-3.5-turbo',verbose=False) | |
| # Create a ConversationEntityMemory object if not already created | |
| if 'entity_memory' not in st.session_state: | |
| st.session_state.entity_memory = ConversationEntityMemory(llm=chat) | |
| # Create the ConversationChain object with the specified configuration | |
| Conversation = ConversationChain( | |
| llm=chat, | |
| prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE, | |
| memory=st.session_state.entity_memory | |
| ) | |
| # Get the user input | |
| user_input = get_text() | |
| if user_input: | |
| output = Conversation.run(input=user_input) | |
| st.session_state.past.append(user_input) | |
| st.session_state.generated.append(output) | |
| # Allow to download as well | |
| download_str = [] | |
| with st.expander("Conversation", expanded=True): | |
| for i in range(len(st.session_state['generated'])-1, -1, -1): | |
| st.info(st.session_state["past"][i]) | |
| st.success(st.session_state["generated"][i]) | |
| download_str.append(st.session_state["past"][i]) | |
| download_str.append(st.session_state["generated"][i]) | |
| # Can throw error - requires fix | |
| download_str = '\n'.join(download_str) | |
| if download_str: | |
| st.download_button('Download',download_str) | |
| # Display stored conversation sessions in the sidebar | |
| for i, sublist in enumerate(st.session_state.stored_session): | |
| with st.sidebar.expander(label= f"Conversation-Session:{i}"): | |
| st.write(sublist) | |
| # Allow the user to clear all stored conversation sessions | |
| if st.session_state.stored_session: | |
| if st.sidebar.checkbox("Clear-all"): | |
| del st.session_state.stored_session |