File size: 2,412 Bytes
fa67bc8 d78dbfd 0308cfc fa67bc8 0308cfc e8b031a 0308cfc e6e4b49 0308cfc e6e4b49 0308cfc b63b2a2 0308cfc e6e4b49 fa67bc8 0308cfc fa67bc8 0308cfc d051b0c 0308cfc fa67bc8 0308cfc fa67bc8 0308cfc fa67bc8 |
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 69 |
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, history):
# Example: returning a placeholder response, update with actual chatbot logic
bot_response = "You said: " + user_input
history.append((user_input, bot_response))
return bot_response, history
# 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, state], outputs=[chatbot, state])
# 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()
|