Spaces:
Runtime error
Runtime error
fix code 4
Browse files- app.py +45 -41
- constants.py +6 -5
app.py
CHANGED
|
@@ -30,49 +30,53 @@ ITEM_KEYWORD_EMBEDDING = "item_vector"
|
|
| 30 |
TOPK = 5
|
| 31 |
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# the present products chain
|
| 56 |
-
@st.cache_resource()
|
| 57 |
-
def present_products_chain():
|
| 58 |
-
template = TEMPLATE_2
|
| 59 |
-
prompt = PromptTemplate(input_variables=["chat_history", "user_msg"], template=template)
|
| 60 |
-
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 61 |
-
llm_chain = LLMChain(
|
| 62 |
-
llm=ChatOpenAI(
|
| 63 |
-
openai_api_key=os.getenv("OPENAI_API_KEY"), temperature=OPENAI_TEMPERATURE, model=OPENAI_MODEL_NAME
|
| 64 |
-
),
|
| 65 |
-
prompt=prompt,
|
| 66 |
-
verbose=False,
|
| 67 |
-
memory=memory,
|
| 68 |
-
)
|
| 69 |
-
return llm_chain
|
| 70 |
|
| 71 |
-
@st.cache_resource()
|
| 72 |
-
def instance_embedding_model():
|
| 73 |
-
embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
|
| 74 |
-
return embedding_model
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
st.title("My Amazon shopping buddy π·οΈ")
|
| 77 |
st.caption("π€ Powered by Falcon Open Source AI model")
|
| 78 |
redis_conn = connect_to_redis()
|
|
|
|
| 30 |
TOPK = 5
|
| 31 |
|
| 32 |
|
| 33 |
+
# connect to redis database
|
| 34 |
+
@st.cache_resource()
|
| 35 |
+
def connect_to_redis():
|
| 36 |
+
pool = create_redis()
|
| 37 |
+
return redis.Redis(connection_pool=pool)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# the encoding keywords chain
|
| 41 |
+
@st.cache_resource()
|
| 42 |
+
def encode_keywords_chain():
|
| 43 |
+
llm = HuggingFaceHub(
|
| 44 |
+
repo_id=FALCON_REPO_ID,
|
| 45 |
+
model_kwargs={"temperature": FALCON_TEMPERATURE, "max_new_tokens": FALCON_MAX_TOKENS},
|
| 46 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 47 |
+
)
|
| 48 |
+
prompt = PromptTemplate(
|
| 49 |
+
input_variables=["product_description"],
|
| 50 |
+
template=TEMPLATE_1,
|
| 51 |
+
)
|
| 52 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
| 53 |
+
return chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# the present products chain
|
| 57 |
+
@st.cache_resource()
|
| 58 |
+
def present_products_chain():
|
| 59 |
+
template = TEMPLATE_2
|
| 60 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 61 |
+
prompt = PromptTemplate(input_variables=["chat_history", "user_msg"], template=template)
|
| 62 |
+
chain = LLMChain(
|
| 63 |
+
llm=ChatOpenAI(
|
| 64 |
+
openai_api_key=os.getenv("OPENAI_API_KEY"), temperature=OPENAI_TEMPERATURE, model=OPENAI_MODEL_NAME
|
| 65 |
+
),
|
| 66 |
+
prompt=prompt,
|
| 67 |
+
verbose=False,
|
| 68 |
+
memory=memory,
|
| 69 |
+
)
|
| 70 |
+
return chain
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
@st.cache_resource()
|
| 74 |
+
def instance_embedding_model():
|
| 75 |
+
embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
|
| 76 |
+
return embedding_model
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def main():
|
| 80 |
st.title("My Amazon shopping buddy π·οΈ")
|
| 81 |
st.caption("π€ Powered by Falcon Open Source AI model")
|
| 82 |
redis_conn = connect_to_redis()
|
constants.py
CHANGED
|
@@ -7,8 +7,9 @@ OPENAI_TEMPERATURE = 0.8
|
|
| 7 |
|
| 8 |
EMBEDDING_MODEL_NAME = "sentence-transformers/all-distilroberta-v1"
|
| 9 |
|
| 10 |
-
TEMPLATE_1 = "Create comma
|
| 11 |
-
TEMPLATE_2 = """You are a salesman.Present the given product results in a nice way as answer to the user_msg.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
| 7 |
|
| 8 |
EMBEDDING_MODEL_NAME = "sentence-transformers/all-distilroberta-v1"
|
| 9 |
|
| 10 |
+
TEMPLATE_1 = "Create comma separated product keywords to perform a query on amazon dataset for this user input: {product_description}"
|
| 11 |
+
TEMPLATE_2 = """You are a salesman.Present the given product results in a nice way as answer to the user_msg. Don't ask questions back,
|
| 12 |
+
if results are empty just say that we don't have such products,
|
| 13 |
+
{chat_history}
|
| 14 |
+
user:{user_msg}
|
| 15 |
+
Chatbot:"""
|