RomyMy commited on
Commit
6f8d992
β€’
1 Parent(s): fbde06d

deploy app

Browse files
Files changed (7) hide show
  1. .env_example +2 -1
  2. main.py β†’ app.py +83 -6
  3. chatbot.py +0 -47
  4. database.py +0 -2
  5. preprocess.py +1 -0
  6. readme.md +10 -0
  7. requirements.txt +1 -0
.env_example CHANGED
@@ -1,2 +1,3 @@
1
  REDIS_KEY = ''
2
- OPENAI_API_KEY = ''
 
 
1
  REDIS_KEY = ''
2
+ OPENAI_API_KEY = ''
3
+ HUGGINGFACEHUB_API_TOKEN = ''
main.py β†’ app.py RENAMED
@@ -1,29 +1,106 @@
1
  import streamlit as st
2
- from chatbot import llm_chain, chain
3
  from sentence_transformers import SentenceTransformer
4
  from redis.commands.search.query import Query
5
- from database import redis_conn
 
 
 
 
 
 
 
 
6
  import numpy as np
7
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  st.title('My Amazon shopping buddy 🏷️')
11
  st.caption('πŸ€– Powered by Falcon Open Source AI model')
12
- st.session_state['disabled']= False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  if "messages" not in st.session_state:
15
  st.session_state["messages"] = [{"role": "assistant", "content": "Hey im your online shopping buddy, how can i help you today?"}]
16
  for msg in st.session_state["messages"]:
17
  st.chat_message(msg["role"]).write(msg["content"])
18
 
19
- prompt = st.chat_input(key="user_input",disabled=st.session_state.disabled )
20
- embedding_model = SentenceTransformer('sentence-transformers/all-distilroberta-v1')
21
  if prompt:
22
  st.session_state["messages"].append({"role": "user", "content": prompt})
23
  st.chat_message('user').write(prompt)
24
  st.session_state.disabled = True
25
  keywords = chain.run(prompt)
26
-
27
  #vectorize the query
28
  query_vector = embedding_model.encode(keywords)
29
  query_vector = np.array(query_vector).astype(np.float32).tobytes()
 
1
  import streamlit as st
 
2
  from sentence_transformers import SentenceTransformer
3
  from redis.commands.search.query import Query
4
+ import redis
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain import HuggingFaceHub
7
+ from langchain.chains import LLMChain
8
+ from langchain.memory import ConversationBufferMemory
9
+ from langchain.chat_models import ChatOpenAI
10
+ from langchain.callbacks.base import BaseCallbackHandler
11
+ import os
12
+ from dotenv import load_dotenv
13
  import numpy as np
14
 
15
+ load_dotenv()
16
+ redis_key = os.getenv('REDIS_KEY')
17
+ HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
18
+ repo_id = 'tiiuae/falcon-7b-instruct'
19
 
20
+ class StreamHandler(BaseCallbackHandler):
21
+ def __init__(self, container, initial_text="", display_method='markdown'):
22
+ self.container = container
23
+ self.text = initial_text
24
+ self.display_method = display_method
25
+
26
+ def on_llm_new_token(self, token: str, **kwargs) -> None:
27
+ self.text += token + " "
28
+ display_function = getattr(self.container, self.display_method, None)
29
+ if display_function is not None:
30
+ display_function(self.text)
31
+ else:
32
+ raise ValueError(f"Invalid display_method: {self.display_method}")
33
+
34
 
35
  st.title('My Amazon shopping buddy 🏷️')
36
  st.caption('πŸ€– Powered by Falcon Open Source AI model')
37
+
38
+ #connect to redis database
39
+ @st.cache_resource()
40
+ def redis_connect():
41
+ redis_conn = redis.Redis(
42
+ host='redis-12882.c259.us-central1-2.gce.cloud.redislabs.com',
43
+ port=12882,
44
+ password=redis_key)
45
+ return redis_conn
46
+
47
+ redis_conn = redis_connect()
48
+
49
+ #the encoding keywords chain
50
+ @st.cache_resource()
51
+ def encode_keywords_chain():
52
+ falcon_llm_1 = HuggingFaceHub(repo_id = repo_id, model_kwargs={'temperature':0.1,'max_new_tokens':500},huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN)
53
+ prompt = PromptTemplate(
54
+ input_variables=["product_description"],
55
+ template="Create comma seperated product keywords to perform a query on a amazon dataset for this user input: {product_description}",
56
+ )
57
+ chain = LLMChain(llm=falcon_llm_1, prompt=prompt)
58
+ return chain
59
+ chain = encode_keywords_chain()
60
+ #the present products chain
61
+
62
+ @st.cache_resource()
63
+ def present_products_chain():
64
+ template = """You are a salesman. Be kind, detailed and nice. take the given context and Present the given queried search result in a nice way as answer to the user_msg. dont ask questions back or freestyle and invent followup conversation!
65
+ {chat_history}
66
+ user:{user_msg}
67
+ Chatbot:"""
68
+ prompt = PromptTemplate(
69
+ input_variables=["chat_history", "user_msg"],
70
+ template=template
71
+ )
72
+ memory = ConversationBufferMemory(memory_key="chat_history")
73
+ llm_chain = LLMChain(
74
+ llm = ChatOpenAI(openai_api_key=os.getenv('OPENAI_API_KEY'),temperature=0.8,model='gpt-3.5-turbo'),
75
+ prompt=prompt,
76
+ verbose=False,
77
+ memory=memory,
78
+ )
79
+ return llm_chain
80
+
81
+
82
+
83
+ llm_chain = present_products_chain()
84
+
85
+ @st.cache_resource()
86
+ def embedding_model():
87
+ embedding_model = SentenceTransformer('sentence-transformers/all-distilroberta-v1')
88
+ return embedding_model
89
+
90
+ embedding_model = embedding_model()
91
 
