mdr123 commited on
Commit
c2b9ef6
1 Parent(s): 5dd38be

Upload 3 files

Browse files
Files changed (3) hide show
  1. .env-sample +1 -1
  2. app.py +93 -19
  3. requirements.txt +2 -2
.env-sample CHANGED
@@ -1 +1 @@
1
- HF=
 
1
+ OPENAI_API_KEY=""
app.py CHANGED
@@ -1,19 +1,93 @@
1
- import streamlit as st
2
- import streamlit_chat
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
- import requests
5
- import os
6
-
7
-
8
- hf = os.getenv("HF")
9
-
10
- st.title("Question Answering System")
11
- st.write(
12
- "This is a simple question answering system that uses a pre-trained model to answer questions."
13
- )
14
-
15
-
16
-
17
-
18
-
19
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, api_key):
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=api_key,
43
+ model_name='gpt-3.5-turbo-instruct'
44
+ )
45
+ st.session_state['conversation'] = ConversationChain(
46
+ llm=llm,
47
+ verbose=True,
48
+ memory=ConversationSummaryMemory(llm=llm)
49
+ )
50
+ response=st.session_state['conversation'].predict(input=userInput)
51
+ print(st.session_state['conversation'].memory.buffer)
52
+ return response
53
+
54
+ # Setting up containers for user input and model response
55
+ response_container = st.container()
56
+ container = st.container()
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
+ # Setting up the user input form
68
+ with container:
69
+ with st.form(key='my_form', clear_on_submit=True):
70
+ user_input = st.text_area("Your question goes here:", key='input', height=100)
71
+ col1, col2 = st.columns(2)
72
+ with col1:
73
+ submit_button = st.form_submit_button(label='Send')
74
+ with col2:
75
+ clear_button = st.form_submit_button(label="Clear")
76
+
77
+ # Handling form submission
78
+ if submit_button:
79
+ st.session_state['messages'].append(user_input)
80
+ model_response=getresponse(user_input,st.session_state['API_Key'])
81
+ st.session_state['messages'].append(model_response)
82
+
83
+ # Displaying the conversation
84
+ with response_container:
85
+ for i in range(len(st.session_state['messages'])):
86
+ if (i % 2) == 0:
87
+ message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
88
+ else:
89
+ message(st.session_state['messages'][i], key=str(i) + '_AI')
90
+
91
+ # Handling clear button click
92
+ if clear_button:
93
+ clear()
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- streamlit
2
- transformers
 
1
+ langchain
2
+ streamlit