Spaces:
Sleeping
Sleeping
Commit
·
c9720d8
1
Parent(s):
d60d98d
Added a simple chat app for talking with a Tony Stark Model
Browse files- .gitignore +2 -0
- Models/HFAgent.py +37 -0
- Models/__init__.py +0 -0
- README.md +9 -0
- app.py +44 -19
- 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 |
-
|
| 4 |
-
|
| 5 |
-
st.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
st.
|
| 9 |
-
st.
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
st.
|
| 13 |
-
st.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
st.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
st.
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|