swayam-the-coder commited on
Commit
8379ca7
Β·
verified Β·
1 Parent(s): 290edaa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +446 -0
app.py ADDED
@@ -0,0 +1,446 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_option_menu import option_menu
3
+ from langchain_community.document_loaders import PyPDFLoader, WebBaseLoader
4
+ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
5
+ from langchain_chroma import Chroma
6
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
7
+ from langchain.chains import create_retrieval_chain
8
+ from langchain.chains.combine_documents import create_stuff_documents_chain
9
+ from langchain_core.prompts import ChatPromptTemplate
10
+ import os
11
+ import bs4
12
+ import speech_recognition as sr
13
+ from sqlalchemy import create_engine
14
+ import pandas as pd
15
+ import requests
16
+
17
+ # Set page config
18
+ st.set_page_config(page_title='πŸ€– GRASP', layout='wide', initial_sidebar_state='expanded')
19
+
20
+ # Custom CSS for styling
21
+ st.markdown("""
22
+ <style>
23
+ body {
24
+ font-family: 'Roboto', sans-serif;
25
+ background-color: #E8F6F3;
26
+ }
27
+ .stButton>button {
28
+ background-color: #4CAF50;
29
+ color: white;
30
+ border: none;
31
+ padding: 10px 20px;
32
+ font-size: 16px;
33
+ border-radius: 5px;
34
+ }
35
+ .stButton>button:hover {
36
+ background-color: #45A049;
37
+ }
38
+ .stTextInput>div>div>input {
39
+ border-radius: 5px;
40
+ border: 1px solid #ccc;
41
+ padding: 10px;
42
+ }
43
+ .stSidebar>div>div>div>div {
44
+ background-color: #E8F6F3;
45
+ }
46
+ .stFileUploader>label>div>div>button {
47
+ background-color: #4CAF50;
48
+ color: white;
49
+ border: none;
50
+ padding: 10px 20px;
51
+ font-size: 16px;
52
+ border-radius: 5px;
53
+ }
54
+ .stFileUploader>label>div>div>button:hover {
55
+ background-color: #45A049;
56
+ }
57
+ </style>
58
+ """, unsafe_allow_html=True)
59
+
60
+ # Get API key from the user
61
+ st.sidebar.header("API Key")
62
+ api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
63
+ os.environ['OPENAI_API_KEY'] = api_key
64
+
65
+ def home_page():
66
+ st.markdown("<h1 style='text-align: center;'> πŸ€– Generative Retrieval Augmented Search Platform</h1>",unsafe_allow_html=True)
67
+ st.header("Welcome to GRASP")
68
+ st.subheader("Explore and learn about each RAG method on their respective pages.")
69
+
70
+ def pdf_rag_page():
71
+ st.title('PDF RAG πŸ“„')
72
+ st.sidebar.header("Upload your PDF")
73
+ uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=["pdf"])
74
+
75
+ st.write("### Instructions πŸ“œ")
76
+ st.write("""
77
+ 1. Upload a PDF file using the sidebar.
78
+ 2. Enter your query in the text input below.
79
+ 3. Enter your OpenAI API Key in the sidebar.
80
+ 4. Click the 'Get Results' button to process the PDF and provide relevant answers based on the content.
81
+ """)
82
+
83
+ def load_and_process_pdf(file):
84
+ with open(file.name, "wb") as f:
85
+ f.write(file.getbuffer())
86
+ loader = PyPDFLoader(file.name)
87
+ docs = loader.load()
88
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
89
+ splits = text_splitter.split_documents(docs)
90
+ vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
91
+ retriever = vectorstore.as_retriever()
92
+ system_prompt = (
93
+ "You are an assistant for question-answering tasks. "
94
+ "Use the following pieces of retrieved context to answer "
95
+ "the question. If you don't know the answer, say that you "
96
+ "don't know. Use three sentences maximum and keep the "
97
+ "answer concise."
98
+ "\n\n"
99
+ "{context}"
100
+ )
101
+ prompt = ChatPromptTemplate.from_messages([
102
+ ("system", system_prompt),
103
+ ("human", "{input}"),
104
+ ])
105
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key=api_key)
106
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
107
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
108
+ return rag_chain
109
+
110
+ rag_chain = None
111
+ if uploaded_file:
112
+ rag_chain = load_and_process_pdf(uploaded_file)
113
+
114
+ input_text = st.text_input("Please feel free to ask any doubts! πŸ“")
115
+
116
+ if st.button("Get Results"):
117
+ if input_text and rag_chain:
118
+ with st.spinner('Processing...'):
119
+ try:
120
+ response = rag_chain.invoke({"input": input_text})
121
+ st.write(response["answer"])
122
+ except Exception as e:
123
+ st.error(f"An error occurred: {e}")
124
+ elif input_text:
125
+ st.warning("Please upload a PDF file to ask questions.")
126
+
127
+ def web_rag_page():
128
+ st.title('Web RAG 🌐')
129
+ st.sidebar.header("Enter a web URL")
130
+ url = st.sidebar.text_input("URL")
131
+
132
+ st.write("### Instructions πŸ“œ")
133
+ st.write("""
134
+ 1. Enter a web URL using the sidebar.
135
+ 2. Enter your query in the text input below.
136
+ 3. Enter your OpenAI API Key in the sidebar.
137
+ 4. Click the 'Get Results' button to process the webpage content and provide relevant answers based on the content.
138
+ """)
139
+
140
+ def load_and_process_web(url):
141
+ loader = WebBaseLoader(web_paths=(url,), bs_kwargs=dict(parse_only=bs4.SoupStrainer(class_=("mw-body-content", "mw-headline"))))
142
+ documents = loader.load()
143
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True)
144
+ all_splits = text_splitter.split_documents(documents)
145
+ vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
146
+ retriever = vectorstore.as_retriever()
147
+ system_prompt = (
148
+ "You are an assistant for question-answering tasks. "
149
+ "Use the following pieces of retrieved context to answer "
150
+ "the question. If you don't know the answer, say that you "
151
+ "don't know. Use three sentences maximum and keep the "
152
+ "answer concise."
153
+ "\n\n"
154
+ "{context}"
155
+ )
156
+ prompt = ChatPromptTemplate.from_messages([
157
+ ("system", system_prompt),
158
+ ("human", "{input}"),
159
+ ])
160
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key=api_key)
161
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
162
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
163
+ return rag_chain
164
+
165
+ rag_chain = None
166
+ if url:
167
+ rag_chain = load_and_process_web(url)
168
+
169
+ input_text = st.text_input("Please feel free to ask any doubts! πŸ“")
170
+
171
+ if st.button("Get Results"):
172
+ if input_text and rag_chain:
173
+ with st.spinner('Processing...'):
174
+ try:
175
+ response = rag_chain.invoke({"input": input_text})
176
+ st.write(response["answer"])
177
+ except Exception as e:
178
+ st.error(f"An error occurred: {e}")
179
+ elif input_text:
180
+ st.warning("Please enter a URL to ask questions.")
181
+
182
+ def text_document_rag_page():
183
+ st.title('Text Document RAG πŸ“„')
184
+ st.sidebar.header("Upload your Text Document")
185
+ uploaded_file = st.sidebar.file_uploader("Choose a text file", type=["txt"])
186
+
187
+ st.write("### Instructions πŸ“œ")
188
+ st.write("""
189
+ 1. Upload a text file using the sidebar.
190
+ 2. Enter your query in the text input below.
191
+ 3. Enter your OpenAI API Key in the sidebar.
192
+ 4. Click the 'Get Results' button to process the text content and provide relevant answers based on the content.
193
+ """)
194
+
195
+ def load_and_process_text(file):
196
+ content = file.read().decode('utf-8')
197
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
198
+ splits = text_splitter.split_text(content)
199
+ vectorstore = Chroma.from_texts(texts=splits, embedding=OpenAIEmbeddings())
200
+ retriever = vectorstore.as_retriever()
201
+ system_prompt = (
202
+ "You are an assistant for question-answering tasks. "
203
+ "Use the following pieces of retrieved context to answer "
204
+ "the question. If you don't know the answer, say that you "
205
+ "don't know. Use three sentences maximum and keep the "
206
+ "answer concise."
207
+ "\n\n"
208
+ "{context}"
209
+ )
210
+ prompt = ChatPromptTemplate.from_messages([
211
+ ("system", system_prompt),
212
+ ("human", "{input}"),
213
+ ])
214
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key=api_key)
215
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
216
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
217
+ return rag_chain
218
+
219
+ rag_chain = None
220
+ if uploaded_file:
221
+ rag_chain = load_and_process_text(uploaded_file)
222
+
223
+ input_text = st.text_input("Please feel free to ask any doubts! πŸ“")
224
+
225
+ if st.button("Get Results"):
226
+ if input_text and rag_chain:
227
+ with st.spinner('Processing...'):
228
+ try:
229
+ response = rag_chain.invoke({"input": input_text})
230
+ st.write(response["answer"])
231
+ except Exception as e:
232
+ st.error(f"An error occurred: {e}")
233
+ elif input_text:
234
+ st.warning("Please upload a text file to ask questions.")
235
+
236
+ def audio_rag_page():
237
+ st.title('Audio RAG 🎀')
238
+ st.sidebar.header("Upload your Audio")
239
+ uploaded_file = st.sidebar.file_uploader("Choose an audio file", type=["wav"])
240
+
241
+ st.write("### Instructions πŸ“œ")
242
+ st.write("""
243
+ 1. Ensure your audio file is in a supported format (PCM WAV, AIFF/AIFF-C, or Native FLAC).
244
+ 2. Upload an audio file using the sidebar.
245
+ 3. Enter your query in the text input below.
246
+ 4. Enter your OpenAI API Key in the sidebar.
247
+ 5. Click the 'Get Results' button to process the text extracted from the audio and provide relevant answers based on the content.
248
+ """)
249
+
250
+ def load_and_process_audio(file):
251
+ recognizer = sr.Recognizer()
252
+ try:
253
+ audio_file = sr.AudioFile(file)
254
+ except ValueError:
255
+ st.error("Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format.")
256
+ return None
257
+ with audio_file as source:
258
+ audio = recognizer.record(source)
259
+ text = recognizer.recognize_google(audio)
260
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
261
+ splits = text_splitter.split_text(text)
262
+ vectorstore = Chroma.from_texts(texts=splits, embedding=OpenAIEmbeddings())
263
+ retriever = vectorstore.as_retriever()
264
+ system_prompt = (
265
+ "You are an assistant for question-answering tasks. "
266
+ "Use the following pieces of retrieved context to answer "
267
+ "the question. If you don't know the answer, say that you "
268
+ "don't know. Use three sentences maximum and keep the "
269
+ "answer concise."
270
+ "\n\n"
271
+ "{context}"
272
+ )
273
+ prompt = ChatPromptTemplate.from_messages([
274
+ ("system", system_prompt),
275
+ ("human", "{input}"),
276
+ ])
277
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key=api_key)
278
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
279
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
280
+ return rag_chain
281
+
282
+ rag_chain = None
283
+ if uploaded_file:
284
+ rag_chain = load_and_process_audio(uploaded_file)
285
+
286
+ input_text = st.text_input("Please feel free to ask any doubts! πŸ“")
287
+
288
+ if st.button("Get Results"):
289
+ if input_text and rag_chain:
290
+ with st.spinner('Processing...'):
291
+ try:
292
+ response = rag_chain.invoke({"input": input_text})
293
+ st.write(response["answer"])
294
+ except Exception as e:
295
+ st.error(f"An error occurred: {e}")
296
+ elif input_text:
297
+ st.warning("Please upload an audio file to ask questions.")
298
+
299
+ def database_rag_page():
300
+ st.title('Database RAG πŸ—„οΈ')
301
+ st.sidebar.header("Enter Database Credentials")
302
+ db_url = st.sidebar.text_input("Database URL")
303
+ table_name = st.sidebar.text_input("Table Name")
304
+
305
+ st.write("### Instructions πŸ“œ")
306
+ st.write("""
307
+ 1. Enter the database URL and table name using the sidebar.
308
+ 2. Enter your query in the text input below.
309
+ 3. Enter your OpenAI API Key in the sidebar.
310
+ 4. Click the 'Get Results' button to process the data from the specified table and provide relevant answers based on the content.
311
+ """)
312
+
313
+ def load_and_process_db(db_url, table_name):
314
+ engine = create_engine(db_url)
315
+ df = pd.read_sql_table(table_name, engine)
316
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
317
+ splits = text_splitter.split_text(df.to_string())
318
+ vectorstore = Chroma.from_texts(texts=splits, embedding=OpenAIEmbeddings())
319
+ retriever = vectorstore.as_retriever()
320
+ system_prompt = (
321
+ "You are an assistant for question-answering tasks. "
322
+ "Use the following pieces of retrieved context to answer "
323
+ "the question. If you don't know the answer, say that you "
324
+ "don't know. Use three sentences maximum and keep the "
325
+ "answer concise."
326
+ "\n\n"
327
+ "{context}"
328
+ )
329
+ prompt = ChatPromptTemplate.from_messages([
330
+ ("system", system_prompt),
331
+ ("human", "{input}"),
332
+ ])
333
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key=api_key)
334
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
335
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
336
+ return rag_chain
337
+
338
+ rag_chain = None
339
+ if db_url and table_name:
340
+ rag_chain = load_and_process_db(db_url, table_name)
341
+
342
+ input_text = st.text_input("Please feel free to ask any doubts! πŸ“")
343
+
344
+ if st.button("Get Results"):
345
+ if input_text and rag_chain:
346
+ with st.spinner('Processing...'):
347
+ try:
348
+ response = rag_chain.invoke({"input": input_text})
349
+ st.write(response["answer"])
350
+ except Exception as e:
351
+ st.error(f"An error occurred: {e}")
352
+ elif input_text:
353
+ st.warning("Please enter database credentials to ask questions.")
354
+
355
+ def api_rag_page():
356
+ st.title('API RAG πŸ”Œ')
357
+ st.sidebar.header("Enter API Endpoint")
358
+ api_url = st.sidebar.text_input("API URL")
359
+
360
+ st.write("### Instructions πŸ“œ")
361
+ st.write("""
362
+ 1. Enter the API URL using the sidebar.
363
+ 2. Enter your query in the text input below.
364
+ 3. Enter your OpenAI API Key in the sidebar.
365
+ 4. Click the 'Get Results' button to process the data from the specified API endpoint and provide relevant answers based on the content.
366
+ """)
367
+
368
+ def load_and_process_api(api_url):
369
+ response = requests.get(api_url)
370
+ data = response.json()
371
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
372
+ splits = text_splitter.split_text(str(data))
373
+ vectorstore = Chroma.from_texts(texts=splits, embedding=OpenAIEmbeddings())
374
+ retriever = vectorstore.as_retriever()
375
+ system_prompt = (
376
+ "You are an assistant for question-answering tasks. "
377
+ "Use the following pieces of retrieved context to answer "
378
+ "the question. If you don't know the answer, say that you "
379
+ "don't know. Use three sentences maximum and keep the "
380
+ "answer concise."
381
+ "\n\n"
382
+ "{context}"
383
+ )
384
+ prompt = ChatPromptTemplate.from_messages([
385
+ ("system", system_prompt),
386
+ ("human", "{input}"),
387
+ ])
388
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key=api_key)
389
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
390
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
391
+ return rag_chain
392
+
393
+ rag_chain = None
394
+ if api_url:
395
+ rag_chain = load_and_process_api(api_url)
396
+
397
+ input_text = st.text_input("Please feel free to ask any doubts! πŸ“")
398
+
399
+ if st.button("Get Results"):
400
+ if input_text and rag_chain:
401
+ with st.spinner('Processing...'):
402
+ try:
403
+ response = rag_chain.invoke({"input": input_text})
404
+ st.write(response["answer"])
405
+ except Exception as e:
406
+ st.error(f"An error occurred: {e}")
407
+ elif input_text:
408
+ st.warning("Please enter an API URL to ask questions.")
409
+
410
+ # Extend the navigation menu
411
+ with st.sidebar:
412
+ selected = option_menu(
413
+ "πŸ€– GRASP",
414
+ ["Home", "PDF RAG πŸ“„", "Web RAG 🌐", "Text Document RAG πŸ“„", "Audio RAG 🎀", "Database RAG πŸ—„οΈ", "API RAG πŸ”Œ"],
415
+ icons=["house", "file-earmark-pdf", "globe", "file-earmark-text", "mic", "database", "plug"],
416
+ default_index=0,
417
+ styles={
418
+ "container": {"padding": "5px"},
419
+ "nav-link": {"font-size": "16px", "text-align": "left", "margin": "0px"},
420
+ "nav-link-selected": {"background-color": "#4CAF50"},
421
+ }
422
+ )
423
+
424
+ # Display the selected page
425
+ if selected == "Home":
426
+ home_page()
427
+ elif selected == "PDF RAG πŸ“„":
428
+ pdf_rag_page()
429
+ elif selected == "Web RAG 🌐":
430
+ web_rag_page()
431
+ elif selected == "Text Document RAG πŸ“„":
432
+ text_document_rag_page()
433
+ elif selected == "Audio RAG 🎀":
434
+ audio_rag_page()
435
+ elif selected == "Database RAG πŸ—„οΈ":
436
+ database_rag_page()
437
+ elif selected == "API RAG πŸ”Œ":
438
+ api_rag_page()
439
+
440
+ # Additional User Feedback Section
441
+ st.sidebar.header("User Feedback")
442
+ feedback = st.sidebar.text_area("Provide your feedback here:")
443
+ if st.sidebar.button("Submit Feedback"):
444
+ with open("feedback.txt", "a") as f:
445
+ f.write(f"Feedback: {feedback}\n")
446
+ st.sidebar.success("Feedback submitted successfully!")