Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import langchain
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain.document_loaders import TextLoader
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
| 7 |
+
from langchain.schema import Document
|
| 8 |
+
import streamlit as st
|
| 9 |
+
from langchain_groq import ChatGroq
|
| 10 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 11 |
+
from langchain.prompts import PromptTemplate
|
| 12 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
+
from langchain.schema.runnable import RunnablePassthrough
|
| 14 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=75, chunk_overlap=0, separators=["\n",'. '])
|
| 15 |
+
loader = TextLoader('data.txt')
|
| 16 |
+
docs = loader.load()
|
| 17 |
+
split_docs = text_splitter.split_documents(docs)
|
| 18 |
+
# Iterate over the Document objects and extract the content and metadata using the appropriate attributes
|
| 19 |
+
split_docss = [Document(page_content=t.page_content, metadata=t.metadata) for t in split_docs]
|
| 20 |
+
embeddings = HuggingFaceBgeEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 21 |
+
vectorstore = FAISS.from_documents(split_docss,embeddings)
|
| 22 |
+
retriever=vectorstore.as_retriever()
|
| 23 |
+
retriever.search_kwargs['k'] = 5
|
| 24 |
+
os.environ['GROQ_API_KEY']='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'
|
| 25 |
+
def preprocess(text):
|
| 26 |
+
return "\n".join(x.page_content for x in text)
|
| 27 |
+
def format_docs(docs):
|
| 28 |
+
return "\n".join(doc.page_content for doc in docs)
|
| 29 |
+
def printer(text):
|
| 30 |
+
print(text)
|
| 31 |
+
return text
|
| 32 |
+
|
| 33 |
+
llm = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768", streaming=True, verbose=False)
|
| 34 |
+
# llama3-8b-8192
|
| 35 |
+
prompt='''You are a coupon recommender chatbot.
|
| 36 |
+
Understand the question with the category of the item and the location:
|
| 37 |
+
{summary}
|
| 38 |
+
and answer with only relevant coupons:
|
| 39 |
+
{context}
|
| 40 |
+
|
| 41 |
+
Instructions:
|
| 42 |
+
List only the coupons that match the category of the item along with the location in the question.Use three sentences maximum and keep the answer concise.
|
| 43 |
+
'''
|
| 44 |
+
template=PromptTemplate(template=prompt,input_variables=['summary','context'])
|
| 45 |
+
rag_chain=({ "summary": RunnablePassthrough(), "context": retriever | format_docs ,
|
| 46 |
+
} | template|printer| llm| StrOutputParser())
|
| 47 |
+
|
| 48 |
+
st.set_page_config(page_title="Lloyds Mobile Rewards Section", layout="wide")
|
| 49 |
+
|
| 50 |
+
st.title("Mock Rewards Section")
|
| 51 |
+
st.header("Welcome to Your Rewards")
|
| 52 |
+
st.write("Find exclusive offers and discounts just for you!")
|
| 53 |
+
|
| 54 |
+
st.sidebar.title("Check your eligibility to turn on the discover mode!")
|
| 55 |
+
# category = st.sidebar.selectbox("Select Category", ["All", "Electronics", "Food", "Groceries"])
|
| 56 |
+
if 'count' not in st.session_state:
|
| 57 |
+
st.session_state.count = 0
|
| 58 |
+
|
| 59 |
+
enable_controls = st.session_state.count == 5
|
| 60 |
+
|
| 61 |
+
def increment_count():
|
| 62 |
+
st.session_state.count += 1
|
| 63 |
+
|
| 64 |
+
st.sidebar.title(f'Number of transactions : {st.session_state.count}')
|
| 65 |
+
# st.write(f"Button has been pressed {st.session_state.count} times")
|
| 66 |
+
button=st.sidebar.button("Mock Transaction")
|
| 67 |
+
if button:
|
| 68 |
+
increment_count()
|
| 69 |
+
response=None
|
| 70 |
+
# Display the updated count
|
| 71 |
+
if enable_controls:
|
| 72 |
+
st.sidebar.write("You are now eligible for discover mode ")
|
| 73 |
+
user_input = st.sidebar.text_input("Enter your request or choice of category", key='user_input')
|
| 74 |
+
response=retriever.get_relevant_documents(query=st.session_state.user_input)
|
| 75 |
+
|
| 76 |
+
st.write(f'{response}')
|
| 77 |
+
if not enable_controls:
|
| 78 |
+
st.sidebar.write(f"Keep going {20-st.session_state.count} times.")
|