ChatBot / app.py
chatbytes's picture
Update app.py
be93c19 verified
raw
history blame
2.39 kB
import gradio as gr
import PyPDF2
from langchain.embeddings import GooglePalmEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.llms import GooglePalm
# Define chatbot response function
def chatbot_response(user_input):
# Example: returning a placeholder response, update with actual chatbot logic
# bot_response = "You said: " + user_input
# history.append((user_input, bot_response))
return "hii"
# Define text splitter function
def text_splitter_function(text):
text_splitter = CharacterTextSplitter(
separator = '\n',
chunk_size = 1000,
chunk_overlap = 40,
length_function = len,
)
texts = text_splitter.split_text(text)
return texts
# Helper function for text processing
def helper(text_splitter):
db = FAISS.from_texts(text_splitter, embeddings) # Use 'embeddings' for FAISS
return 'hi'
# PDF text extraction function
def text_extract(file):
pdf_reader = PyPDF2.PdfReader(file.name)
num_pages = len(pdf_reader.pages)
text = ""
for page_num in range(num_pages):
page = pdf_reader.pages[page_num]
text += page.extract_text() or ""
text_splitter = text_splitter_function(text) # Split extracted text into chunks
result = helper(text_splitter) # Call helper to process text chunks
return result
# Define Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Chat with ChatGPT-like Interface")
chatbot = gr.Chatbot()
state = gr.State([])
with gr.Row():
with gr.Column():
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
send_btn = gr.Button("Send")
# with gr.Column():
# input_file = gr.File(label="Upload PDF", file_count="single")
# submit_btn = gr.Button("Submit")
# Connect submit button to text_extract function
# submit_btn.click(text_extract, inputs=[input_file], outputs=[user_input])
# Connect send button to chatbot_response function
send_btn.click(chatbot_response, inputs=[user_input], outputs=[chatbot])
# Initialize embeddings and launch the app
if __name__ == "__main__":
google_api_key = "YOUR_GOOGLE_API_KEY" # Replace with your actual Google API key
embeddings = GooglePalmEmbeddings(google_api_key=google_api_key)
demo.launch()