|
import gradio as gr |
|
from langchain_community.llms import GooglePalm |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain_community.embeddings import GooglePalmEmbeddings |
|
from langchain_community.vectorstores import FAISS |
|
from langchain.chains import RetrievalQA |
|
from secret1 import GOOGLE_API as google_api |
|
import PyPDF2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
def helper(text_splitter): |
|
db = FAISS.from_texts(text_splitter, embeddings); |
|
return 'hi'; |
|
|
|
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() |
|
text_splitter=text_splitter_function(text); |
|
|
|
result=helper(text_splitter); |
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
embeddings=GooglePalmEmbeddings(google_api_key=google_api) |
|
demo.launch() |
|
|