Shipmaster1 commited on
Commit
5845f06
·
verified ·
1 Parent(s): 348f7b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -107
app.py CHANGED
@@ -1,108 +1,139 @@
1
- import gradio as gr
2
- import os
3
- from aimakerspace.text_utils import PDFLoader, CharacterTextSplitter
4
- from aimakerspace.vectordatabase import VectorDatabase
5
- from aimakerspace.openai_utils.prompts import SystemRolePrompt, UserRolePrompt
6
- from aimakerspace.openai_utils.chatmodel import ChatOpenAI
7
- from aimakerspace.openai_utils.embedding import EmbeddingModel
8
- import asyncio
9
-
10
- # Initialize the RAG pipeline
11
- def initialize_rag():
12
- # Load the PDF
13
- pdf_loader = PDFLoader("data/How-to-Build-a-Career-in-AI.pdf")
14
- documents = pdf_loader.load_documents()
15
-
16
- # Split the documents
17
- text_splitter = CharacterTextSplitter(chunk_size=1500, chunk_overlap=300)
18
- split_documents = text_splitter.split_texts(documents)
19
-
20
- # Create vector database
21
- embedding_model = EmbeddingModel()
22
- vector_db = VectorDatabase(embedding_model=embedding_model)
23
- vector_db = asyncio.run(vector_db.abuild_from_list(split_documents))
24
-
25
- # Set up prompts
26
- RAG_PROMPT_TEMPLATE = """ \
27
- Use the provided context to answer the user's query.
28
-
29
- You may not answer the user's query unless there is specific context in the following text.
30
-
31
- If you do not know the answer, or cannot answer, please respond with "I don't know".
32
- """
33
- rag_prompt = SystemRolePrompt(RAG_PROMPT_TEMPLATE)
34
-
35
- USER_PROMPT_TEMPLATE = """ \
36
- Context:
37
- {context}
38
-
39
- User Query:
40
- {user_query}
41
- """
42
- user_prompt = UserRolePrompt(USER_PROMPT_TEMPLATE)
43
-
44
- # Create ChatOpenAI instance
45
- chat_openai = ChatOpenAI()
46
-
47
- # Create and return pipeline
48
- return RetrievalAugmentedQAPipeline(vector_db_retriever=vector_db, llm=chat_openai)
49
-
50
- class RetrievalAugmentedQAPipeline:
51
- def __init__(self, llm: ChatOpenAI, vector_db_retriever: VectorDatabase) -> None:
52
- self.llm = llm
53
- self.vector_db_retriever = vector_db_retriever
54
-
55
- def run_pipeline(self, user_query: str) -> str:
56
- context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
57
- context_prompt = ""
58
- for context in context_list:
59
- context_prompt += context[0] + "\n"
60
-
61
- formatted_system_prompt = SystemRolePrompt(""" \
62
- Use the provided context to answer the user's query.
63
- You may not answer the user's query unless there is specific context in the following text.
64
- If you do not know the answer, or cannot answer, please respond with "I don't know".
65
- """).create_message()
66
-
67
- formatted_user_prompt = UserRolePrompt(""" \
68
- Context:
69
- {context}
70
-
71
- User Query:
72
- {user_query}
73
- """).create_message(user_query=user_query, context=context_prompt)
74
-
75
- response = self.llm.run([formatted_system_prompt, formatted_user_prompt])
76
- return response
77
-
78
- # Create Gradio interface
79
- def create_interface():
80
- # Initialize RAG pipeline
81
- rag_pipeline = initialize_rag()
82
-
83
- def query_rag(question):
84
- return rag_pipeline.run_pipeline(question)
85
-
86
- with gr.Blocks(title="RAG Implementation") as demo:
87
- gr.Markdown("# RAG Implementation Demo")
88
- gr.Markdown("Ask questions about the 'How to Build a Career in AI' document")
89
-
90
- with gr.Row():
91
- with gr.Column():
92
- question = gr.Textbox(label="Your Question", placeholder="Type your question here...")
93
- submit_btn = gr.Button("Submit")
94
-
95
- with gr.Column():
96
- answer = gr.Textbox(label="Answer", lines=5)
97
-
98
- submit_btn.click(
99
- fn=query_rag,
100
- inputs=question,
101
- outputs=answer
102
- )
103
-
104
- return demo
105
-
106
- if __name__ == "__main__":
107
- demo = create_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  demo.launch()
 
