lorentz commited on
Commit
4c0413d
·
verified ·
1 Parent(s): 31f48af

Upload app backup 2.py

Browse files
Files changed (1) hide show
  1. app backup 2.py +103 -0
app backup 2.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain.chains import ConversationChain
5
+ from langchain.chains.conversation.memory import (ConversationBufferMemory,
6
+ ConversationSummaryMemory,
7
+ ConversationBufferWindowMemory
8
+
9
+ )
10
+
11
+ if 'conversation' not in st.session_state:
12
+ st.session_state['conversation'] =None
13
+ if 'messages' not in st.session_state:
14
+ st.session_state['messages'] =[]
15
+ if 'API_Key' not in st.session_state:
16
+ st.session_state['API_Key'] =''
17
+
18
+ # Setting page title and header
19
+ st.set_page_config(page_title="ChatMate: Your Professional AI Conversation Partner Solution", page_icon=":robot_face:")
20
+ st.markdown("<h1 style='text-align: center; color: navy;'>ChatMate</h1>", unsafe_allow_html=True)
21
+ st.markdown("<h4 style='text-align: center;'>A cutting-edge language model</h4>", unsafe_allow_html=True)
22
+ st.markdown("<p style='text-align: right'>By <a href='https://entzyeung.github.io/portfolio/index.html'>Lorentz Yeung</a></p>", unsafe_allow_html=True)
23
+
24
+ st.markdown("<p style='text-align: left;'>I am capable of recalling previous parts of our conversation, such as remembering your name if you share it with me.</p>", unsafe_allow_html=True)
25
+ st.session_state['API_Key']= st.text_input("First, to get it work, put your OpenAI API Key here please, the system will enter for you automatically.",type="password")
26
+ st.markdown("<p style='text-align: left;'>Then Tell me how I can help:</p>", unsafe_allow_html=True)
27
+
28
+
29
+
30
+ # API Keys
31
+ # st.sidebar.text_input() will automatically update st.session_state['API_Key'] with the input value whenever the user types into the field.
32
+ st.sidebar.title("Introduction")
33
+ st.sidebar.markdown("""
34
+ ChatMate is an advanced conversational AI interface, expertly crafted to demonstrate the fusion of Streamlit's user-friendly design and OpenAI's powerful GPT-3.5 model. Here are its highlights:
35
+
36
+ <ul style='text-align: left;'>
37
+ <li><strong>Intuitive Interface</strong>: Built with Streamlit, ChatMate offers a clean, responsive user experience, allowing for natural dialogue with the AI.</li>
38
+ <li><strong>Advanced NLP</strong>: Incorporating OpenAI's most advanced GPT model, the app provides nuanced understanding and generation of human-like text, showcasing the model's impressive capabilities.</li>
39
+ <li><strong>State Management</strong>: Utilizes <code>ConversationChain</code> and <code>ConversationMemory</code> from <code>langchain</code> to preserve the context and flow, ensuring coherent and engaging interactions.</li>
40
+ <li><strong>Python Proficiency</strong>: The app's robust backend, written in Python, reflects the data scientist’s adeptness in programming and system design.</li>
41
+ <li><strong>Secure Interaction</strong>: Streamlit's session state management is used for secure API key handling and user input retention across sessions.</li>
42
+ </ul>
43
+
44
+ ChatMate is developed by Lorentz Yeung
45
+ """, unsafe_allow_html=True)
46
+
47
+ #st.session_state['API_Key']= st.sidebar.text_input("Put your OpenAI API Key here please, the system will enter for you automatically.",type="password")
48
+
49
+ # summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
50
+ #if summarise_button:
51
+ # summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️")
52
+
53
+
54
+
55
+ # Function to get response from the model
56
+ def getresponse(userInput, api_key):
57
+
58
+ if st.session_state['conversation'] is None:
59
+
60
+ llm = ChatOpenAI(
61
+ temperature=0,
62
+ openai_api_key=api_key,
63
+ model_name='gpt-3.5-turbo'
64
+ )
65
+
66
+ st.session_state['conversation'] = ConversationChain(
67
+ llm=llm,
68
+ verbose=True,
69
+ memory=ConversationSummaryMemory(llm=llm)
70
+ )
71
+
72
+ response=st.session_state['conversation'].predict(input=userInput)
73
+ print(st.session_state['conversation'].memory.buffer)
74
+
75
+
76
+ return response
77
+
78
+
79
+
80
+ response_container = st.container()
81
+ # Here we will have a container for user input text box
82
+ container = st.container()
83
+
84
+ # User input and response display
85
+ with container:
86
+ with st.form(key='my_form', clear_on_submit=True):
87
+ user_input = st.text_area("Ask me questions please", key='input', height=100)
88
+ submit_button = st.form_submit_button(label='Send')
89
+
90
+ if submit_button:
91
+ st.session_state['messages'].append(user_input)
92
+ model_response=getresponse(user_input,st.session_state['API_Key'])
93
+ st.session_state['messages'].append(model_response)
94
+
95
+
96
+ with response_container:
97
+ for i in range(len(st.session_state['messages'])):
98
+ if (i % 2) == 0:
99
+ message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
100
+ else:
101
+ message(st.session_state['messages'][i], key=str(i) + '_AI')
102
+
103
+