ChatModelApp / app.py
gutai123's picture
Update app.py
5789561 verified
import streamlit as st
from langchain.schema import AIMessage, HumanMessage, SystemMessage
# Set up Streamlit page
st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
st.header("DIBYAJYOTI'S PERSONAL GPT ASSISTANT")
# Check for session state and initialize session if not set
if "sessionMessages" not in st.session_state:
st.session_state.sessionMessages = [
SystemMessage(content="You are a helpful assistant.")
]
# Use HuggingFace GPT-2 model (Free and Open-Source)
chat = HuggingFaceLLM(model="gpt2")
# Function to get user input
def get_text():
input_text = st.text_input("You: ")
return input_text
# Function to process the chat and load an answer
def load_answer(question):
st.session_state.sessionMessages.append(HumanMessage(content=question))
# Get the response from the assistant (HuggingFace GPT-2 model)
assistant_answer = chat.invoke(st.session_state.sessionMessages)
# Add the assistant's response to the session state
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
return assistant_answer.content
# Streamlit UI
user_input = get_text()
submit = st.button('CLICK HERE TO GET YOUR RESPONSE')
if submit:
if user_input:
response = load_answer(user_input)
st.subheader("Answer:")
st.write(response)
else:
st.warning("Please enter a question.")