|
import gradio as gr |
|
import PyPDF2 |
|
|
|
from secret1 import GOOGLE_API as google_api |
|
from langchain.llms import GooglePalm |
|
from langchain.embeddings import HuggingFaceInstructEmbeddings |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain.embeddings import GooglePalmEmbeddings |
|
from langchain.vectorstores import FAISS |
|
from langchain.document_loaders import PyPDFLoader |
|
from langchain.chains import RetrievalQA |
|
import google.generativeai as genai |
|
|
|
def chatbot_response(user_input): |
|
|
|
bot_response = "You said: " + user_input |
|
|
|
return "hii" |
|
|
|
|
|
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() or "" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client = genai.Client(api_key="AIzaSyBaY8zx4ak0t4TkBp28lL2hLqREzlN_Mb0",location='us-central1') |
|
response = client.models.generate_content( |
|
model="gemini-2.0-flash", contents=f"you will be given the input data you have to answer the question according to the user input : {text}" |
|
) |
|
return print(response.text) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Chat with ChatGPT-like Interface") |
|
|
|
output = gr.Textbox(label="Output Box") |
|
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, inputs=input_file, outputs=output) |
|
|
|
|
|
send_btn.click(chatbot_response, inputs=user_input, outputs=output) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
demo.launch() |
|
|