Spaces:
Runtime error
Runtime error
Nicole Hye Won Hwang
commited on
Commit
β’
c47e25f
1
Parent(s):
bc2c3d1
first commit
Browse files- app.py +65 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_chat import message
|
4 |
+
|
5 |
+
from langchain_openai import OpenAI
|
6 |
+
from langchain.chains import ConversationChain
|
7 |
+
from langchain.chains.conversation.memory import (
|
8 |
+
ConversationBufferMemory,
|
9 |
+
ConversationSummaryMemory,
|
10 |
+
ConversationBufferWindowMemory
|
11 |
+
)
|
12 |
+
|
13 |
+
if "conversation" not in st.session_state:
|
14 |
+
st.session_state['conversation'] = None
|
15 |
+
|
16 |
+
if "messages" not in st.session_state:
|
17 |
+
st.session_state['messages'] = []
|
18 |
+
|
19 |
+
if 'api_key' not in st.session_state:
|
20 |
+
st.session_state['api_key'] = ''
|
21 |
+
|
22 |
+
def getresponse(userInput, api_key):
|
23 |
+
|
24 |
+
if not st.session_state['conversation']:
|
25 |
+
llm = OpenAI(
|
26 |
+
model="gpt-3.5-turbo-instruct",
|
27 |
+
temperature=0.1,
|
28 |
+
api_key = api_key,
|
29 |
+
)
|
30 |
+
|
31 |
+
st.session_state['conversation'] = ConversationChain(
|
32 |
+
llm = llm,
|
33 |
+
verbose = True,
|
34 |
+
memory = ConversationSummaryMemory(llm=llm)
|
35 |
+
)
|
36 |
+
response = st.session_state['conversation'].predict(input=userInput)
|
37 |
+
return response
|
38 |
+
|
39 |
+
st.set_page_config(page_title="ChatGPT Clone", page_icon=":robot_face:")
|
40 |
+
st.markdown("<h1 style='text-align: center;'>How can I assist you</h1>", unsafe_allow_html=True)
|
41 |
+
|
42 |
+
st.sidebar.title("π")
|
43 |
+
st.session_state['api_key'] = st.sidebar.text_input("What is your api key", type="password")
|
44 |
+
summarize_button = st.sidebar.button("Summarize the conversation", key='summarize')
|
45 |
+
|
46 |
+
if summarize_button:
|
47 |
+
summarize_placeholder = st.sidebar.write("Summary of current conversation:\n" + st.session_state['conversation'].memory.buffer)
|
48 |
+
|
49 |
+
response_container = st.container()
|
50 |
+
container = st.container()
|
51 |
+
|
52 |
+
with container:
|
53 |
+
with st.form(key='my_form', clear_on_submit=True):
|
54 |
+
user_input = st.text_area("Your question goes here:", key="input", height=100)
|
55 |
+
submit_button = st.form_submit_button(label="Send")
|
56 |
+
|
57 |
+
if submit_button and user_input:
|
58 |
+
answer = getresponse(user_input, st.session_state['api_key'])
|
59 |
+
# Append both user input and AI response to messages
|
60 |
+
st.session_state['messages'].append((user_input, answer)) # Store messages as tuples
|
61 |
+
|
62 |
+
with response_container:
|
63 |
+
for i, (user_msg, ai_msg) in enumerate(st.session_state['messages']): # Update loop to handle tuples
|
64 |
+
message(user_msg, is_user=True, key=f'{i}_user')
|
65 |
+
message(ai_msg, key=f'{i}_AI')
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.1.4
|
2 |
+
streamlit==1.30.0
|
3 |
+
streamlit-chat==0.1.1
|
4 |
+
openai==1.10.0
|
5 |
+
langchain-openai==0.0.5
|