PDFQueryBot / app.py
aakash0563's picture
Update app.py
d5d5fbc
raw
history blame
No virus
2.6 kB
import gradio as gr
import chromadb
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from uuid import uuid4
import google.generativeai as genai
import re
import os
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# Now you can use hugging_face_api_key in your code
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-pro') # Load the model
# Necessary imports for Gradio
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=800,
chunk_overlap=50
)
client = chromadb.PersistentClient("test")
collection = client.create_collection("test_data")
def upload_pdf(file_path):
loader = PyPDFLoader(file_path)
pages = loader.load()
documents = []
for page in pages:
docs = text_splitter.split_text(page.page_content)
for doc in docs:
documents.append({
"text": docs, "meta_data": page.metadata,
})
collection.add(
ids=[str(uuid4()) for _ in range(len(documents))],
documents=[doc['text'][0] for doc in documents],
metadatas=[doc['meta_data'] for doc in documents]
)
return f"PDF Uploaded Successfully. {collection.count()} chunks stored in ChromaDB"
def get_Answer(query):
res = collection.query( # Assuming `collection` is defined elsewhere
query_texts=query,
n_results=2
)
system = f"""You are a teacher. You will be provided some context, 
your task is to analyze the relevant context and answer the below question:
- {query}
"""
context = " ".join([re.sub(r'[^\x00-\x7F]+', ' ', r) for r in res['documents'][0]])
prompt = f"### System: {system} \n\n ###: User: {context} \n\n ### Assistant:\n"
answer = model.generate_content(prompt).text
return answer
# # Define the Gradio interface
# iface = gr.Interface(
# fn=get_Answer,
# inputs=gr.Textbox(lines=5, placeholder="Ask a question"), # Textbox for query
# outputs="textbox", # Display the generated answer in a textbox
# title="Answer Questions with Gemini-Pro",
# description="Ask a question and get an answer based on context from a ChromaDB collection.",
# )
# # Launch the Gradio app
# iface.launch()
# Define the Gradio interface
iface = gr.Interface(
fn=upload_pdf,
inputs=["file"], # Specify a file input component
outputs="textbox", # Display the output text in a textbox
title="Upload PDF to ChromaDB",
description="Upload a PDF file and store its text chunks in ChromaDB.",
)
# Launch the Gradio app
iface.launch()