YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
- This Python 3 environment comes with many helpful analytics libraries installed
- It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
- For example, here's several helpful packages to load
- 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
- 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
- Load JSON file instead of PDF
- Check if document_text is empty
- Split the document into smaller sections for vectorization
- Load embedding model
- Create FAISS vector store
- Define chatbot prompt
- Initialize Groq API client
- Ensure Groq is correctly installed and the API key is valid.
- Function to process user queries
- Gradio app
This Python 3 environment comes with many helpful analytics libraries installed
It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
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.")
Load embedding model
embedding_model_name = "sentence-transformers/all-MiniLM-L6-v2" try: embeddings_model = HuggingFaceEmbeddings(model_name=embedding_model_name) except Exception as e: raise RuntimeError(f"Error loading embedding model: {e}")
Create FAISS vector store
try: vector_store = FAISS.from_texts( texts=sections, embedding=embeddings_model ) except Exception as e: raise RuntimeError(f"Error creating FAISS vector store: {e}")
Define chatbot prompt
prompt = """ You are a chatbot designed to answer questions about my following CV: {retrieved_data}
User Query: {user_query} """
Initialize Groq API client
Ensure Groq is correctly installed and the API key is valid.
try: from groq.api import Groq client = Groq(api_key="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") except Exception as e: raise RuntimeError(f"Error initializing Groq client: {e}")
Function to process user queries
def process_query(user_query): try: retrieved_docs = vector_store.similarity_search(user_query, k=3) retrieved_data = "\n".join([doc.page_content for doc in retrieved_docs]) formatted_prompt = prompt.format(retrieved_data=retrieved_data, user_query=user_query)
completion = client.chat.completions.create(
model="llama3-70b-8192",
messages=[{"role": "user", "content": formatted_prompt}],
temperature=0.5,
max_tokens=500,
top_p=0.85,
stream=False,
stop=None,
)
response = completion.choices[0].message.content
return response
except Exception as e:
return f"Error processing query: {e}"
Gradio app
with gr.Blocks() as app: gr.Markdown("""
CV Chatbot
Use this chatbot to ask questions about my CV. """)# CV Chatbot
query_input = gr.Textbox(
label="Ask a Question",
placeholder="Enter your question about the CV...",
lines=3
)
query_output = gr.Textbox(
label="Response",
placeholder="The chatbot's response will appear here...",
lines=10,
interactive=False
)
ask_button = gr.Button("Ask")
ask_button.click(
fn=process_query,
inputs=query_input,
outputs=query_output
)
gr.Markdown("""<h3>Disclaimer</h3>
The chatbot provides responses based on my CV content. Verify critical details independently.
""")
app.launch()