lorentz commited on
Commit
7b0adee
1 Parent(s): 66b171e

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +5 -5
  2. app.py +79 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: Llm 4
3
- emoji: 🌍
4
  colorFrom: pink
5
- colorTo: green
6
  sdk: streamlit
7
- sdk_version: 1.30.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Lorentz's LLM ChatGPT Clone
3
+ emoji: 💻
4
  colorFrom: pink
5
+ colorTo: purple
6
  sdk: streamlit
7
+ sdk_version: 1.21.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
12
+ # 3 variables in session_state
13
+ if 'conversation' not in st.session_state:
14
+ st.session_state['conversation'] =None
15
+ if 'messages' not in st.session_state:
16
+ st.session_state['messages'] =[]
17
+ if 'API_Key' not in st.session_state:
18
+ st.session_state['OPENAI_API_KEY'] =''
19
+
20
+ # Setting page title and header
21
+ st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
22
+ st.markdown("<h1 style='text-align: center;'>How can I assist you? </h1>", unsafe_allow_html=True)
23
+
24
+
25
+ st.sidebar.title("😎")
26
+ st.session_state['OPENAI_API_KEY']= st.sidebar.text_input("What's your API key?",type="password")
27
+ summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
28
+ if summarise_button:
29
+ summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
30
+ #summarise_placeholder.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
31
+
32
+
33
+ def getresponse(userInput, api_key):
34
+
35
+ if st.session_state['conversation'] is None:
36
+
37
+ # create a LLM
38
+ llm = ChatOpenAI(
39
+ temperature=0,
40
+ openai_api_key=api_key,
41
+ model_name='gpt-3.5-turbo'
42
+ )
43
+
44
+ # store the placeholder as "conversation" in session_state
45
+ st.session_state['conversation'] = ConversationChain(
46
+ llm=llm,
47
+ verbose=True,
48
+ memory=ConversationSummaryMemory(llm=llm)
49
+ )
50
+
51
+ # call st.session_state['conversation'] to create dialogues
52
+ response=st.session_state['conversation'].predict(input=userInput)
53
+ print(st.session_state['conversation'].memory.buffer)
54
+
55
+ return response
56
+
57
+ response_container = st.container()
58
+ # Here we will have a container for user input text box
59
+ container = st.container()
60
+
61
+ with container:
62
+ with st.form(key='my_form', clear_on_submit=True):
63
+ user_input = st.text_area("Your question goes here:", key='input', height=100)
64
+ submit_button = st.form_submit_button(label='Send')
65
+
66
+ if submit_button:
67
+ # store the user input text into st.session_state['messages']
68
+ st.session_state['messages'].append(user_input)
69
+ # getresponse is the function we just created, a wrapper of predict()
70
+ # model_response = getresponse(user_input,st.session_state['API_Key'])
71
+ model_response = getresponse(user_input,st.session_state['OPENAI_API_KEY'])
72
+ st.session_state['messages'].append(model_response)
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')
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ langchain
2
+ streamlit
3
+ langchain_openai