-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save pdf content in chroma collection (#5)
- Loading branch information
Showing
5 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,53 @@ | ||
""" A simple example of Streamlit. """ | ||
import streamlit as st | ||
from datetime import datetime as Date | ||
import chromadb | ||
import fitz | ||
import streamlit as st | ||
|
||
# from tika import parser | ||
# from openai import OpenAI | ||
|
||
# client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | ||
chroma_client = chromadb.PersistentClient(path="tmp/chroma") | ||
chroma_client.heartbeat() | ||
|
||
collection = chroma_client.get_or_create_collection("pdf-explainer") | ||
|
||
# Query ChromaDb | ||
query = st.text_input("Query ChromaDb", value="", placeholder="Enter query") | ||
if st.button("Search"): | ||
results = collection.query( | ||
query_texts=[query], | ||
n_results=3, | ||
) | ||
|
||
for idx, result in enumerate(results["documents"][0]): | ||
st.markdown( | ||
result[0:150] | ||
+ "..." | ||
+ "**Source:** " | ||
+ results["metadatas"][0][idx]["source"] | ||
) | ||
|
||
|
||
pdf = st.file_uploader("Upload a file", type="pdf") | ||
|
||
if st.button("Extract text"): | ||
|
||
if st.button("Save"): | ||
if pdf is not None: | ||
with fitz.open(stream=pdf.read(), filetype="pdf") as doc: # open document | ||
text = chr(12).join([page.get_text() for page in doc]) | ||
st.write(text) | ||
st.write(text[0:200]) | ||
collection.add( | ||
documents=[text], | ||
metadatas=[{"source": pdf.name}], | ||
ids=[pdf.name + str(Date.now())], | ||
) | ||
else: | ||
st.write("Please upload a file of type: pdf") | ||
|
||
|
||
if st.button("Chroma data collection"): | ||
st.write(collection) | ||
|
||
if st.button("Delete Chroma Collection"): | ||
chroma_client.delete_collection(collection.name) | ||
st.write("Deleted Chroma Collection") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
openai | ||
tiktoken | ||
langchain | ||
pymupdf | ||
pypdf | ||
chromadb | ||
sentence_transformers | ||
streamlit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
streamlit | ||
pymupdf | ||
pylint | ||
pylint | ||
chromadb |