Spaces:
Sleeping
Sleeping
Technocoloredgeek
commited on
Commit
•
eb5a0c9
1
Parent(s):
e9ac804
Update app.py
Browse filesFixing display issue
app.py
CHANGED
@@ -1,159 +1,103 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
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 |
-
|
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 |
-
" all_chunks = []\n",
|
105 |
-
" async with aiohttp.ClientSession() as session:\n",
|
106 |
-
" pdf_contents = await asyncio.gather(*[fetch_pdf(session, url) for url in pdf_urls])\n",
|
107 |
-
" \n",
|
108 |
-
" for pdf_content in pdf_contents:\n",
|
109 |
-
" if pdf_content:\n",
|
110 |
-
" chunks = await process_pdf(pdf_content)\n",
|
111 |
-
" all_chunks.extend(chunks)\n",
|
112 |
-
"\n",
|
113 |
-
" print(f\"Created {len(all_chunks)} chunks from {len(pdf_urls)} PDF files\")\n",
|
114 |
-
"\n",
|
115 |
-
" embeddings = OpenAIEmbeddings(openai_api_key=api_key)\n",
|
116 |
-
" vector_db = Chroma.from_texts(all_chunks, embeddings)\n",
|
117 |
-
" \n",
|
118 |
-
" chat_openai = ChatOpenAI(openai_api_key=api_key)\n",
|
119 |
-
" retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(vector_db=vector_db, llm=chat_openai)\n",
|
120 |
-
" \n",
|
121 |
-
" # Example query\n",
|
122 |
-
" query = \"What are the key principles of the AI Bill of Rights?\"\n",
|
123 |
-
" result = await retrieval_augmented_qa_pipeline.arun_pipeline(query)\n",
|
124 |
-
" \n",
|
125 |
-
" print(\"Query:\", query)\n",
|
126 |
-
" print(\"\\nResponse:\")\n",
|
127 |
-
" async for chunk in result[\"response\"]:\n",
|
128 |
-
" print(chunk, end=\"\")\n",
|
129 |
-
" print(\"\\n\\nContext used:\")\n",
|
130 |
-
" for i, context in enumerate(result[\"context\"], 1):\n",
|
131 |
-
" print(f\"{i}. {context[:100]}...\")\n",
|
132 |
-
"\n",
|
133 |
-
"# Cell 8: Run the main function\n",
|
134 |
-
"await main()"
|
135 |
-
]
|
136 |
-
}
|
137 |
-
],
|
138 |
-
"metadata": {
|
139 |
-
"kernelspec": {
|
140 |
-
"display_name": "base",
|
141 |
-
"language": "python",
|
142 |
-
"name": "python3"
|
143 |
-
},
|
144 |
-
"language_info": {
|
145 |
-
"codemirror_mode": {
|
146 |
-
"name": "ipython",
|
147 |
-
"version": 3
|
148 |
-
},
|
149 |
-
"file_extension": ".py",
|
150 |
-
"mimetype": "text/x-python",
|
151 |
-
"name": "python",
|
152 |
-
"nbconvert_exporter": "python",
|
153 |
-
"pygments_lexer": "ipython3",
|
154 |
-
"version": "3.10.14"
|
155 |
-
}
|
156 |
-
},
|
157 |
-
"nbformat": 4,
|
158 |
-
"nbformat_minor": 2
|
159 |
-
}
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import asyncio
|
3 |
+
import os
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.document_loaders import PyPDFLoader
|
6 |
+
from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
7 |
+
from langchain.vectorstores import Chroma
|
8 |
+
from langchain.embeddings import OpenAIEmbeddings
|
9 |
+
from langchain.chat_models import ChatOpenAI
|
10 |
+
from PyPDF2 import PdfReader
|
11 |
+
import aiohttp
|
12 |
+
from io import BytesIO
|
13 |
+
|
14 |
+
# Set up API key
|
15 |
+
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
|
16 |
+
|
17 |
+
# Set up prompts
|
18 |
+
system_template = "Use the following context to answer a user's question. If you cannot find the answer in the context, say you don't know the answer."
|
19 |
+
system_role_prompt = SystemMessagePromptTemplate.from_template(system_template)
|
20 |
+
|
21 |
+
user_prompt_template = "Context:\n{context}\n\nQuestion:\n{question}"
|
22 |
+
user_role_prompt = HumanMessagePromptTemplate.from_template(user_prompt_template)
|
23 |
+
|
24 |
+
# Define RetrievalAugmentedQAPipeline class
|
25 |
+
class RetrievalAugmentedQAPipeline:
|
26 |
+
def __init__(self, llm: ChatOpenAI, vector_db: Chroma) -> None:
|
27 |
+
self.llm = llm
|
28 |
+
self.vector_db = vector_db
|
29 |
+
|
30 |
+
async def arun_pipeline(self, user_query: str):
|
31 |
+
context_docs = self.vector_db.similarity_search(user_query, k=2)
|
32 |
+
context_list = [doc.page_content for doc in context_docs]
|
33 |
+
context_prompt = "\n".join(context_list)
|
34 |
+
|
35 |
+
max_context_length = 12000
|
36 |
+
if len(context_prompt) > max_context_length:
|
37 |
+
context_prompt = context_prompt[:max_context_length]
|
38 |
+
|
39 |
+
formatted_system_prompt = system_role_prompt.format()
|
40 |
+
formatted_user_prompt = user_role_prompt.format(question=user_query, context=context_prompt)
|
41 |
+
|
42 |
+
response = await self.llm.agenerate([formatted_system_prompt, formatted_user_prompt])
|
43 |
+
return {"response": response.generations[0][0].text, "context": context_list}
|
44 |
+
|
45 |
+
# PDF processing functions
|
46 |
+
async def fetch_pdf(session, url):
|
47 |
+
async with session.get(url) as response:
|
48 |
+
if response.status == 200:
|
49 |
+
return await response.read()
|
50 |
+
else:
|
51 |
+
st.error(f"Failed to fetch PDF from {url}")
|
52 |
+
return None
|
53 |
+
|
54 |
+
async def process_pdf(pdf_content):
|
55 |
+
pdf_reader = PdfReader(BytesIO(pdf_content))
|
56 |
+
text = "\n".join([page.extract_text() for page in pdf_reader.pages])
|
57 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
58 |
+
return text_splitter.split_text(text)
|
59 |
+
|
60 |
+
@st.cache_resource
|
61 |
+
def initialize_pipeline():
|
62 |
+
return asyncio.run(main())
|
63 |
+
|
64 |
+
# Main execution
|
65 |
+
async def main():
|
66 |
+
pdf_urls = [
|
67 |
+
"https://www.whitehouse.gov/wp-content/uploads/2022/10/Blueprint-for-an-AI-Bill-of-Rights.pdf",
|
68 |
+
"https://nvlpubs.nist.gov/nistpubs/ai/NIST.AI.600-1.pdf",
|
69 |
+
]
|
70 |
+
|
71 |
+
all_chunks = []
|
72 |
+
async with aiohttp.ClientSession() as session:
|
73 |
+
pdf_contents = await asyncio.gather(*[fetch_pdf(session, url) for url in pdf_urls])
|
74 |
+
|
75 |
+
for pdf_content in pdf_contents:
|
76 |
+
if pdf_content:
|
77 |
+
chunks = await process_pdf(pdf_content)
|
78 |
+
all_chunks.extend(chunks)
|
79 |
+
|
80 |
+
st.write(f"Created {len(all_chunks)} chunks from {len(pdf_urls)} PDF files")
|
81 |
+
|
82 |
+
embeddings = OpenAIEmbeddings()
|
83 |
+
vector_db = Chroma.from_texts(all_chunks, embeddings)
|
84 |
+
|
85 |
+
chat_openai = ChatOpenAI()
|
86 |
+
return RetrievalAugmentedQAPipeline(vector_db=vector_db, llm=chat_openai)
|
87 |
+
|
88 |
+
# Streamlit UI
|
89 |
+
st.title("AI Bill of Rights Q&A")
|
90 |
+
|
91 |
+
pipeline = initialize_pipeline()
|
92 |
+
|
93 |
+
user_query = st.text_input("Enter your question about the AI Bill of Rights:")
|
94 |
+
|
95 |
+
if user_query:
|
96 |
+
result = asyncio.run(pipeline.arun_pipeline(user_query))
|
97 |
+
|
98 |
+
st.write("Response:")
|
99 |
+
st.write(result["response"])
|
100 |
+
|
101 |
+
st.write("Context used:")
|
102 |
+
for i, context in enumerate(result["context"], 1):
|
103 |
+
st.write(f"{i}. {context[:100]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|