Spaces:
Running
Running
Wilame Lima
commited on
Commit
•
fdbec52
1
Parent(s):
987f564
First commit
Browse files- .gitignore +2 -0
- README.md +36 -1
- app.py +89 -0
- config.py +13 -0
- functions.py +1 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
*.pyc
|
2 |
+
*.env
|
README.md
CHANGED
@@ -9,4 +9,39 @@ app_file: app.py
|
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
+
# Marketer Chatbot Project
|
13 |
+
|
14 |
+
## Overview
|
15 |
+
Marketer Chatbot is a Python-based project designed to provide a simple interface for users to interact with a chatbot behaving like a marketer. The chatbot is built using the Streamlit library and Hugging Face Inference API, together with a Llama-family 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 marketer_chatbot
|
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,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
Meta Llama 3 8B Instruct is part of a family of large language models (LLMs) optimized for dialogue tasks.
|
11 |
+
|
12 |
+
This project uses Streamlit to create a simple chatbot interface that allows you to chat with the model using the Hugging Face Inference API.
|
13 |
+
|
14 |
+
Ask the model marketing-related questions and see how it responds. Have fun!
|
15 |
+
|
16 |
+
Model used: [{MODEL_PATH}]({MODEL_LINK})
|
17 |
+
"""
|
18 |
+
)
|
19 |
+
|
20 |
+
first_assistant_message = "Hello! I am Marketing expert. What can I help you with today?"
|
21 |
+
|
22 |
+
# clear conversation
|
23 |
+
if st.sidebar.button("Clear conversation"):
|
24 |
+
chat_history = [{'role':'assistant', 'content':first_assistant_message}]
|
25 |
+
st.session_state['chat_history'] = chat_history
|
26 |
+
st.rerun()
|
27 |
+
|
28 |
+
# Get the chat history
|
29 |
+
if "chat_history" not in st.session_state:
|
30 |
+
chat_history = [{'role':'assistant', 'content':first_assistant_message}]
|
31 |
+
st.session_state['chat_history'] = chat_history
|
32 |
+
else:
|
33 |
+
chat_history = st.session_state['chat_history']
|
34 |
+
|
35 |
+
# print the conversation
|
36 |
+
for message in chat_history:
|
37 |
+
with st.chat_message(message['role']):
|
38 |
+
st.write(message['content'])
|
39 |
+
|
40 |
+
# keep only last 10 messages
|
41 |
+
shorter_history = [message for message in chat_history[-10:] if 'content' in message]
|
42 |
+
|
43 |
+
# include a system prompt to explain the bot what to do
|
44 |
+
system_prompt = """For this task, you are a Marketer specialized in E-commerce helping a user with marketing-related questions. Provide insights and recommendations based on the user's questions. Don't write more than 3-4 sentences per response."""
|
45 |
+
shorter_history = [{'role': 'system', 'content': system_prompt}] + shorter_history
|
46 |
+
|
47 |
+
# get the input from user
|
48 |
+
user_input = st.chat_input("Write something...")
|
49 |
+
|
50 |
+
if user_input:
|
51 |
+
|
52 |
+
with st.chat_message("user"):
|
53 |
+
st.write(user_input)
|
54 |
+
|
55 |
+
# make the request
|
56 |
+
with st.spinner("Generating the response..."):
|
57 |
+
|
58 |
+
client = InferenceClient(
|
59 |
+
"meta-llama/Meta-Llama-3-8B-Instruct",
|
60 |
+
token=HUGGING_FACE_API_KEY,
|
61 |
+
)
|
62 |
+
|
63 |
+
messages = shorter_history + [{'role': 'user', 'content': user_input}]
|
64 |
+
|
65 |
+
# query the model
|
66 |
+
try:
|
67 |
+
response = client.chat_completion(
|
68 |
+
messages=messages,
|
69 |
+
max_tokens = 500,
|
70 |
+
stream = False,
|
71 |
+
)
|
72 |
+
|
73 |
+
# get the response
|
74 |
+
message = response.choices[0].message['content']
|
75 |
+
|
76 |
+
# append to the history
|
77 |
+
chat_history.append({'content':user_input, 'role':'user'})
|
78 |
+
chat_history.append(response.choices[0].message) # append the response
|
79 |
+
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"An error occurred: {e}")
|
82 |
+
st.stop()
|
83 |
+
|
84 |
+
st.session_state['chat_history'] = chat_history
|
85 |
+
st.rerun()
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
config.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
import os
|
5 |
+
|
6 |
+
# load variables from the env file
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
HUGGING_FACE_API_KEY = os.environ.get('HUGGING_FACE_API_KEY', None)
|
10 |
+
|
11 |
+
DASHBOARD_TITLE = "The Marketer Chatbot"
|
12 |
+
MODEL_PATH = "meta-llama/Meta-Llama-3-8B-Instruct"
|
13 |
+
MODEL_LINK = f"https://huggingface.co/{MODEL_PATH}"
|
functions.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from config import *
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
python-dotenv
|
3 |
+
huggingface_hub
|