generated from streamlit/streamlit-hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.py
38 lines (28 loc) · 1.11 KB
/
temp.py
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
import os
from dotenv import load_dotenv
from pinecone import Pinecone as Pine
from langchain_community.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
import pandas as pd
# Load environment variables
load_dotenv()
# Initialize Pinecone
pc = Pine(api_key=os.environ.get("PINECONE_API_KEY"))
# Specify Pinecone index name
index_name = "physical-therapy"
index = pc.Index(index_name)
# Directory with documents
directory = 'content/Surgery'
def load_docs(directory):
"""Load documents from the specified directory."""
loader = DirectoryLoader(directory)
return loader.load()
def split_docs(documents, chunk_size=500, chunk_overlap=20):
"""Split documents into chunks for processing."""
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
return text_splitter.split_documents(documents)
# Load and split documents
documents = load_docs(directory)
docs = split_docs(documents)
text_documents = [doc.page_content for doc in docs]
df = pd.DataFrame([d.page_content for d in documents], columns=["text"])