tensorgirl commited on
Commit
1e940e5
1 Parent(s): 4b1e362

Upload 15 files

Browse files
Files changed (15) hide show
  1. Data/0.txt +6 -0
  2. Data/1.txt +6 -0
  3. Data/10.txt +6 -0
  4. Data/11.txt +6 -0
  5. Data/2.txt +6 -0
  6. Data/3.txt +6 -0
  7. Data/4.txt +6 -0
  8. Data/5.txt +6 -0
  9. Data/6.txt +6 -0
  10. Data/7.txt +6 -0
  11. Data/8.txt +6 -0
  12. Data/9.txt +6 -0
  13. app.py +112 -0
  14. main.py +17 -0
  15. requirements.txt +6 -0
Data/0.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 001
2
+ item: Paneer Grilled Roll
3
+ cost: 220
4
+ vegan: N
5
+ veg: Y
6
+ about: Marinated Paneer cubes with spices and yogurt, grilled with onion and pepper and rolled into breadding and shallow fried.
Data/1.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 002
2
+ item: Mushroom Pepper Skewers
3
+ cost: 240
4
+ vegan: Y
5
+ veg: Y
6
+ about: Threaded mushrooms and peppers alternativel on skewers, brushed with flavourful oils, lemon juice, garlic, thyme, rosemary, salt, pepper and grilled.
Data/10.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 011
2
+ item: Chicken A La King
3
+ cost: 320
4
+ vegan: N
5
+ veg: N
6
+ about: Oyster sauce stewed chicken in a rich creamy sauce served along with herbed basil rice.
Data/11.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 012
2
+ item: Mexican Stuffed Bhetki Fish
3
+ cost: 400
4
+ vegan: N
5
+ veg: N
6
+ about: Bhetki fish fillet stuffed with cheese and veggies and roasted in green tomato salad served with Mexican rice.
Data/2.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 003
2
+ item: Crispy corn
3
+ cost: 150
4
+ vegan: Y
5
+ veg: Y
6
+ about: Batter coated sweet corn, deep fried and srinkled with spices.
Data/3.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 004
2
+ item: Chicken Cheese Balls
3
+ cost: 200
4
+ vegan: N
5
+ veg: N
6
+ about: Freshly ground chicken, cheese adspices made into lemon sized balls and golden fried.
Data/4.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 005
2
+ item: Fish Finger
3
+ cost: 180
4
+ vegan: N
5
+ veg: N
6
+ about: Bhetki fish fillets are cut in shape of fingers, marinated and deep fried.
Data/5.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 006
2
+ item: Fish Finger
3
+ cost: 180
4
+ vegan: N
5
+ veg: N
6
+ about:
Data/6.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 007
2
+ item: Jade Chicken
3
+ cost: 280
4
+ vegan: N
5
+ veg: N
6
+ about: Boneless chicken that are battered and deep fried, and dressed with translucent green, semi-thick sauce of mint and coriander.
Data/7.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 008
2
+ item: Veggie Steak
3
+ cost: 230
4
+ vegan: Y
5
+ veg: Y
6
+ about: A platter of sauted vegetables, topped with spicy tomato sauce, served with steamed rice.
Data/8.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 009
2
+ item: Tofu Cutlet
3
+ cost: 190
4
+ vegan: Y
5
+ veg: Y
6
+ about: Tofu with green peas mashed with boiled potatoes and onions, covered in bread crumbs and crisp fried
Data/9.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ item_id: 010
2
+ item: White Sauce Veggie Extravaganza
3
+ cost: 220
4
+ vegan: N
5
+ veg: Y
6
+ about: Mushroom, baby corn, beans, cooked in milky white sauce and cheese
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import TextLoader
2
+ import os
3
+
4
+ loaders = []
5
+ folder_path = "Data"
6
+
7
+ for i in range(12):
8
+ file_path = os.path.join(folder_path,"{}.txt".format(i))
9
+ loaders.append(TextLoader(file_path))
10
+
11
+ docs = []
12
+ for loader in loaders:
13
+ docs.extend(loader.load())
14
+
15
+ from langchain.vectorstores import Chroma
16
+ from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
17
+
18
+ HF_TOKEN = os.getenv("HF_TOKEN")
19
+ embeddings = HuggingFaceInferenceAPIEmbeddings(
20
+ api_key=HF_TOKEN, model_name="sentence-transformers/all-mpnet-base-v2"
21
+ )
22
+
23
+ vectordb = Chroma.from_documents(
24
+ documents=docs,
25
+ embedding=embeddings
26
+ )
27
+
28
+ from langchain_community.llms import HuggingFaceHub
29
+
30
+ llm = HuggingFaceHub(
31
+ repo_id="google/gemma-2b-it",
32
+ task="text-generation",
33
+ model_kwargs={
34
+ "max_new_tokens": 512,
35
+ "top_k": 5,
36
+ "temperature": 0.1,
37
+ "repetition_penalty": 1.03,
38
+ },
39
+ huggingfacehub_api_token = HF_TOKEN
40
+ )
41
+
42
+ from langchain.prompts import PromptTemplate
43
+
44
+ template = """You are a Chatbot at a Restaurant. Help the customer pick the right dish to order. The items in the context are dishes. The field below the item is the cost of the dish. About is the description of the dish. Use the context below to answe the questions
45
+ {context}
46
+ Question: {question}
47
+ Helpful Answer:"""
48
+ QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question"],template=template,)
49
+
50
+ from langchain.memory import ConversationBufferMemory
51
+ memory = ConversationBufferMemory(
52
+ memory_key="chat_history",
53
+ return_messages=True
54
+ )
55
+
56
+ from langchain.chains import ConversationalRetrievalChain
57
+
58
+ retriever = vectordb.as_retriever()
59
+ qa = ConversationalRetrievalChain.from_llm(
60
+ llm,
61
+ retriever=retriever,
62
+ memory=memory,
63
+ )
64
+
65
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
66
+ from langchain_core.output_parsers import StrOutputParser
67
+ from langchain_core.runnables import RunnablePassthrough
68
+
69
+ contextualize_q_system_prompt = """Given a chat history and the latest user question \
70
+ which might reference context in the chat history, formulate a standalone question \
71
+ which can be understood without the chat history. Do NOT answer the question, \
72
+ just reformulate it if needed and otherwise return it as is."""
73
+ contextualize_q_prompt = ChatPromptTemplate.from_messages(
74
+ [
75
+ ("system", contextualize_q_system_prompt),
76
+ MessagesPlaceholder(variable_name="chat_history"),
77
+ ("human", "{question}"),
78
+ ]
79
+ )
80
+ contextualize_q_chain = contextualize_q_prompt | llm | StrOutputParser()
81
+
82
+ def contextualized_question(input: dict):
83
+ if input.get("chat_history"):
84
+ return contextualize_q_chain
85
+ else:
86
+ return input["question"]
87
+
88
+
89
+ rag_chain = (
90
+ RunnablePassthrough.assign(
91
+ context=contextualized_question | retriever
92
+ )
93
+ | QA_CHAIN_PROMPT
94
+ | llm
95
+ )
96
+
97
+
98
+ from langchain_core.messages import AIMessage, HumanMessage
99
+
100
+ os.environ["LANGCHAIN_WANDB_TRACING"] = "true"
101
+ os.environ["WANDB_PROJECT"] = "Restaurant_ChatBot"
102
+
103
+ print("Welcome to the Restaurant. How can I help you today?")
104
+ chat_history = []
105
+
106
+ def predict(message):
107
+ ai_msg = rag_chain.invoke({"question": message, "chat_history": chat_history})
108
+ idx = ai_msg.find("Answer")
109
+ chat_history.extend([HumanMessage(content=message), ai_msg])
110
+
111
+ return ai_msg[idx:]
112
+
main.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from app import predict
3
+ import os
4
+ from huggingface_hub import login
5
+
6
+ os.environ['HF_HOME'] = '/hug/cache/'
7
+ os.environ['TRANSFORMERS_CACHE'] = '/blabla/cache/'
8
+
9
+ app = FastAPI()
10
+
11
+ @app.get("/")
12
+ async def root():
13
+ return {"Restaurant ChatBot":"Version 1.0 'First Draft'"}
14
+
15
+ @app.post("/Predict/")
16
+ def read_user(input_json: str):
17
+ return predict(input_json)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain-community
2
+ langchain
3
+ chromadb
4
+ wandb
5
+ requests
6
+ fastapi