Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
-
from langchain_community.llms import GooglePalm
|
3 |
-
from langchain.text_splitter import CharacterTextSplitter
|
4 |
-
from langchain_community.embeddings import GooglePalmEmbeddings
|
5 |
-
from langchain_community.vectorstores import FAISS
|
6 |
-
from langchain.chains import RetrievalQA
|
7 |
-
from secret1 import GOOGLE_API as google_api
|
8 |
import PyPDF2
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
15 |
def text_splitter_function(text):
|
16 |
text_splitter = CharacterTextSplitter(
|
17 |
separator = '\n',
|
@@ -20,51 +21,48 @@ def text_splitter_function(text):
|
|
20 |
length_function = len,
|
21 |
)
|
22 |
texts = text_splitter.split_text(text)
|
23 |
-
return texts
|
24 |
-
|
25 |
|
|
|
26 |
def helper(text_splitter):
|
27 |
-
db = FAISS.from_texts(text_splitter, embeddings)
|
28 |
-
return 'hi'
|
29 |
-
|
|
|
30 |
def text_extract(file):
|
31 |
pdf_reader = PyPDF2.PdfReader(file.name)
|
32 |
-
# Get the number of pages
|
33 |
num_pages = len(pdf_reader.pages)
|
34 |
-
# Extract text from each page
|
35 |
text = ""
|
36 |
for page_num in range(num_pages):
|
37 |
page = pdf_reader.pages[page_num]
|
38 |
-
text += page.extract_text()
|
39 |
-
text_splitter=text_splitter_function(text)
|
40 |
-
|
41 |
-
result=helper(text_splitter);
|
42 |
return result
|
43 |
-
# db = FAISS.from_texts(text_splitter, embeddings);
|
44 |
-
# retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
45 |
-
# llm=GooglePalm(google_api_key=google_api)
|
46 |
-
# qa = RetrievalQA.from_chain_type(
|
47 |
-
# llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True
|
48 |
-
# )
|
49 |
-
# result=qa.invoke("where is tajmahal")
|
50 |
|
51 |
-
|
52 |
-
|
53 |
with gr.Blocks() as demo:
|
54 |
gr.Markdown("# Chat with ChatGPT-like Interface")
|
55 |
|
56 |
chatbot = gr.Chatbot()
|
57 |
state = gr.State([])
|
|
|
58 |
with gr.Row():
|
59 |
with gr.Column():
|
60 |
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
61 |
send_btn = gr.Button("Send")
|
62 |
with gr.Column():
|
63 |
-
input_file=gr.File(label="Upload PDF", file_count="single")
|
64 |
-
submit_btn=gr.Button("Submit")
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
|
|
|
68 |
if __name__ == "__main__":
|
69 |
-
|
|
|
70 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import PyPDF2
|
3 |
+
from langchain.embeddings import GooglePalmEmbeddings
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain.chains import RetrievalQA
|
6 |
+
from langchain.llms import GooglePalm
|
7 |
+
|
8 |
+
# Define chatbot response function
|
9 |
+
def chatbot_response(user_input, history):
|
10 |
+
# Example: returning a placeholder response, update with actual chatbot logic
|
11 |
+
bot_response = "You said: " + user_input
|
12 |
+
history.append((user_input, bot_response))
|
13 |
+
return bot_response, history
|
14 |
|
15 |
+
# Define text splitter function
|
16 |
def text_splitter_function(text):
|
17 |
text_splitter = CharacterTextSplitter(
|
18 |
separator = '\n',
|
|
|
21 |
length_function = len,
|
22 |
)
|
23 |
texts = text_splitter.split_text(text)
|
24 |
+
return texts
|
|
|
25 |
|
26 |
+
# Helper function for text processing
|
27 |
def helper(text_splitter):
|
28 |
+
db = FAISS.from_texts(text_splitter, embeddings) # Use 'embeddings' for FAISS
|
29 |
+
return 'hi'
|
30 |
+
|
31 |
+
# PDF text extraction function
|
32 |
def text_extract(file):
|
33 |
pdf_reader = PyPDF2.PdfReader(file.name)
|
|
|
34 |
num_pages = len(pdf_reader.pages)
|
|
|
35 |
text = ""
|
36 |
for page_num in range(num_pages):
|
37 |
page = pdf_reader.pages[page_num]
|
38 |
+
text += page.extract_text() or ""
|
39 |
+
text_splitter = text_splitter_function(text) # Split extracted text into chunks
|
40 |
+
result = helper(text_splitter) # Call helper to process text chunks
|
|
|
41 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
# Define Gradio interface
|
|
|
44 |
with gr.Blocks() as demo:
|
45 |
gr.Markdown("# Chat with ChatGPT-like Interface")
|
46 |
|
47 |
chatbot = gr.Chatbot()
|
48 |
state = gr.State([])
|
49 |
+
|
50 |
with gr.Row():
|
51 |
with gr.Column():
|
52 |
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
53 |
send_btn = gr.Button("Send")
|
54 |
with gr.Column():
|
55 |
+
input_file = gr.File(label="Upload PDF", file_count="single")
|
56 |
+
submit_btn = gr.Button("Submit")
|
57 |
+
|
58 |
+
# Connect submit button to text_extract function
|
59 |
+
submit_btn.click(text_extract, inputs=[input_file], outputs=[user_input])
|
60 |
+
|
61 |
+
# Connect send button to chatbot_response function
|
62 |
+
send_btn.click(chatbot_response, inputs=[user_input, state], outputs=[chatbot, state])
|
63 |
|
64 |
+
# Initialize embeddings and launch the app
|
65 |
if __name__ == "__main__":
|
66 |
+
google_api_key = "YOUR_GOOGLE_API_KEY" # Replace with your actual Google API key
|
67 |
+
embeddings = GooglePalmEmbeddings(google_api_key=google_api_key)
|
68 |
demo.launch()
|