92
  if "messages" not in st.session_state:
93
  st.session_state["messages"] = [{"role": "assistant", "content": "Hey im your online shopping buddy, how can i help you today?"}]
94
  for msg in st.session_state["messages"]:
95
  st.chat_message(msg["role"]).write(msg["content"])
96
 
97
+ prompt = st.chat_input(key="user_input" )
98
+
99
  if prompt:
100
  st.session_state["messages"].append({"role": "user", "content": prompt})
101
  st.chat_message('user').write(prompt)
102
  st.session_state.disabled = True
103
  keywords = chain.run(prompt)
 
104
  #vectorize the query
105
  query_vector = embedding_model.encode(keywords)
106
  query_vector = np.array(query_vector).astype(np.float32).tobytes()
chatbot.py DELETED
@@ -1,47 +0,0 @@
1
- from langchain.prompts import PromptTemplate
2
- from langchain import HuggingFaceHub
3
- from langchain.chains import LLMChain
4
- from langchain.memory import ConversationBufferMemory
5
- from redis.commands.search.query import Query
6
- import time
7
- import os
8
- from dotenv import load_dotenv
9
- import numpy as np
10
- load_dotenv()
11
- HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
12
- repo_id = 'tiiuae/falcon-7b-instruct'
13
-
14
- falcon_llm_1 = HuggingFaceHub(repo_id = repo_id, model_kwargs={'temperature':0.1,'max_new_tokens':500},huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN)
15
-
16
- prompt = PromptTemplate(
17
- input_variables=["product_description"],
18
- template="Create comma seperated product keywords to perform a query on a amazon dataset for this user input: {product_description}",
19
- )
20
-
21
- chain = LLMChain(llm=falcon_llm_1, prompt=prompt)
22
-
23
- # code The response
24
- repo_id_2 = 'tiiuae/falcon-7b'
25
- template = """You are a salesman. Be kind, detailed and nice. take the given context and Present the given queried search result in a nice way as answer to the user_msg. dont ask questions back or freestyle and invent followup conversation! just
26
-
27
- {chat_history}
28
- {user_msg}
29
- Chatbot:"""
30
-
31
- prompt = PromptTemplate(
32
- input_variables=["chat_history", "user_msg"],
33
- template=template
34
- )
35
- memory = ConversationBufferMemory(memory_key="chat_history")
36
-
37
- llm_chain = LLMChain(
38
- llm = HuggingFaceHub(repo_id = repo_id_2, model_kwargs={'temperature':0.8,'max_new_tokens':500}),
39
- prompt=prompt,
40
- verbose=False,
41
- memory=memory,
42
- )
43
-
44
-
45
-
46
-
47
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
database.py CHANGED
@@ -11,5 +11,3 @@ redis_conn = redis.Redis(
11
  host='redis-12882.c259.us-central1-2.gce.cloud.redislabs.com',
12
  port=12882,
13
  password=redis_key)
14
-
15
- print('connected to redis')
 
11
  host='redis-12882.c259.us-central1-2.gce.cloud.redislabs.com',
12
  port=12882,
13
  password=redis_key)
 
 
preprocess.py CHANGED
@@ -8,6 +8,7 @@ from database import redis_conn
8
  from utilities import create_flat_index, load_vectors
9
 
10
 
 
11
  #set maximum length for text fields
12
  MAX_TEXT_LENGTH = 512
13
 
 
8
  from utilities import create_flat_index, load_vectors
9
 
10
 
11
+
12
  #set maximum length for text fields
13
  MAX_TEXT_LENGTH = 512
14
 
readme.md CHANGED
@@ -1,3 +1,13 @@
 
 
 
 
 
 
 
 
 
 
1
  **Description:**
2
  An ***e-commerce chatBot*** which goes through the Amazon dataset products and suggests the most suitable goods according to the user needs.
3
  By utilizing the power of product embeddings and large language models exploiting Langchain and Redis technologies alongside the open source sentence-transformer embedding model and Falcon LLM, this chatbot acts as a real salesperson, can understand the client's request and efficiently search for relevant product recommendations based on the user description and present them in an engaging and informative manner.
 
1
+ ---
2
+ title: EcomShoppingBuddy
3
+ emoji: 🌍
4
+ colorFrom: pink
5
+ colorTo: red
6
+ sdk: streamlit
7
+ sdk_version: 1.27.2
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
  **Description:**
12
  An ***e-commerce chatBot*** which goes through the Amazon dataset products and suggests the most suitable goods according to the user needs.
13
  By utilizing the power of product embeddings and large language models exploiting Langchain and Redis technologies alongside the open source sentence-transformer embedding model and Falcon LLM, this chatbot acts as a real salesperson, can understand the client's request and efficiently search for relevant product recommendations based on the user description and present them in an engaging and informative manner.
requirements.txt CHANGED
@@ -2,6 +2,7 @@ langchain == 0.0.242
2
  openai == 0.27.8
3
  redis == 5.0.1
4
  pandas == 2.0.3
 
5
  sentence-transformers == 2.2.2
6
  tiktoken == 0.5.1
7
  streamlit == 1.27.2
 
2
  openai == 0.27.8
3
  redis == 5.0.1
4
  pandas == 2.0.3
5
+ numpy == 1.24.3
6
  sentence-transformers == 2.2.2
7
  tiktoken == 0.5.1
8
  streamlit == 1.27.2