Amitgm commited on
Commit
4c458bf
·
verified ·
1 Parent(s): e3de726

Delete main.py

Browse files
Files changed (1) hide show
  1. main.py +0 -375
main.py DELETED
@@ -1,375 +0,0 @@
1
- from langchain_community.document_loaders import CSVLoader
2
- from langchain.text_splitter import RecursiveCharacterTextSplitter
3
- # from langchain_community.embeddings import OpenAIEmbeddings
4
- from langchain_community.vectorstores import chroma
5
- from langchain_community.llms import openai
6
- from langchain.chains import LLMChain
7
- from dotenv import load_dotenv
8
- from langchain.chains import ConversationalRetrievalChain
9
- from langchain_core.prompts import ChatPromptTemplate,PromptTemplate
10
- from langchain.memory import ConversationBufferMemory
11
- from langchain_community.chat_models import ChatOpenAI
12
- from langchain_openai import OpenAIEmbeddings
13
- from langchain_chroma import Chroma
14
- import os
15
- from dotenv import load_dotenv
16
- import streamlit as st
17
- import streamlit_chat
18
- from langchain_groq import ChatGroq
19
- global seed
20
- from langchain.chains import LLMChain
21
- from langchain.prompts import PromptTemplate
22
- from langchain.memory import ConversationBufferMemory
23
- from langchain_community.chat_models import ChatOpenAI
24
- from langchain.docstore.document import Document
25
- from langchain.llms import HuggingFacePipeline
26
- from langchain.embeddings import HuggingFaceEmbeddings
27
-
28
- import pandas as pd
29
-
30
- load_dotenv()
31
-
32
-
33
- # OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
34
- # os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
35
-
36
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
37
-
38
-
39
- class prompts:
40
-
41
- prompt = PromptTemplate.from_template("""
42
-
43
- You are a helpful fitness assistant. Use the following context to answer the question The Level is provided for you to get a better idea on how to answer the question
44
- .
45
- If you don't know the answer, just say that you don't know, don't try to make up an answer.Also make sure to mention the level passed for the user.
46
- Context:
47
- {context}
48
-
49
- Chat History:
50
- {history}
51
-
52
- Question:
53
- {question}
54
-
55
- Level:
56
- {level}
57
-
58
- Answer:
59
- """)
60
-
61
- # Data Filteration
62
- def filter_transform_data(dataframe):
63
-
64
- dataframe.drop("RatingDesc",axis=1,inplace=True)
65
-
66
- dataframe.dropna(subset=["Desc","Equipment"],inplace=True)
67
-
68
- dataframe.drop("Rating",inplace=True,axis=1)
69
-
70
- # transform data
71
-
72
- document_data = dataframe.to_dict(orient="records")
73
-
74
- return document_data
75
-
76
-
77
- def get_context(vector_store,query,level):
78
-
79
- results = vector_store.max_marginal_relevance_search(
80
-
81
- query=query,
82
- k=5,
83
- filter={"Level": level},
84
- )
85
-
86
- # Creating the LLM Chain
87
-
88
- # Pass your context manually from retrieved documents
89
- context = "\n\n".join([doc.page_content for doc in results])
90
-
91
- return context
92
-
93
- def generate_vector_store():
94
-
95
- # embedding = OpenAIEmbeddings(
96
-
97
- if "vector_store" not in st.session_state:
98
-
99
- langchain_documents = []
100
-
101
- dataframe = pd.read_csv("megaGymDataset.csv",index_col=0)
102
-
103
- document_data = filter_transform_data(dataframe)
104
-
105
- # Iterate through the sample data and create Document objects
106
- for item in document_data:
107
- # Formulate the page_content string
108
- page_content = (
109
- f"Title: {item['Title']}\n"
110
- f"Type:{item['Type']}\n"
111
- f"BodyPart: {item['BodyPart']}\n"
112
- f"Desc: {item['Desc']}\n"
113
- f"Equipment: {item['Equipment']}\n"
114
- )
115
-
116
- # Create the metadata dictionary
117
- metadata = {"Level": item['Level']}
118
-
119
- # Create the Document object
120
- doc = Document(page_content=page_content, metadata=metadata)
121
-
122
- # Add the Document to our list
123
- langchain_documents.append(doc)
124
-
125
- # creating the session_state for vector_store
126
-
127
- # embedding = OpenAIEmbeddings(openai_api_key=os.environ["OPENAI_API_KEY"])
128
- embedding = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-large-instruct")
129
-
130
-
131
- # if path not exist
132
- if not os.path.exists("db"):
133
-
134
- st.session_state.vector_store = Chroma.from_documents(langchain_documents,embedding=embedding,collection_name="gym-queries-data",persist_directory = "db")
135
- # st.session_state.vector_store.persist()
136
-
137
- else:
138
-
139
- st.session_state.vector_store = Chroma(
140
-
141
- persist_directory="db",
142
- embedding_function=embedding
143
- )
144
-
145
- return st.session_state.vector_store
146
-
147
- def get_conversational_chain(vector_store,query,level):
148
-
149
- # model_name = "msu-rcc-lair/RuadaptQwen2.5-32B-Instruct" # Replace with actual name
150
-
151
- # tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
152
- # model = AutoModelForCausalLM.from_pretrained(
153
- # model_name,
154
- # device_map="auto",
155
- # torch_dtype=torch.bfloat16, # or float16
156
- # trust_remote_code=True
157
- # )
158
- # generator = pipeline(
159
- # "text-generation",
160
- # model=model,
161
- # tokenizer=tokenizer,
162
- # max_new_tokens=512,
163
- # temperature=0.7,
164
- # return_full_text=False
165
- # )
166
-
167
-
168
-
169
- # llm = HuggingFacePipeline(pipeline=generator)
170
- # llm = ChatOpenAI(temperature=0.5,model_name="gpt-4o")
171
-
172
- # llama3-70b-8192
173
- llm = ChatGroq(
174
- temperature=1,
175
- groq_api_key = GROQ_API_KEY,
176
- model_name="llama-3.1-8b-instant",
177
- max_tokens=560,
178
- # top_p=0.95,
179
- # frequency_penalty=1,
180
- # presence_penalty=1,
181
- )
182
- # llm_chain = LLMChain(llm=llm, prompt=prompts.prompt)
183
-
184
- if "memory" not in st.session_state:
185
-
186
- st.session_state.memory = ConversationBufferMemory(memory_key="history", input_key="question", return_messages=True)
187
-
188
- st.session_state.conversational_chain = LLMChain(
189
- llm=llm,
190
- # taking the prompt template
191
- prompt=prompts.prompt,
192
- memory=st.session_state.memory
193
- )
194
-
195
-
196
- return st.session_state.conversational_chain,st.session_state.memory
197
-
198
- def stick_it_good():
199
-
200
- # make header sticky.
201
- st.markdown(
202
- """
203
- <div class='fixed-header'/>
204
- <style>
205
- div[data-testid="stVerticalBlock"] div:has(div.fixed-header) {
206
- position: sticky;
207
- top: 2.875rem;
208
- background-color: ##393939;
209
- z-index: 999;
210
- }
211
- .fixed-header {
212
- border-bottom: 1px solid black;
213
- }
214
- </style>
215
- """,
216
- unsafe_allow_html=True
217
- )
218
-
219
-
220
- def show_privacy_policy():
221
- st.title("Privacy Policy")
222
-
223
-
224
- def show_terms_of_service():
225
- st.title("Terms of Service")
226
-
227
- seed = 0
228
-
229
- def main():
230
-
231
- global seed
232
-
233
- page = st.sidebar.selectbox("Choose a page", ["Home", "Privacy Policy", "Terms of Service"])
234
-
235
- if page == "Privacy Policy":
236
-
237
- show_privacy_policy()
238
-
239
- elif page == "Terms of Service":
240
-
241
- show_terms_of_service()
242
-
243
- else:
244
-
245
- st.write("Welcome to the Home Page")
246
-
247
- with st.container():
248
-
249
- st.title("Workout Wizard")
250
- stick_it_good()
251
-
252
-
253
- with st.sidebar:
254
-
255
- if "seed" not in st.session_state:
256
-
257
- st.session_state.seed = 0
258
-
259
- # Display the image using the URL
260
-
261
- choose_mode = st.selectbox('Choose Workout Level',["Beginner","Intermediate","Expert"])
262
-
263
-
264
- st.markdown("<h2 style='text-align: center;'>Choose Your Avatar</h2>", unsafe_allow_html=True)
265
-
266
- # st.markdown(f"<h2 style='text-align: center;'>{st.button("Back")}</h2>", unsafe_allow_html=True)
267
-
268
- # Center the buttons using HTML and CSS
269
- col1, col2, col3 = st.columns([1, 1, 1])
270
-
271
-
272
- with col1:
273
-
274
- st.write("") # Empty column for spacing
275
-
276
- with col2:
277
-
278
- print(st.session_state.seed)
279
-
280
- choose_Avatar = st.button("Next")
281
-
282
- choose_Avatar_second = st.button("Back")
283
-
284
-
285
- if choose_Avatar:
286
-
287
- st.session_state.seed += 1
288
-
289
- if choose_Avatar_second:
290
-
291
- st.session_state.seed -= 1
292
-
293
- avatar_url = f"https://api.dicebear.com/9.x/adventurer/svg?seed={st.session_state.seed}"
294
-
295
- st.image(avatar_url, caption=f"Avatar {st.session_state.seed }")
296
-
297
- with col3:
298
-
299
- st.write("") # Empty column for spacing
300
-
301
-
302
- streamlit_chat.message("Hi. I'm your friendly Gym Assistant Bot.")
303
- streamlit_chat.message("Ask me anything about the gym! Just don’t ask me to do any push-ups... I'm already *up* and running!")
304
- streamlit_chat.message("If you want to change your workout level and avatar, press the top left arrow and you will have options to make changes")
305
-
306
-
307
- question = st.chat_input("Ask a question related to your GYM queries")
308
-
309
-
310
- if "conversation_chain" not in st.session_state:
311
-
312
- st.session_state.conversation_chain = None
313
-
314
-
315
- # if question:
316
-
317
- # Converstion chain
318
- if st.session_state.conversation_chain == None:
319
- # st.session_state.vectors
320
-
321
- print("the vector store generated")
322
-
323
- st.session_state.vector_store = generate_vector_store()
324
-
325
- st.session_state.conversation_chain, st.session_state.memory = get_conversational_chain(st.session_state.vector_store,question,choose_mode)
326
-
327
- # the session state memory
328
- if st.session_state.memory != None:
329
-
330
- for i,message in enumerate(st.session_state.memory.chat_memory.messages):
331
-
332
- if i%2 == 0:
333
-
334
- suffix = f" for {choose_mode} level"
335
-
336
- # Check if the message ends with the suffix and strip it
337
- if message.content.endswith(suffix):
338
-
339
- message.content = message.content[:-len(suffix)]
340
-
341
- # message.content = message.content.strip(f" for {choose_mode} level")
342
-
343
- print("this is the message content",message.content)
344
-
345
- streamlit_chat.message(message.content,is_user=True, avatar_style="adventurer",seed=st.session_state.seed, key=f"user_msg_{i}")
346
-
347
- else:
348
-
349
- streamlit_chat.message(message.content,key=f"bot_msg_{i}")
350
-
351
- st.write("--------------------------------------------------")
352
-
353
- if question:
354
-
355
- streamlit_chat.message(question,is_user=True, avatar_style="adventurer",seed=st.session_state.seed)
356
-
357
- print(question)
358
-
359
- print("------------------------")
360
-
361
- # GETTING THE CONTEXT AND ANSWER FROM THE MODEL
362
-
363
- context = get_context(st.session_state.vector_store,question,choose_mode)
364
-
365
- print("context::",context)
366
- print("the choose mode:",choose_mode)
367
-
368
- response = st.session_state.conversational_chain.run({"context": context, "question": question,"level":choose_mode})
369
-
370
- streamlit_chat.message(response)
371
-
372
-
373
- if __name__ == "__main__":
374
-
375
- main()