forked from Srinidhi-Chitti/Mosdac-Interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_handler.py
More file actions
29 lines (24 loc) · 1.2 KB
/
upload_handler.py
File metadata and controls
29 lines (24 loc) · 1.2 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
# CSV and PDF uploader to parse & inject into RAG + KG
import streamlit as st
from langchain_community.document_loaders import CSVLoader, PyMuPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import OllamaEmbeddings
from langchain.text_splitter import CharacterTextSplitter
def handle_upload():
uploaded_file = st.file_uploader("Upload CSV or PDF", type=["csv", "pdf"])
if uploaded_file:
if uploaded_file.name.endswith(".csv"):
with open("uploaded.csv", "wb") as f:
f.write(uploaded_file.read())
loader = CSVLoader(file_path="uploaded.csv")
else:
with open("uploaded.pdf", "wb") as f:
f.write(uploaded_file.read())
loader = PyMuPDFLoader("uploaded.pdf")
docs = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
split_docs = text_splitter.split_documents(docs)
embeddings = OllamaEmbeddings(model="nomic-embed-text")
db = FAISS.from_documents(split_docs, embeddings)
db.save_local("custom_isro_vectorstore")
st.success("✅ File uploaded and indexed successfully!")