Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +93 -2
- requirements.txt +7 -1
app.py
CHANGED
|
@@ -11,8 +11,77 @@ from google.generativeai.types.generation_types import (
|
|
| 11 |
BlockedPromptException,
|
| 12 |
BrokenResponseError
|
| 13 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
load_dotenv()
|
| 15 |
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
url = "https://y39t47-8080.csb.app/search"
|
| 17 |
|
| 18 |
weathe_key = os.getenv("WEATHER_KEY")
|
|
@@ -675,6 +744,10 @@ with cols[2]:
|
|
| 675 |
with cols[3]:
|
| 676 |
graphviz_mode = st.toggle("Graphviz mode", value=False, help="Activate this mode to generate a graph with graphviz in .dot from your message")
|
| 677 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
if image_atachment:
|
| 679 |
image = st.file_uploader("Upload your image", type=['png', 'jpg', 'jpeg'])
|
| 680 |
url = st.text_input("Or paste your image url")
|
|
@@ -685,7 +758,7 @@ else:
|
|
| 685 |
|
| 686 |
|
| 687 |
if txt_atachment:
|
| 688 |
-
txtattachment = st.file_uploader("Upload your text file", type=['txt'])
|
| 689 |
else:
|
| 690 |
txtattachment = None
|
| 691 |
|
|
@@ -694,7 +767,25 @@ if csv_excel_atachment:
|
|
| 694 |
else:
|
| 695 |
csvexcelattachment = None
|
| 696 |
|
| 697 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 698 |
|
| 699 |
if prompt:
|
| 700 |
txt = ''
|
|
|
|
| 11 |
BlockedPromptException,
|
| 12 |
BrokenResponseError
|
| 13 |
)
|
| 14 |
+
from PyPDF2 import PdfReader
|
| 15 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 16 |
+
import os
|
| 17 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 18 |
+
import google.generativeai as genai
|
| 19 |
+
from langchain.vectorstores import FAISS
|
| 20 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 22 |
+
from langchain.prompts import PromptTemplate
|
| 23 |
load_dotenv()
|
| 24 |
import datetime
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def get_pdf_text(pdf_docs):
|
| 28 |
+
text=""
|
| 29 |
+
for pdf in pdf_docs:
|
| 30 |
+
pdf_reader= PdfReader(pdf)
|
| 31 |
+
for page in pdf_reader.pages:
|
| 32 |
+
text+= page.extract_text()
|
| 33 |
+
return text
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def get_text_chunks(text):
|
| 38 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
| 39 |
+
chunks = text_splitter.split_text(text)
|
| 40 |
+
return chunks
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_vector_store(text_chunks):
|
| 44 |
+
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
| 45 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
| 46 |
+
vector_store.save_local("faiss_index")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def get_conversational_chain():
|
| 50 |
+
|
| 51 |
+
prompt_template = """
|
| 52 |
+
Try always to answer only available in the context and please always give perfect answer from the available context , if i say Summerize then Summerize , and if ask key points then give the key points, if i ask who are you then answer 'i am BHAI(Best High-Quality Artificial Intelligence) Made by hk4crprasad' and always give the perfect and correct output, if i say that tell me more about something in the context then tell accordingly.,
|
| 53 |
+
Context:\n {context}?\n
|
| 54 |
+
Question: \n{question}\n
|
| 55 |
+
|
| 56 |
+
Answer:
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
| 60 |
+
temperature=0.5)
|
| 61 |
+
|
| 62 |
+
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
| 63 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
| 64 |
+
|
| 65 |
+
return chain
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def user_input(user_question):
|
| 70 |
+
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
| 71 |
+
|
| 72 |
+
new_db = FAISS.load_local("faiss_index", embeddings)
|
| 73 |
+
docs = new_db.similarity_search(user_question)
|
| 74 |
+
|
| 75 |
+
chain = get_conversational_chain()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
pdfans = chain(
|
| 79 |
+
{"input_documents":docs, "question": user_question}
|
| 80 |
+
, return_only_outputs=True)
|
| 81 |
+
|
| 82 |
+
return pdfans["output_text"]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
url = "https://y39t47-8080.csb.app/search"
|
| 86 |
|
| 87 |
weathe_key = os.getenv("WEATHER_KEY")
|
|
|
|
| 744 |
with cols[3]:
|
| 745 |
graphviz_mode = st.toggle("Graphviz mode", value=False, help="Activate this mode to generate a graph with graphviz in .dot from your message")
|
| 746 |
|
| 747 |
+
with cols[4]:
|
| 748 |
+
pdf_mode = st.toggle("Pdf ask mode", value=False, help="Activate this mode to generate a graph with graphviz in .dot from your message")
|
| 749 |
+
|
| 750 |
+
|
| 751 |
if image_atachment:
|
| 752 |
image = st.file_uploader("Upload your image", type=['png', 'jpg', 'jpeg'])
|
| 753 |
url = st.text_input("Or paste your image url")
|
|
|
|
| 758 |
|
| 759 |
|
| 760 |
if txt_atachment:
|
| 761 |
+
txtattachment = st.file_uploader("Upload your text file", type=['txt, py, go, java, php, etc'])
|
| 762 |
else:
|
| 763 |
txtattachment = None
|
| 764 |
|
|
|
|
| 767 |
else:
|
| 768 |
csvexcelattachment = None
|
| 769 |
|
| 770 |
+
if pdf_mode:
|
| 771 |
+
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", type=['pdf'])
|
| 772 |
+
if st.button("Submit & Process"):
|
| 773 |
+
with st.spinner("Processing..."):
|
| 774 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 775 |
+
text_chunks = get_text_chunks(raw_text)
|
| 776 |
+
get_vector_store(text_chunks)
|
| 777 |
+
st.success("Done")
|
| 778 |
+
else:
|
| 779 |
+
pdf_docs = None
|
| 780 |
+
|
| 781 |
+
if pdf_docs:
|
| 782 |
+
prompt = st.chat_input("Write your questions according to the pdf")
|
| 783 |
+
if prompt:
|
| 784 |
+
prmt = {'role': 'user', 'parts':[prompt]}
|
| 785 |
+
usertxt = user_input(prompt)
|
| 786 |
+
append_message({'role': 'model', 'parts':usertxt})
|
| 787 |
+
else:
|
| 788 |
+
prompt = st.chat_input("Write your message")
|
| 789 |
|
| 790 |
if prompt:
|
| 791 |
txt = ''
|
requirements.txt
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
google-generativeai
|
| 2 |
pandas
|
| 3 |
numpy
|
| 4 |
-
python-dotenv==1.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
google-generativeai
|
| 2 |
pandas
|
| 3 |
numpy
|
| 4 |
+
python-dotenv==1.0.0
|
| 5 |
+
langchain
|
| 6 |
+
PyPDF2
|
| 7 |
+
chromadb
|
| 8 |
+
faiss-cpu
|
| 9 |
+
langchain_google_genai
|
| 10 |
+
langchain-community
|