ricardo-lsantos commited on
Commit
c9720d8
1 Parent(s): d60d98d

Added a simple chat app for talking with a Tony Stark Model

Browse files
Files changed (6) hide show
  1. .gitignore +2 -0
  2. Models/HFAgent.py +37 -0
  3. Models/__init__.py +0 -0
  4. README.md +9 -0
  5. app.py +44 -19
  6. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .venv
2
+ __pycache__
Models/HFAgent.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ # MODEL_CKPT = "HuggingFaceH4/zephyr-7b-beta"
4
+ MODEL_CKPT = "AVeryRealHuman/DialoGPT-small-TonyStark"
5
+
6
+ class HFAgent:
7
+ def __init__(self):
8
+ self.pipe = pipeline("conversational", MODEL_CKPT)
9
+
10
+ def generate(self, chat_history):
11
+ return self.pipe(chat_history)
12
+
13
+ def __call__(self, chat_history):
14
+ return self.generate(chat_history)
15
+
16
+ def __repr__(self):
17
+ return f"HFAgent(model={self.pipe.model})"
18
+
19
+ def __str__(self):
20
+ return f"HFAgent(model={self.pipe.model})"
21
+
22
+
23
+ ## For testing purposes
24
+ # def main():
25
+ # agent = HFAgent()
26
+ # messages = [
27
+ # {
28
+ # "role": "system",
29
+ # "content": "You are a friendly chatbot who always responds in the style of a pirate",
30
+ # },
31
+ # {"role": "user", "content": "How many hotdogs can a human eat in one sitting?"},
32
+ # ]
33
+ # new_messages = agent(messages)
34
+ # print(new_messages[-1])
35
+
36
+ # if __name__ == "__main__":
37
+ # main()
Models/__init__.py ADDED
File without changes
README.md CHANGED
@@ -9,4 +9,13 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
 
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
9
  pinned: false
10
  ---
11
 
12
+ Simple test application to view streamlit components.
13
+
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
15
+
16
+ ## References
17
+
18
+ * https://huggingface.co/docs/transformers/chat_templating
19
+
20
+ * https://huggingface.co/AVeryRealHuman/DialoGPT-small-TonyStark?text=Hey+my+name+is+Julien%21+How+are+you%3F
21
+
app.py CHANGED
@@ -1,21 +1,46 @@
 
1
  import streamlit as st
 
2
 
3
- data = ""
4
-
5
- st.button('Hit me')
6
- # st.data_editor('Edit data', data)
7
- st.checkbox('Check me out')
8
- st.radio('Pick one:', ['nose','ear'])
9
- st.selectbox('Select', [1,2,3])
10
- st.multiselect('Multiselect', [1,2,3])
11
- st.slider('Slide me', min_value=0, max_value=10)
12
- st.select_slider('Slide to select', options=[1,'2'])
13
- st.text_input('Enter some text')
14
- st.number_input('Enter a number')
15
- st.text_area('Area for textual entry')
16
- st.date_input('Date input')
17
- st.time_input('Time entry')
18
- st.file_uploader('File uploader')
19
- st.download_button('On the dl', data)
20
- st.camera_input("Hello Camera!")
21
- st.color_picker('Pick a color')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import openai
2
  import streamlit as st
3
+ from Models import HFAgent
4
 
5
+ def initMessages():
6
+ if "messages" not in st.session_state:
7
+ st.session_state["messages"] = [{"role": "assistant", "content": "Hello, Sir. How can I assist you today, Sir?"}]
8
+
9
+ def showMessages():
10
+ for msg in st.session_state.messages:
11
+ st.chat_message(msg["role"]).write(msg["content"])
12
+
13
+ # def sidebar():
14
+ # with st.sidebar:
15
+ # st.title("Settings")
16
+
17
+ def appendMessage(role, content):
18
+ st.session_state.messages.append({"role": role, "content": content})
19
+
20
+ def writeMessage(role, content):
21
+ st.chat_message(role).write(content)
22
+
23
+
24
+ def generateHFResponse():
25
+ if "agent" not in st.session_state:
26
+ st.session_state["agent"] = HFAgent.HFAgent()
27
+ agent = st.session_state.agent
28
+ new_messages = agent(st.session_state.messages)
29
+ return new_messages[-1]
30
+
31
+ def app():
32
+
33
+ # sidebar()
34
+ st.title("💬 Chatbot with Jarvis")
35
+ initMessages()
36
+ showMessages()
37
+
38
+ if prompt := st.chat_input():
39
+ appendMessage("user", prompt)
40
+ # writeMessage("user", prompt)
41
+ msg = generateHFResponse()
42
+ appendMessage(msg["role"], msg["content"])
43
+ # writeMessage(msg["role"], msg["content"])
44
+
45
+ if __name__ == "__main__":
46
+ app()
requirements.txt CHANGED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ torch
3
+ transformers