mdr123 commited on
Commit
a193112
1 Parent(s): 5b17d17

Upload app_simplified.py

Browse files
Files changed (1) hide show
  1. app_simplified.py +102 -0
app_simplified.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Importing necessary modules and setting up environment variables
3
+ from langchain_openai import OpenAI
4
+ from langchain.chains import ConversationChain
5
+ from langchain.chains.conversation.memory import (ConversationBufferMemory,
6
+ ConversationSummaryMemory,
7
+ ConversationBufferWindowMemory
8
+ )
9
+ import tiktoken
10
+ from langchain.memory import ConversationTokenBufferMemory
11
+ import streamlit as st
12
+ from streamlit_chat import message
13
+ import os
14
+
15
+
16
+
17
+ # Initializing session state variables
18
+ if 'conversation' not in st.session_state:
19
+ st.session_state['conversation'] =None
20
+ if 'messages' not in st.session_state:
21
+ st.session_state['messages'] =[]
22
+ #if 'API_Key' not in st.session_state:
23
+ #st.session_state['API_Key'] =''
24
+
25
+ # Setting up the page title and header
26
+ st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
27
+ st.markdown("<h1 style='text-align: center;'>How can I assist you? </h1>", unsafe_allow_html=True)
28
+ st.sidebar.title("😎")
29
+ #st.session_state['API_Key']= st.sidebar.text_input("What's your API key?",type="password")
30
+ summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
31
+ if summarise_button:
32
+ summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
33
+
34
+ # Function to get response from the model
35
+ def getresponse(userInput):
36
+ """
37
+ This function takes user input and api key as arguments, and returns the model's response.
38
+ """
39
+ if st.session_state['conversation'] is None:
40
+ llm = OpenAI(
41
+ temperature=0,
42
+ #openai_api_key from spaces secret
43
+ #openai_api_key=api_key,
44
+ model_name='gpt-3.5-turbo-instruct'
45
+ )
46
+ #Creating a new conversation chain
47
+ #ConversationChain is a class that handles the conversation between the user and the model
48
+ st.session_state['conversation'] = ConversationChain(
49
+ llm=llm,
50
+ verbose=True,
51
+ memory=ConversationSummaryMemory(llm=llm)
52
+ )
53
+ #
54
+ response=st.session_state['conversation'].predict(input=userInput)
55
+ print(st.session_state['conversation'].memory.buffer)
56
+ return response
57
+
58
+ # Function to clear the conversation
59
+ def clear():
60
+ """
61
+ This function clears the conversation and messages from the session state.
62
+ """
63
+ if st.session_state['conversation']:
64
+ st.session_state['conversation'].memory.clear()
65
+ st.session_state['messages'] =[]
66
+
67
+
68
+ # -- GUI --
69
+
70
+ # Setting up containers for user input and model response
71
+ response_container = st.container()
72
+ container = st.container()
73
+
74
+
75
+
76
+ # Setting up the user input form
77
+ with container:
78
+ with st.form(key='my_form', clear_on_submit=True):
79
+ user_input = st.text_area("Your question goes here:", key='input', height=100)
80
+ col1, col2 = st.columns(2)
81
+ with col1:
82
+ submit_button = st.form_submit_button(label='Send')
83
+ with col2:
84
+ clear_button = st.form_submit_button(label="Clear")
85
+
86
+ # Handling form submission
87
+ if submit_button:
88
+ st.session_state['messages'].append(user_input)
89
+ model_response=getresponse(user_input)
90
+ st.session_state['messages'].append(model_response)
91
+
92
+ # Displaying the conversation
93
+ with response_container:
94
+ for i in range(len(st.session_state['messages'])):
95
+ if (i % 2) == 0:
96
+ message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
97
+ else:
98
+ message(st.session_state['messages'][i], key=str(i) + '_AI')
99
+
100
+ # Handling clear button click
101
+ if clear_button:
102
+ clear()