-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
415 lines (358 loc) · 15.4 KB
/
Copy pathapp.py
File metadata and controls
415 lines (358 loc) · 15.4 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import os
import queue
import re
import tempfile
import threading
import requests
import streamlit as st
import markdown2
from embedchain import App
from embedchain.config import BaseLlmConfig
from embedchain.helpers.callbacks import StreamingStdOutCallbackHandlerYield, generate
# __import__('pysqlite3')
# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
@st.cache_data
def realtime_search(query, domains, max):
url = "https://real-time-web-search.p.rapidapi.com/search"
# Combine domains and query
full_query = f"{domains} {query}"
querystring = {"q": full_query, "limit": max}
headers = {
"X-RapidAPI-Key": st.secrets["X-RapidAPI-Key"],
"X-RapidAPI-Host": "real-time-web-search.p.rapidapi.com",
}
urls = []
snippets = []
try:
response = requests.get(url, headers=headers, params=querystring)
# Check if the request was successful
if response.status_code == 200:
response_data = response.json()
# st.write(response_data.get('data', []))
for item in response_data.get("data", []):
urls.append(item.get("url"))
snippets.append(
f"**{item.get('title')}** \n*{item.get('snippet')}* \n{item.get('url')} <END OF SITE>"
)
else:
st.error(f"Search failed with status code: {response.status_code}")
return [], []
except requests.exceptions.RequestException as e:
st.error(f"RapidAPI real-time search failed to respond: {e}")
return [], []
return snippets, urls
def clean_text(text):
text = re.sub(r"([a-z])([A-Z])", r"\1 \2", text)
text = text.replace("-", " ").replace(" .", ".")
text = re.sub(r"\s{2,}", " ", text) # Replace multiple spaces with a single space
return text
def refine_output(data):
with st.expander("Source Excerpts:"):
for text, info in sorted(data, key=lambda x: x[1]["score"], reverse=True)[:3]:
st.write(f"Score: {info['score']}\n")
cleaned_text = clean_text(text)
# if "Table" in cleaned_text:
# st.write("Extracted Table:")
# st.write(create_table_from_text(cleaned_text)) # Example of integrating table extraction
# else:
st.write("Text:\n", cleaned_text)
st.write("\n")
def process_data(data):
# Sort the data based on the score in descending order and select the top three
top_three = sorted(data, key=lambda x: x[1]["score"], reverse=True)[:3]
# Format each text entry
for text, info in top_three:
cleaned_text = clean_text(text)
st.write(f"Score: {info['score']}\nText: {cleaned_text}\n")
def embedchain_bot(db_path, api_key):
return App.from_config(
config={
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o",
"temperature": 0.5,
"max_tokens": 4000,
"top_p": 1,
"stream": True,
"api_key": api_key,
},
},
"vectordb": {
"provider": "chroma",
"config": {
"collection_name": "pad",
"dir": "db_pad",
"allow_reset": False,
},
},
"embedder": {
"provider": "openai",
"config": {"api_key": api_key, "model": "text-embedding-3-small"},
},
"chunker": {
"chunk_size": 2000,
"chunk_overlap": 0,
"length_function": "len",
},
}
)
def get_db_path():
# tmpdirname = tempfile.mkdtemp()
# tmpdirname = tempfile.mkdtemp(prefix= "pdf_")
return "db_pad"
def get_ec_app(api_key):
if "app" in st.session_state:
print("Found app in session state")
app = st.session_state.app
else:
print("Creating app")
db_path = get_db_path()
app = embedchain_bot(db_path, api_key)
st.session_state.app = app
return app
def check_password():
"""Returns `True` if the user has entered the correct password."""
def password_entered():
"""Checks whether the entered password is correct."""
st.session_state["password_correct"] = (
st.session_state["password"] == st.secrets["password"]
)
if "password_correct" not in st.session_state:
# First run, show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
st.write(
"*Please contact David Liebovitz, MD if you need an updated password for access.*"
)
return False
elif not st.session_state["password_correct"]:
# Password not correct, show input + error.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
st.error("😕 Password incorrect")
return False
else:
# Password correct.
return True
st.title("📄 Chat with AI Sally about PAD!")
# st.warning("Before using - clear the database on left sidebar! I'm working to make sure it starts empty! ")
if check_password():
if "data_type" not in st.session_state:
st.session_state.data_type = "pdf"
# PDF Additions
with st.sidebar:
st.header("Give AI Sally More Knowledge! Upload PDF Files or Search the Web")
# "Just paste your OpenAI API key here and we'll use it to power the chatbot. [Get your OpenAI API key](https://platform.openai.com/api-keys)" # noqa: E501
openai_access_token = st.secrets["OPENAI_API_KEY"]
st.session_state.api_key = openai_access_token
if st.session_state.api_key:
app = get_ec_app(st.session_state.api_key)
pdf_files = st.file_uploader(
"Upload your PDF files", accept_multiple_files=True, type="pdf"
)
# st.write("File Upload history only ⬆️. Section below shows current files in the knowledge base⬇️.")
add_pdf_files = st.session_state.get("add_pdf_files", [])
for pdf_file in pdf_files:
file_name = pdf_file.name
if file_name in add_pdf_files:
continue
try:
if not st.session_state.api_key:
st.error("Please enter your OpenAI API Key")
st.stop()
temp_file_name = None
with tempfile.NamedTemporaryFile(
mode="wb", delete=False, prefix=file_name, suffix=".pdf"
) as f:
f.write(pdf_file.getvalue())
temp_file_name = f.name
if temp_file_name:
st.markdown(f"Adding {file_name} to knowledge base...")
app.add(temp_file_name, data_type="pdf_file")
st.markdown("")
add_pdf_files.append(file_name)
os.remove(temp_file_name)
st.session_state.messages_pdf.append(
{
"role": "assistant",
"content": f"Added {file_name} to knowledge base!",
}
)
except Exception as e:
st.error(f"Error adding {file_name} to knowledge base: {e}")
st.stop()
st.session_state["add_pdf_files"] = add_pdf_files
# web additions!
openai_access_token = st.secrets["OPENAI_API_KEY"]
st.session_state.api_key = openai_access_token
all_site_text = []
if "snippets" not in st.session_state:
st.session_state["snippets"] = []
if "urls" not in st.session_state:
st.session_state["urls"] = []
st.divider()
st.subheader("Search the Web!")
initial_search = st.text_input(
"Enter search terms to send pages to your AI!", max_chars=4000
)
site_number = st.number_input(
"Number of web pages to retrieve:",
min_value=1,
max_value=15,
value=6,
step=1,
)
restrict_domains = st.checkbox(
"Restrict search to reliable medical domains", value=False
)
medical_domains = """site:www.nih.gov OR site:www.ncbi.nlm.nih.gov/books OR site:www.cdc.gov OR site:www.who.int OR site:www.pubmed.gov OR site:www.cochranelibrary.com OR
site:www.uptodate.com OR site:www.medscape.com OR site:www.ama-assn.org OR site:www.nejm.org OR
site:www.bmj.com OR site:www.thelancet.com OR site:www.jamanetwork.com OR site:www.mayoclinic.org OR site:www.acpjournals.org OR
site:www.cell.com OR site:www.nature.com OR site:www.springer.com OR site:www.wiley.com OR site:www.ahrq.gov OR site:www.edu"""
if restrict_domains:
domains = medical_domains
else:
domains = ""
if st.button("Search"):
st.session_state.snippets, st.session_state.urls = realtime_search(
initial_search, domains, site_number
)
for site in st.session_state.urls:
try:
app.add(site, data_type="web_page")
# st.session_state.search_results += f"{site}\n"
except Exception:
# st.error(f"Error adding {site}: {e}, skipping that one!")
st.sidebar.error(
f"This site, {site}, won't let us retrieve content. Skipping it."
)
with st.sidebar:
with st.expander("View Search Result Snippets"):
if st.session_state.snippets:
for snippet in st.session_state.snippets:
snippet = snippet.replace("<END OF SITE>", "")
st.markdown(snippet)
else:
st.markdown("No search results found!")
if "messages_pdf" not in st.session_state:
st.session_state.messages_pdf = [
{
"role": "assistant",
"content": """
Hi! I'm an AI chatbot running the latest OpenAI GPT-4o model. I can answer questions about your pdfs or web search results.\n
Please upload your ⬅️ pdfs, or search the ⬅️ web and I'll answer questions about the content.
""",
}
]
for message in st.session_state.messages_pdf:
if message["role"] != "system":
with st.chat_message(message["role"]):
st.markdown(message["content"])
prompt_guidance = (
"\n\n"
+ """Please structure your response into two distinct sections:\n
## Contextual Response:\n
Provide a detailed response using only the information from the provided context.\n
## Expert Commentary:\n
Offer insights or commentary from a domain expert's perspective."""
)
if st.sidebar.checkbox(
"Just summarize the context (enter a space into the prompt)", value=False
):
prompt_guidance = (
"\n\n"
+ "Please summarize each context file individually. Identify and list the title, authors, and publication year for each context file. Then, create an organized outline of the key assertions from each file. Conclude with a concise three-sentence summary."
)
if prompt := st.chat_input("Ask me anything!"):
tweaked_prompt = prompt + prompt_guidance
if not st.session_state.api_key:
st.error("Please enter your OpenAI API Key", icon="🤖")
st.stop()
app = get_ec_app(st.session_state.api_key)
with st.chat_message("user"):
st.session_state.messages_pdf.append({"role": "user", "content": prompt})
st.markdown(prompt)
with st.chat_message("assistant"):
msg_placeholder = st.empty()
msg_placeholder.markdown("Thinking...")
full_response = ""
q = queue.Queue()
def app_response(result):
llm_config = app.llm.config.as_dict()
llm_config["callbacks"] = [StreamingStdOutCallbackHandlerYield(q=q)]
config = BaseLlmConfig(**llm_config)
answer, citations = app.query(
tweaked_prompt, config=config, citations=True
)
result["answer"] = answer
result["citations"] = citations
results = {}
thread = threading.Thread(target=app_response, args=(results,))
thread.start()
for answer_chunk in generate(q):
full_response += answer_chunk
msg_placeholder.markdown(full_response)
thread.join()
answer, citations = results["answer"], results["citations"]
if citations:
full_response += "\n\n**Sources**:\n"
sources = []
for i, citation in enumerate(citations):
source = citation[1]["url"]
pattern = re.compile(r"([^/]+)\.[^\.]+\.pdf$")
match = pattern.search(source)
if match:
source = match.group(1) + ".pdf"
sources.append(source)
sources = list(set(sources))
for source in sources:
full_response += f"- {source}\n"
# st.write(f' here are the full {citations}')
refine_output(citations)
msg_placeholder.markdown(full_response)
# print("Answer: ", full_response)
st.session_state.messages_pdf.append(
{"role": "assistant", "content": full_response}
)
# app = App()
data_sources = app.get_data_sources()
# st.sidebar.write("Files in database: ", len(data_sources))
with st.sidebar:
st.divider()
st.subheader("Files in database:")
with st.expander(f"See {len(data_sources)} files in database."):
for i in range(len(data_sources)):
full_path = data_sources[i]["data_value"]
# Extract just the filename from the full path
temp_filename = os.path.basename(full_path)
# Use regex to only keep up to the first .pdf in the filename
cleaned_filename = re.sub(r"^(.+?\.pdf).*$", r"\1", temp_filename)
st.write(i, ": ", cleaned_filename)
if st.sidebar.button("Clear database (click twice to confirm)"):
app = App()
app.reset()
if st.session_state.messages_pdf:
if st.sidebar.button("Clear chat history."):
st.session_state["messages_pdf"] = []
if st.session_state.messages_pdf:
pdf_conversation_str = "\n\n".join(
f"👩⚕️: {msg['content']}"
if msg["role"] == "user"
else f"🤓: {msg['content']}"
for msg in st.session_state.messages_pdf
)
html = markdown2.markdown(pdf_conversation_str, extras=["tables"])
st.download_button(
"Download the PDF conversation", html, "pdf_conversation.html", "text/html"
)
# @misc{embedchain,
# author = {Taranjeet Singh, Deshraj Yadav},
# title = {Embedchain: The Open Source RAG Framework},
# year = {2023},
# publisher = {GitHub},
# journal = {GitHub repository},
# howpublished = {\url{https://github.com/embedchain/embedchain}},
# }