Wilame Lima commited on
Commit ·
40b3f8e
1
Parent(s): 7fe48f7
First commit
Browse files- .gitignore +1 -0
- README.md +36 -1
- app.py +77 -0
- config.py +7 -0
- functions.py +1 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
*.pyc
|
README.md
CHANGED
|
@@ -9,4 +9,39 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Dumbot Project
|
| 13 |
+
|
| 14 |
+
## Overview
|
| 15 |
+
Dumbot is a Python-based project designed to provide a simple interface for users to interact with a chatbot. The chatbot is built using the Streamlit library and Facebook blenderbot model.
|
| 16 |
+
|
| 17 |
+
## Contents
|
| 18 |
+
- `functions.py`: Contains various functions used in the project.
|
| 19 |
+
- `config.py`: Configuration settings for the project.
|
| 20 |
+
- `requirements.txt`: Lists the dependencies required to run the project.
|
| 21 |
+
- `app.py`: The main application file.
|
| 22 |
+
- `.gitignore`: Specifies files and directories to be ignored by git.
|
| 23 |
+
- `.gitattributes`: Configuration for git attributes.
|
| 24 |
+
|
| 25 |
+
## Getting Started
|
| 26 |
+
|
| 27 |
+
### Prerequisites
|
| 28 |
+
Ensure you have the following installed:
|
| 29 |
+
- Python 3.x
|
| 30 |
+
- pip (Python package installer)
|
| 31 |
+
|
| 32 |
+
### Installation
|
| 33 |
+
1. Clone the repository:
|
| 34 |
+
2. Navigate to the project directory:
|
| 35 |
+
```sh
|
| 36 |
+
cd dumbot
|
| 37 |
+
```
|
| 38 |
+
3. Install the required dependencies:
|
| 39 |
+
```sh
|
| 40 |
+
pip install -r requirements.txt
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### Running the Application
|
| 44 |
+
Run the main application file:
|
| 45 |
+
```sh
|
| 46 |
+
streamlit run app.py
|
| 47 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functions import *
|
| 2 |
+
|
| 3 |
+
# set the title
|
| 4 |
+
st.sidebar.title(DASHBOARD_TITLE)
|
| 5 |
+
info_section = st.empty()
|
| 6 |
+
|
| 7 |
+
# add an explanation of what is NER and why it is important for medical tasks
|
| 8 |
+
st.sidebar.markdown(
|
| 9 |
+
f"""
|
| 10 |
+
Facebook blenderbot is a family of conversational models that are trained on a large dataset of conversations and can generate limited, yet sometimes coherent responses.
|
| 11 |
+
|
| 12 |
+
For this project, we are using the 400M distill version of the model. This model is smaller and faster than the original model, but it may not be as accurate. I have used Streamlit to create a simple chatbot interface that allows you to chat with the model and demonstrate how easy it is to use these models for conversational AI tasks.
|
| 13 |
+
|
| 14 |
+
Have fun, but don't expect too much from the model! It is a little dumb sometimes.
|
| 15 |
+
|
| 16 |
+
Model used: [{MODEL_PATH}]({MODEL_LINK})
|
| 17 |
+
"""
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
first_assistant_message = "Hello! I am a dumb bot. What is your dumb question?"
|
| 22 |
+
|
| 23 |
+
# clear conversation
|
| 24 |
+
if st.sidebar.button("Clear conversation"):
|
| 25 |
+
chat_history = [{'user':'assistant', 'content':first_assistant_message}]
|
| 26 |
+
st.session_state['chat_history'] = chat_history
|
| 27 |
+
st.rerun()
|
| 28 |
+
|
| 29 |
+
# Get the chat history
|
| 30 |
+
if "chat_history" not in st.session_state:
|
| 31 |
+
chat_history = [{'user':'assistant', 'content':first_assistant_message}]
|
| 32 |
+
st.session_state['chat_history'] = chat_history
|
| 33 |
+
else:
|
| 34 |
+
chat_history = st.session_state['chat_history']
|
| 35 |
+
|
| 36 |
+
# print the conversation
|
| 37 |
+
for message in chat_history:
|
| 38 |
+
with st.chat_message(message['user']):
|
| 39 |
+
st.write(message['content'])
|
| 40 |
+
|
| 41 |
+
# convert the chat history to a string to be passed to the model
|
| 42 |
+
# keep only last 4 messages
|
| 43 |
+
chat_history_str = "\n".join([message['content'] for message in chat_history[-4:] if 'content' in message])
|
| 44 |
+
|
| 45 |
+
# get the input from user
|
| 46 |
+
user_input = st.chat_input("Write something...")
|
| 47 |
+
if user_input:
|
| 48 |
+
|
| 49 |
+
with st.chat_message("user"):
|
| 50 |
+
st.write(user_input)
|
| 51 |
+
|
| 52 |
+
# load the tokenizer
|
| 53 |
+
info_section.info("Loading the tokenizer. This may take a while...")
|
| 54 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 55 |
+
inputs = tokenizer.encode_plus(chat_history_str,
|
| 56 |
+
user_input,
|
| 57 |
+
return_tensors="pt")
|
| 58 |
+
|
| 59 |
+
# get the model's response
|
| 60 |
+
info_section.info("Loading the model. This also may take a while...")
|
| 61 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH)
|
| 62 |
+
info_section.empty()
|
| 63 |
+
|
| 64 |
+
with st.spinner("Generating the response..."):
|
| 65 |
+
|
| 66 |
+
# generate the response
|
| 67 |
+
outputs = model.generate(**inputs)
|
| 68 |
+
# decode the outputs
|
| 69 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 70 |
+
|
| 71 |
+
# append to the history
|
| 72 |
+
chat_history.append({'content':user_input, 'user':'user'})
|
| 73 |
+
chat_history.append({'content':response, 'user':'assistant'})
|
| 74 |
+
|
| 75 |
+
st.session_state['chat_history'] = chat_history
|
| 76 |
+
st.rerun()
|
| 77 |
+
|
config.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
DASHBOARD_TITLE = "Dumb Chatbot"
|
| 6 |
+
MODEL_PATH = "facebook/blenderbot-400M-distill"
|
| 7 |
+
MODEL_LINK = f"https://huggingface.co/{MODEL_PATH}"
|
functions.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from config import *
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
tf-keras
|
| 4 |
+
transformers==4.30.2
|
| 5 |
+
torch
|