Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# Create
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|