1
+ import gradio as gr
2
+ import os
3
+ from aimakerspace.text_utils import PDFLoader, CharacterTextSplitter
4
+ from aimakerspace.vectordatabase import VectorDatabase
5
+ from aimakerspace.openai_utils.prompts import SystemRolePrompt, UserRolePrompt
6
+ from aimakerspace.openai_utils.chatmodel import ChatOpenAI
7
+ from aimakerspace.openai_utils.embedding import EmbeddingModel
8
+ import asyncio
9
+
10
+ def load_notebook():
11
+ notebook_path = "Pythonic_RAG_Assignment.ipynb"
12
+ if os.path.exists(notebook_path):
13
+ with open(notebook_path, "r", encoding="utf-8") as f:
14
+ return f.read()
15
+ return "Notebook not found"
16
+
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("# RAG Implementation Notebook")
19
+ gr.Markdown("This space contains a Jupyter notebook demonstrating a Retrieval Augmented Generation (RAG) implementation.")
20
+
21
+ with gr.Tabs():
22
+ with gr.TabItem("Notebook Preview"):
23
+ notebook_content = gr.Markdown(load_notebook())
24
+
25
+ with gr.TabItem("About"):
26
+ gr.Markdown("""
27
+ ## About This Space
28
+
29
+ This space contains a Jupyter notebook that demonstrates:
30
+ - PDF document processing
31
+ - Text chunking and embedding
32
+ - Vector database implementation
33
+ - RAG pipeline with context-aware responses
34
+
35
+ To run the notebook locally:
36
+ 1. Clone this repository
37
+ 2. Install requirements: `pip install -r requirements.txt`
38
+ 3. Run: `jupyter notebook Pythonic_RAG_Assignment.ipynb`
39
+ """)
40
+
41
+ # Initialize the RAG pipeline
42
+ def initialize_rag():
43
+ # Load the PDF
44
+ pdf_loader = PDFLoader("data/How-to-Build-a-Career-in-AI.pdf")
45
+ documents = pdf_loader.load_documents()
46
+
47
+ # Split the documents
48
+ text_splitter = CharacterTextSplitter(chunk_size=1500, chunk_overlap=300)
49
+ split_documents = text_splitter.split_texts(documents)
50
+
51
+ # Create vector database
52
+ embedding_model = EmbeddingModel()
53
+ vector_db = VectorDatabase(embedding_model=embedding_model)
54
+ vector_db = asyncio.run(vector_db.abuild_from_list(split_documents))
55
+
56
+ # Set up prompts
57
+ RAG_PROMPT_TEMPLATE = """ \
58
+ Use the provided context to answer the user's query.
59
+
60
+ You may not answer the user's query unless there is specific context in the following text.
61
+
62
+ If you do not know the answer, or cannot answer, please respond with "I don't know".
63
+ """
64
+ rag_prompt = SystemRolePrompt(RAG_PROMPT_TEMPLATE)
65
+
66
+ USER_PROMPT_TEMPLATE = """ \
67
+ Context:
68
+ {context}
69
+
70
+ User Query:
71
+ {user_query}
72
+ """
73
+ user_prompt = UserRolePrompt(USER_PROMPT_TEMPLATE)
74
+
75
+ # Create ChatOpenAI instance
76
+ chat_openai = ChatOpenAI()
77
+
78
+ # Create and return pipeline
79
+ return RetrievalAugmentedQAPipeline(vector_db_retriever=vector_db, llm=chat_openai)
80
+
81
+ class RetrievalAugmentedQAPipeline:
82
+ def __init__(self, llm: ChatOpenAI, vector_db_retriever: VectorDatabase) -> None:
83
+ self.llm = llm
84
+ self.vector_db_retriever = vector_db_retriever
85
+
86
+ def run_pipeline(self, user_query: str) -> str:
87
+ context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
88
+ context_prompt = ""
89
+ for context in context_list:
90
+ context_prompt += context[0] + "\n"
91
+
92
+ formatted_system_prompt = SystemRolePrompt(""" \
93
+ Use the provided context to answer the user's query.
94
+ You may not answer the user's query unless there is specific context in the following text.
95
+ If you do not know the answer, or cannot answer, please respond with "I don't know".
96
+ """).create_message()
97
+
98
+ formatted_user_prompt = UserRolePrompt(""" \
99
+ Context:
100
+ {context}
101
+
102
+ User Query:
103
+ {user_query}
104
+ """).create_message(user_query=user_query, context=context_prompt)
105
+
106
+ response = self.llm.run([formatted_system_prompt, formatted_user_prompt])
107
+ return response
108
+
109
+ # Create Gradio interface
110
+ def create_interface():
111
+ # Initialize RAG pipeline
112
+ rag_pipeline = initialize_rag()
113
+
114
+ def query_rag(question):
115
+ return rag_pipeline.run_pipeline(question)
116
+
117
+ with gr.Blocks(title="RAG Implementation") as demo:
118
+ gr.Markdown("# RAG Implementation Demo")
119
+ gr.Markdown("Ask questions about the 'How to Build a Career in AI' document")
120
+
121
+ with gr.Row():
122
+ with gr.Column():
123
+ question = gr.Textbox(label="Your Question", placeholder="Type your question here...")
124
+ submit_btn = gr.Button("Submit")
125
+
126
+ with gr.Column():
127
+ answer = gr.Textbox(label="Answer", lines=5)
128
+
129
+ submit_btn.click(
130
+ fn=query_rag,
131
+ inputs=question,
132
+ outputs=answer
133
+ )
134
+
135
+ return demo
136
+
137
+ if __name__ == "__main__":
138
+ demo = create_interface()
139
  demo.launch()