BInura Yasodya
commited on
Commit
·
2a96647
1
Parent(s):
50d4e1d
Initial commit
Browse files- .DS_Store +0 -0
- app.py +67 -0
- requirements.txt +8 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from langchain.schema import HumanMessage, AIMessage, SystemMessage
|
4 |
+
from langchain_openai import ChatOpenAI
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
## Streamlit UI
|
11 |
+
st.set_page_config(page_title='Conversational Q&A Chatbot', page_icon=':robot_face:', layout='wide')
|
12 |
+
st.markdown("<h1 style='text-align: center; color: #ed4613;'>Conversational Q&A Chatbot</h1>", unsafe_allow_html=True)
|
13 |
+
st.markdown("<h4 style='text-align: center; color: #4CAF50;'>Hey, Let's Chat!</h4>", unsafe_allow_html=True)
|
14 |
+
|
15 |
+
chat_model = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY_NEW"), model='gpt-3.5-turbo', temperature=0.7)
|
16 |
+
|
17 |
+
if 'chat_history' not in st.session_state:
|
18 |
+
st.session_state['chat_history'] = [
|
19 |
+
SystemMessage(content="You are an intelligent chatbot. Please answer the following questions.")
|
20 |
+
]
|
21 |
+
|
22 |
+
def get_chatmodel_response(question):
|
23 |
+
st.session_state['chat_history'].append(HumanMessage(content=question))
|
24 |
+
response = chat_model(st.session_state['chat_history'])
|
25 |
+
st.session_state['chat_history'].append(AIMessage(content=response.content))
|
26 |
+
return response.content
|
27 |
+
|
28 |
+
input_container = st.container()
|
29 |
+
response_container = st.container()
|
30 |
+
|
31 |
+
with input_container:
|
32 |
+
with st.form(key='input_form', clear_on_submit=True):
|
33 |
+
user_input = st.text_input("Input:", key="Input", placeholder="Ask me anything...", label_visibility='collapsed')
|
34 |
+
submit = st.form_submit_button('Ask the question..')
|
35 |
+
|
36 |
+
# JavaScript to trigger form submission on Enter key press
|
37 |
+
st.markdown("""
|
38 |
+
<script>
|
39 |
+
document.addEventListener('DOMContentLoaded', function() {
|
40 |
+
const inputBox = document.querySelector('input[type="text"]');
|
41 |
+
inputBox.addEventListener('keydown', function(event) {
|
42 |
+
if (event.key === 'Enter') {
|
43 |
+
event.preventDefault();
|
44 |
+
document.querySelector('button[type="submit"]').click();
|
45 |
+
}
|
46 |
+
});
|
47 |
+
});
|
48 |
+
</script>
|
49 |
+
""", unsafe_allow_html=True)
|
50 |
+
|
51 |
+
if submit:
|
52 |
+
if user_input.strip() == "":
|
53 |
+
st.warning("Please enter a question first.")
|
54 |
+
else:
|
55 |
+
with st.spinner('Thinking...'):
|
56 |
+
response = get_chatmodel_response(user_input)
|
57 |
+
with response_container:
|
58 |
+
st.subheader('The response is:')
|
59 |
+
st.write(response)
|
60 |
+
|
61 |
+
if 'chat_history' in st.session_state and st.session_state['chat_history']:
|
62 |
+
st.markdown("<h3 style='color: #4CAF50;'>Chat History</h3>", unsafe_allow_html=True)
|
63 |
+
for msg in st.session_state['chat_history']:
|
64 |
+
if isinstance(msg, HumanMessage):
|
65 |
+
st.markdown(f"<div style='text-align: right; color: #0000FF;'>{msg.content}</div>", unsafe_allow_html=True)
|
66 |
+
elif isinstance(msg, AIMessage):
|
67 |
+
st.markdown(f"<div style='text-align: left; color: #FF5733;'>{msg.content}</div>", unsafe_allow_html=True)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
langchain
|
3 |
+
streamlit
|
4 |
+
python-dotenv
|
5 |
+
langchain-openai
|
6 |
+
langchain-community
|
7 |
+
langchain_core
|
8 |
+
huggingface_hub
|