For example, here's several helpful packages to load
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
Input data files are available in the read-only "../input/" directory
For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
pip install --quiet PyPDF2 gradio groq langchain langchain-community langchain-huggingface sentence-transformers transformers faiss-gpu
import gradio as gr
from langchain.prompts import PromptTemplate
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from PyPDF2 import PdfReader
from groq import Groq
import os
import json # For handling JSON data
from PyPDF2 import PdfReader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
import gradio as gr
Load JSON file instead of PDF
pdf_path = "/kaggle/input/ai-assignment/pdf_data.json" # Updated path
try:
with open(pdf_path, "r") as f:
data = json.load(f)
# Handle different possible structures of the JSON file
if isinstance(data, list):
# Assuming the text data is in the first element of the list
document_text = data[0].get("text", "") if isinstance(data[0], dict) else ""
elif isinstance(data, dict):
document_text = data.get("text", "")
else:
raise ValueError("Unexpected JSON structure: expected a list or dict.")
except Exception as e:
raise FileNotFoundError(f"Error reading JSON file: {e}")
Check if document_text is empty
if not document_text.strip():
raise ValueError("The document text is empty. Please check the file content.")
Split the document into smaller sections for vectorization
sections = document_text.split("\n\n")
if not sections:
raise ValueError("No sections found in the document text. Please check the formatting.")