ULMER Louis (T0240644) commited on
Commit
260934b
β€’
1 Parent(s): 5109849

add application file

Browse files
Files changed (6) hide show
  1. Dockerfile +16 -0
  2. README.md +6 -7
  3. app.py +181 -0
  4. env.yml +16 -0
  5. launch.sh +1 -0
  6. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ ENV HTTP_PROXY=http://10.31.255.65:8080
6
+ ENV HTTPS_PROXY=http://10.31.255.65:8080
7
+
8
+
9
+ COPY ./requirements.txt /code/requirements.txt
10
+
11
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
+
13
+
14
+ COPY . .
15
+
16
+ ENTRYPOINT ["sh","./launch.sh"]
README.md CHANGED
@@ -1,11 +1,10 @@
1
  ---
2
- title: Homemade Gpt
3
- emoji: 🐠
4
- colorFrom: pink
5
- colorTo: yellow
6
  sdk: docker
7
- pinned: false
8
- license: openrail
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Basic Docker SDK Space
3
+ emoji: 🐳
4
+ colorFrom: purple
5
+ colorTo: gray
6
  sdk: docker
7
+ app_port: 8501
 
8
  ---
9
 
10
+
app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st # import the Streamlit library
2
+ from langchain.chains import ConversationChain
3
+ from langchain.llms import OpenAIChat # import OpenAI model
4
+ from langchain.chains.conversation.memory import ConversationEntityMemory
5
+ from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
6
+
7
+ import pickle
8
+
9
+ # Initialize session State
10
+ st.session_state["show_new_chat_button"] = False
11
+ if "id" not in st.session_state:
12
+ st.session_state["id"] = 0
13
+ if "conversation" not in st.session_state:
14
+ st.session_state.conversation = []
15
+ if "input" not in st.session_state:
16
+ st.session_state["input"] = ""
17
+ if "stored_session" not in st.session_state:
18
+ st.session_state["stored_session"]={}
19
+ if "input_temp" not in st.session_state:
20
+ st.session_state["input_temp"] = ""
21
+
22
+ # Set the title of the Streamlit app
23
+ st.title("HomemadeGPT πŸ€– - The custom chatbot you need")
24
+
25
+ # Historique des conversations
26
+ conversation_history = st.empty()
27
+
28
+ API_KEY = st.sidebar.text_input("API-Key", type="password")
29
+
30
+
31
+ with st.sidebar.expander(" πŸ› οΈ Settings ", expanded=False):
32
+ # Option to preview memory store
33
+ if 'entity_memory' in st.session_state:
34
+ if st.checkbox("Preview memory store"):
35
+ st.write(st.session_state.entity_memory.store)
36
+ # Option to preview memory buffer
37
+ if st.checkbox("Preview memory buffer"):
38
+ st.write(st.session_state.entity_memory.buffer)
39
+ MODEL = st.selectbox(label='Model', options=['gpt-3.5-turbo','gpt-4','gpt-4-32k','text-davinci-003','text-davinci-002'])
40
+ K = st.number_input(' (#)Summary of prompts to consider',min_value=3,max_value=1000)
41
+
42
+
43
+ def clear_text():
44
+ """
45
+ A function that clears the text in the input box when the user type a search query and press enter
46
+ """
47
+ st.session_state["input_temp"] = st.session_state["input"]
48
+ st.session_state["input"] = ""
49
+
50
+ def get_text():
51
+ """
52
+ Get the user input text.
53
+ Returns:
54
+ (str): The text entered by the user
55
+ """
56
+ input_text = st.text_input("You: ", key="input", placeholder = "Your AI assistant ! Ask me anything...", label_visibility='hidden',on_change=clear_text)
57
+ return input_text
58
+
59
+ def new_chat():
60
+ """
61
+ Clears session state and start a new chat
62
+ """
63
+ save_current_chat()
64
+ clean_screen()
65
+ clean_memory()
66
+ st.session_state["id"] += 1
67
+
68
+ def clean_screen():
69
+ """
70
+ Clears the current conversation screen
71
+ """
72
+ st.session_state.conversation = []
73
+ st.session_state["input"] = ""
74
+ st.session_state["input_temp"] = ""
75
+
76
+ def clean_memory():
77
+ """
78
+ Clears the current conversation memory
79
+ """
80
+ st.session_state.entity_memory.store = {}
81
+ st.session_state.entity_memory.buffer.clear()
82
+
83
+ def save_current_chat():
84
+ """
85
+ Save the current chat in st.session_state["stored_session"]
86
+ """
87
+ saved_dict=dict()
88
+ saved_dict['conversation'] = st.session_state['conversation']
89
+ saved_dict['conversation_memory'] = pickle.dumps(st.session_state.entity_memory)
90
+
91
+ st.session_state["stored_session"][st.session_state["id"]]=saved_dict
92
+
93
+ def resume_chat(session_id):
94
+ """
95
+ Clears session state and start a new chat
96
+ """
97
+ save_current_chat()
98
+ clean_screen()
99
+ clean_memory()
100
+ st.session_state["id"] = session_id
101
+ st.session_state["conversation"] = st.session_state["stored_session"][session_id]["conversation"]
102
+ st.session_state.entity_memory = pickle.loads(st.session_state["stored_session"][session_id]["conversation_memory"])
103
+ st.session_state["show_new_chat_button"] = True
104
+
105
+ def show_conv():
106
+ """
107
+ Render the current conversation in html
108
+ """
109
+ conversation_html = ""
110
+ for entry in st.session_state.conversation:
111
+ if 'user' in entry:
112
+ conversation_html += f'<div style="margin: 10px; padding: 8px; border-radius: 5px; background-color: #8090FF; text-align: left;">🀡 {entry["user"]}</div>'
113
+ if 'chatbot' in entry:
114
+ conversation_html += f'<div style="margin: 10px; padding: 8px; border-radius: 5px; background-color: #D7BB2C; display: flex; align-items: center;">πŸ€– <pre style="color: white; background-color: #D7BB2C; padding: 8px; border-radius: 5px; max-width: calc(100% - 60px); white-space: pre-wrap; word-wrap: break-word; word-break: break-all;">{entry["chatbot"]}</pre></div>'
115
+
116
+ conversation_history.write(conversation_html, unsafe_allow_html=True)
117
+
118
+ ### Main APP
119
+ # Allow the user to clear all stored conversation sessions
120
+ if st.session_state.stored_session:
121
+ if st.sidebar.button("Clear-all"):
122
+ st.session_state.stored_session={}
123
+ clean_screen()
124
+
125
+ if API_KEY :
126
+ # Create an Open AI instance
127
+ llm = OpenAIChat(
128
+ temperature=0,
129
+ openai_api_key=API_KEY,
130
+ model_name = MODEL
131
+ )
132
+
133
+ # Create conversation memory
134
+ if 'entity_memory' not in st.session_state:
135
+ st.session_state.entity_memory= ConversationEntityMemory(llm=llm, k=K)
136
+
137
+ # Create the Conversation Chain
138
+
139
+ st.session_state.Conversation = ConversationChain(llm=llm,
140
+ prompt = ENTITY_MEMORY_CONVERSATION_TEMPLATE,
141
+ memory = st.session_state.entity_memory)
142
+ else :
143
+ st.markdown('''
144
+ ```
145
+ - 1. Enter API Key + Hit enter πŸ”
146
+
147
+ - 2. Ask anything via the text input widget
148
+ ```
149
+
150
+ ''')
151
+ st.sidebar.warning('API key required to try this app.The API key is not stored in any form.')
152
+ st.sidebar.info("Your API-key is not stored in any form by this app. However, for transparency ensure to delete your API once used.")
153
+
154
+ # Get the user input
155
+ user_input = get_text()
156
+
157
+ if st.session_state["input_temp"] :
158
+ output = st.session_state.Conversation.run(input=st.session_state["input_temp"])
159
+ st.session_state.conversation.append({"user": st.session_state["input_temp"]})
160
+ st.session_state.conversation.append({"chatbot": output})
161
+
162
+ st.session_state["show_new_chat_button"] = True
163
+
164
+
165
+ if st.session_state["show_new_chat_button"] :
166
+ st.sidebar.button("New Chat", on_click=new_chat, type='primary')
167
+
168
+ if "conversation" in st.session_state:
169
+ show_conv()
170
+
171
+ if st.session_state.stored_session.values():
172
+ # Display stored conversation sessions in the sidebar
173
+ for i, sublist in enumerate(st.session_state.stored_session.values()):
174
+ with st.sidebar.expander(label= f"Conversation-Session:{i}"):
175
+ st.button("Resume session", on_click=resume_chat,kwargs={"session_id":i},type='primary', key=f"Conversation-Session:{i}")
176
+ st.markdown(sublist)
177
+
178
+
179
+
180
+
181
+
env.yml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: chatgpt
2
+
3
+ channels:
4
+ - conda-forge
5
+
6
+ dependencies:
7
+ - mamba
8
+ - openai
9
+ - tqdm
10
+ - ipykernel
11
+ - langchain
12
+ - streamlit
13
+
14
+
15
+
16
+
launch.sh ADDED
@@ -0,0 +1 @@
 
 
1
+ streamlit run app.py --browser.serverAddress "0.0.0.0" --browser.serverPort "7681"
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ openai
2
+ langchain==0.0.131
3
+ streamlit
4
+
5
+
6
+
7
+