Prathamesh1420 commited on
Commit
1abc3a3
·
verified ·
1 Parent(s): c3940ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -83
app.py CHANGED
@@ -1,83 +1,84 @@
1
- import streamlit as st
2
- from streamlit_chat import message
3
- # from langchain.llms import OpenAI #This import has been replaced by the below import please
4
- from langchain_openai import OpenAI
5
- from langchain.chains import ConversationChain
6
- from langchain.chains.conversation.memory import (ConversationBufferMemory,
7
- ConversationSummaryMemory,
8
- ConversationBufferWindowMemory
9
-
10
- )
11
-
12
- if 'conversation' not in st.session_state:
13
- st.session_state['conversation'] =None
14
- if 'messages' not in st.session_state:
15
- st.session_state['messages'] =[]
16
- if 'API_Key' not in st.session_state:
17
- st.session_state['API_Key'] =''
18
-
19
- # Setting page title and header
20
- st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
21
- st.markdown("<h1 style='text-align: center;'>How can I assist you? </h1>", unsafe_allow_html=True)
22
-
23
-
24
- st.sidebar.title("😎")
25
- st.session_state['API_Key']= st.sidebar.text_input("What's your API key?",type="password")
26
- summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
27
- if summarise_button:
28
- summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
29
- #summarise_placeholder.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
30
-
31
- #import os
32
- #os.environ["OPENAI_API_KEY"] = "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"
33
-
34
- def getresponse(userInput, api_key):
35
-
36
- if st.session_state['conversation'] is None:
37
-
38
- llm = OpenAI(
39
- temperature=0,
40
- openai_api_key=api_key,
41
- model_name='gpt-3.5-turbo-instruct' # 'text-davinci-003' model is depreciated now, so we are using the openai's recommended model
42
- )
43
-
44
- st.session_state['conversation'] = ConversationChain(
45
- llm=llm,
46
- verbose=True,
47
- memory=ConversationSummaryMemory(llm=llm)
48
- )
49
-
50
- response=st.session_state['conversation'].predict(input=userInput)
51
- print(st.session_state['conversation'].memory.buffer)
52
-
53
-
54
- return response
55
-
56
-
57
-
58
- response_container = st.container()
59
- # Here we will have a container for user input text box
60
- container = st.container()
61
-
62
-
63
- with container:
64
- with st.form(key='my_form', clear_on_submit=True):
65
- user_input = st.text_area("Your question goes here:", key='input', height=100)
66
- submit_button = st.form_submit_button(label='Send')
67
-
68
- if submit_button:
69
- st.session_state['messages'].append(user_input)
70
- model_response=getresponse(user_input,st.session_state['API_Key'])
71
- st.session_state['messages'].append(model_response)
72
-
73
-
74
- with response_container:
75
- for i in range(len(st.session_state['messages'])):
76
- if (i % 2) == 0:
77
- message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
78
- else:
79
- message(st.session_state['messages'][i], key=str(i) + '_AI')
80
-
81
-
82
-
83
-
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ # from langchain.llms import OpenAI #This import has been replaced by the below import please
4
+ from langchain_openai import OpenAI
5
+ from langchain.chains import ConversationChain
6
+ from langchain.chains.conversation.memory import (ConversationBufferMemory,
7
+ ConversationSummaryMemory,
8
+ ConversationBufferWindowMemory
9
+
10
+ )
11
+
12
+ if 'conversation' not in st.session_state:
13
+ st.session_state['conversation'] =None
14
+ if 'messages' not in st.session_state:
15
+ st.session_state['messages'] =[]
16
+ if 'API_Key' not in st.session_state:
17
+ st.session_state['API_Key'] =''
18
+
19
+ # Setting page title and header
20
+ st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
21
+ st.markdown("<h1 style='text-align: center;'>How can I assist you? </h1>", unsafe_allow_html=True)
22
+
23
+
24
+ st.sidebar.title("😎")
25
+ groq_api_key=st.sidebar.text_input(label="Groq API Key",type="password")
26
+ summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
27
+ if summarise_button:
28
+ summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
29
+ #summarise_placeholder.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
30
+
31
+
32
+ def getresponse(userInput, api_key):
33
+
34
+ if st.session_state['conversation'] is None:
35
+
36
+ '''llm = OpenAI(
37
+ temperature=0,
38
+ openai_api_key=api_key,
39
+ model_name='gpt-3.5-turbo-instruct' # 'text-davinci-003' model is depreciated now, so we are using the openai's recommended model
40
+ )'''
41
+
42
+ llm=ChatGroq(model="Gemma2-9b-It",groq_api_key=groq_api_key)
43
+
44
+
45
+ st.session_state['conversation'] = ConversationChain(
46
+ llm=llm,
47
+ verbose=True,
48
+ memory=ConversationSummaryMemory(llm=llm)
49
+ )
50
+
51
+ response=st.session_state['conversation'].predict(input=userInput)
52
+ print(st.session_state['conversation'].memory.buffer)
53
+
54
+
55
+ return response
56
+
57
+
58
+
59
+ response_container = st.container()
60
+ # Here we will have a container for user input text box
61
+ container = st.container()
62
+
63
+
64
+ with container:
65
+ with st.form(key='my_form', clear_on_submit=True):
66
+ user_input = st.text_area("Your question goes here:", key='input', height=100)
67
+ submit_button = st.form_submit_button(label='Send')
68
+
69
+ if submit_button:
70
+ st.session_state['messages'].append(user_input)
71
+ model_response=getresponse(user_input,st.session_state['API_Key'])
72
+ st.session_state['messages'].append(model_response)
73
+
74
+
75
+ with response_container:
76
+ for i in range(len(st.session_state['messages'])):
77
+ if (i % 2) == 0:
78
+ message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
79
+ else:
80
+ message(st.session_state['messages'][i], key=str(i) + '_AI')
81
+
82
+
83
+
84
+