File size: 2,339 Bytes
fa67bc8
2ccc6ea
e8b031a
2ccc6ea
b98d9ca
9f319f2
2ccc6ea
d78dbfd
9b540af
 
 
 
 
fa67bc8
e8b031a
 
 
 
 
 
 
 
 
fa67bc8
b63b2a2
 
 
 
 
 
 
 
 
e8b031a
b98d9ca
 
 
9b707b8
 
 
 
 
 
 
 
f9efe30
2da261e
fa67bc8
 
 
 
 
 
 
 
 
 
 
 
 
 
b63b2a2
be61cf7
fa67bc8
 
2ccc6ea
fa67bc8
6fed879
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 chatbot_response(user_input, history):
#     # This is a placeholder function. Replace with your actual chatbot logic.
#     bot_response = "You said: " + user_input
#     history.append((user_input, bot_response))
#     return history, history

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 text_extract(file):
    pdf_reader = PyPDF2.PdfReader(file.name)
    # Get the number of pages
    num_pages = len(pdf_reader.pages)
    # Extract text from each page
    text = ""
    for page_num in range(num_pages):
        page = pdf_reader.pages[page_num]
        text += page.extract_text()
    text_splitter=text_splitter_function(text);
    db = FAISS.from_texts(text_splitter, embeddings);
    retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
    llm=GooglePalm(google_api_key=google_api)
    try:
        qa = RetrievalQA.from_chain_type(
            llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True
        )
        result=qa.invoke("hi")
    except:
        result="hi"
        
    
    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")
    submit_btn.click(text_extract, [input_file], [user_input])
    #send_btn.click(chatbot_response,[user_input,state],[chatbot, state])

if __name__ == "__main__":
    embeddings=GooglePalmEmbeddings(google_api_key=google_api)
    demo.launch()
#u