hello-llm / lamagemini.py
Bandipreethamreddy's picture
Upload 4 files
da796d2 verified
raw
history blame
No virus
45.9 kB
# from langchain.prompts import PromptTemplate
# from langchain_community.llms import CTransformers
# import streamlit as st
# from PyPDF2 import PdfReader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# import os
# from langchain_google_genai import GoogleGenerativeAIEmbeddings
# import google.generativeai as genai
# from langchain_community.vectorstores import FAISS
# from langchain_google_genai import ChatGoogleGenerativeAI
# from langchain.chains.question_answering import load_qa_chain
# from langchain.prompts import PromptTemplate
# from dotenv import load_dotenv
# load_dotenv()
# os.getenv("GOOGLE_API_KEY")
# genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# def get_pdf_text(pdf_docs):
# text = ""
# for pdf in pdf_docs:
# pdf_reader = PdfReader(pdf)
# for page in pdf_reader.pages:
# text += page.extract_text() or ""
# return text
# def get_text_chunks(text):
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
# chunks = text_splitter.split_text(text)
# return chunks
# def get_vector_store(text_chunks):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
# vector_store.save_local("faiss_index")
# def get_conversational_chain():
# prompt_template = """
# Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
# provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
# Context:\n {context}?\n
# Question: \n{question}\n
# Answer:
# """
# model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
# prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
# chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
# return chain
# def getLLamaresponse(input_text, no_words, blog_style):
# llm = CTransformers(
# model='models/llama-2-7b-chat.ggmlv3.q8_0.bin',
# model_type='llama',
# config={'max_new_tokens': 256, 'temperature': 0.01}
# )
# template = """
# Explain about {input_text} for a {blog_style} blog within {no_words} words and ensure your information is accurate.
# """
# # Use PromptTemplate to format your prompt correctly
# prompt = PromptTemplate(
# input_variables=["input_text", "no_words", "blog_style"],
# template=template
# ).format(input_text=input_text, no_words=no_words, blog_style=blog_style)
# # Ensure the prompt is passed as a list
# response = llm.generate([prompt])
# return response
# def user_input(user_question):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
# docs = new_db.similarity_search(user_question)
# gemini_chain = get_conversational_chain()
# gemini_response = gemini_chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
# initial_response = gemini_response["output_text"]
# if "answer is not available in the context" in initial_response:
# refined_response = getLLamaresponse(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Reponse from LLaMA-2: ", refined_response)
# else:
# refined_response = getLLamaresponse(initial_response, no_words=256, blog_style="detailed")
# st.write("Refined Reply: ", refined_response)
# def main():
# st.set_page_config(page_title="Chat With AUTHOR", page_icon="πŸ“š", layout='centered')
# st.header("Enhance Understanding with Gemini and LLaMA-2 models πŸ€–")
# user_question = st.text_input("Ask a Question from the PDF Files uploaded")
# if user_question:
# user_input(user_question)
# with st.sidebar:
# st.title("Menu:")
# pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
# if st.button("Submit & Process"):
# with st.spinner("Processing..."):
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# st.success("Done")
# if __name__ == "__main__":
# main()
# import os
# import streamlit as st
# from PyPDF2 import PdfReader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI #embeding model used for embeding the tokens
# import google.generativeai as genai
# from langchain_community.vectorstores import FAISS
# from langchain_community.llms import CTransformers
# from langchain.chains.question_answering import load_qa_chain
# from langchain.prompts import PromptTemplate
# from dotenv import load_dotenv
# load_dotenv() # this will load env variables
# google_api_key = os.getenv("GOOGLE_API_KEY")
# if not google_api_key:
# raise ValueError("Google API key not found. Please check your environment variables.")
# genai.configure(api_key=google_api_key)
# def get_pdf_text(pdf_docs):
# text = ""
# for pdf in pdf_docs:
# pdf_reader = PdfReader(pdf)
# for page in pdf_reader.pages:
# text += page.extract_text() or ""
# return text
# # Function to split text into manageable chunks
# def get_text_chunks(text):
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
# #using recursive text spliting we are spliting the text into the chunks.. and we mention its size and chunk over lap..
# return text_splitter.split_text(text)
# # Function to create a vector store for text chunks
# def get_vector_store(text_chunks):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001") #we are using embedding-001 model from googleaiembeding
# vector_store = FAISS.from_texts(text_chunks, embedding=embeddings) #the vector data base is used for search and store mechanism
# vector_store.save_local("faiss_index")
# # facebook ai similarity search and it also stores the data into the vector
# # Function to load the conversational chain
# def get_conversational_chain():
# prompt_template = """
# Please provide a detailed answer based on the provided context. If the necessary information to answer the question is not present in the context, respond with 'The answer is not available in the context'
# Context:
# {context}
# Question:
# {question}
# Answer:
# """
# model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3) #chat google generative ai is used to get the LLM model
# prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) # we will give prompt to the LLm model which has both context and the User question
# x=load_qa_chain(model, chain_type="stuff", prompt=prompt)
# print(x) #load qa will generate the response from the llm model
# return x
# def get_llama_response(input_text, no_words, blog_style):
# llm = CTransformers(
# model='models/llama-2-7b-chat.ggmlv3.q8_0.bin',
# model_type='llama',
# config={'max_new_tokens': 256, 'temperature': 0.01}
# )#we use CT transformers which is langchain library to use LLama2 model in our project
# prompt_template = """
# Given some information of '{input_text}', provide a concise summary suitable for a {blog_style} blog post in approximately {no_words} words. Focus on key aspects and provide accurate information.
# """
# prompt = PromptTemplate(input_variables=["input_text", "no_words", "blog_style"], template=prompt_template)
# formatted_prompt = prompt.format(input_text=input_text, no_words=no_words, blog_style=blog_style)
# print("Formatted Prompt:", formatted_prompt)
# response = llm.generate([formatted_prompt])
# return response
# from sklearn.feature_extraction.text import TfidfVectorizer
# from sklearn.metrics.pairwise import cosine_similarity
# import PyPDF2
# import nltk
# from nltk.corpus import stopwords
# nltk.download('stopwords')
# stop_words = stopwords.words('english')
# custom_stopwords = ["what", "is", "how", "who", "explain", "about","?","please","hey","whatsup","can u explain"]
# stop_words.extend(custom_stopwords)
# def calculate_cosine_similarity(text,user_question):
# vectorizer = TfidfVectorizer(stop_words=stop_words)
# tfidf_matrix=vectorizer.fit_transform([text,user_question])
# cos_similarity=cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
# return cos_similarity
# # def user_input(user_question,raw_text):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
# docs = new_db.similarity_search(user_question)
# gemini_chain = get_conversational_chain()
# gemini_response = gemini_chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
# initial_response = gemini_response["output_text"]
# print(initial_response)
# similarity_score = calculate_cosine_similarity(raw_text, user_question)
# st.write(similarity_score)
# if "The answer is not available in the context" or "The provided context does not contain any information" in initial_response:
# if(similarity_score>0.00125):
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# else:
# st.write("oops I'm sorry, I cannot answer this question based on the provided context.")
# st.write("wait for more info about your question.......llama2 model is ready to give me u the iformation...")
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# else:
# refined_response = get_llama_response(initial_response, no_words=256, blog_style="detailed")
# st.write("Refined Reply:", refined_response)
# def user_input(user_question, raw_text):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
# docs = new_db.similarity_search(user_question)
# gemini_chain=get_conversational_chain()
# gemini_response=gemini_chain({"input_documents":docs, "question": user_question}, return_only_outputs=True)
# initial_response=gemini_response["output_text"]
# similarity_score=calculate_cosine_similarity(raw_text, user_question)
# st.write("Cosine similarity score: ", similarity_score)
# if "The answer is not available in the context" in initial_response or "The provided context does not contain any information" in initial_response:
# if similarity_score > 0.00125:
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# else:
# st.write("I'm sorry, I cannot answer this question based on the provided context.")
# st.write("Waiting for more info about your question... LLaMA-2 model is preparing to provide the information...")
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# else:
# refined_response = get_llama_response(initial_response, no_words=256, blog_style="detailed")
# st.write("Refined Reply:", refined_response)
# def main():
# st.set_page_config(page_title="Chat With AUTHOR", page_icon="πŸ“š", layout='centered')
# st.header("Enhance Understanding with Gemini and LLaMA-2 models πŸ€–")
# user_question = st.text_input("Ask a Question from the PDF Files uploaded")
# with st.sidebar:
# st.title("Menu:")
# pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
# if st.button("Submit & Process"):
# with st.spinner("Processing..."):
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# st.success("Done")
# if user_question:
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# user_input(user_question,raw_text)
# if __name__ == "__main__":
# main()
# import os
# import streamlit as st
# from PyPDF2 import PdfReader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
# import google.generativeai as genai
# from langchain_community.vectorstores import FAISS
# from langchain_community.llms import CTransformers
# from langchain.chains.question_answering import load_qa_chain
# from langchain.prompts import PromptTemplate
# from dotenv import load_dotenv
# import pyttsx3
# load_dotenv() # this will load env variables
# google_api_key = os.getenv("GOOGLE_API_KEY")
# if not google_api_key:
# raise ValueError("Google API key not found. Please check your environment variables.")
# genai.configure(api_key=google_api_key)
# def get_pdf_text(pdf_docs):
# text = ""
# for pdf in pdf_docs:
# pdf_reader = PdfReader(pdf)
# for page in pdf_reader.pages:
# text += page.extract_text() or ""
# return text
# # Function to split text into manageable chunks
# def get_text_chunks(text):
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
# return text_splitter.split_text(text)
# # Function to create a vector store for text chunks
# def get_vector_store(text_chunks):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
# vector_store.save_local("faiss_index")
# # Function to load the conversational chain
# def get_conversational_chain():
# prompt_template = """
# Please provide a detailed answer based on the provided context. If the necessary information to answer the question is not present in the context, respond with 'The answer is not available in the context'
# Context:
# {context}
# Question:
# {question}
# Answer:
# """
# model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
# prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
# return load_qa_chain(model, chain_type="stuff", prompt=prompt)
# def get_llama_response(input_text, no_words, blog_style):
# llm = CTransformers(
# model='models/llama-2-7b-chat.ggmlv3.q8_0.bin',
# model_type='llama',
# config={'max_new_tokens': 256, 'temperature': 0.01}
# )
# prompt_template = """
# Given some information of '{input_text}', provide a concise summary suitable for a {blog_style} blog post in approximately {no_words} words. Focus on key aspects and provide accurate information.
# """
# prompt = PromptTemplate(input_variables=["input_text", "no_words", "blog_style"], template=prompt_template)
# formatted_prompt = prompt.format(input_text=input_text, no_words=no_words, blog_style=blog_style)
# response = llm.generate([formatted_prompt])
# return response
# from sklearn.feature_extraction.text import TfidfVectorizer
# from sklearn.metrics.pairwise import cosine_similarity
# import PyPDF2
# import nltk
# from nltk.corpus import stopwords
# nltk.download('stopwords')
# stop_words = stopwords.words('english')
# custom_stopwords = ["what", "is", "how", "who", "explain", "about", "?", "please", "hey", "whatsup", "can u explain"]
# stop_words.extend(custom_stopwords)
# def calculate_cosine_similarity(text, user_question):
# vectorizer = TfidfVectorizer(stop_words=stop_words)
# tfidf_matrix = vectorizer.fit_transform([text, user_question])
# cos_similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
# return cos_similarity
# def user_input(user_question, raw_text, engine, language):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
# docs = new_db.similarity_search(user_question)
# gemini_chain = get_conversational_chain()
# gemini_response = gemini_chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
# initial_response = gemini_response["output_text"]
# similarity_score = calculate_cosine_similarity(raw_text, user_question)
# st.write("Cosine similarity score: ", similarity_score)
# if "The answer is not available in the context" in initial_response or "The provided context does not contain any information" in initial_response:
# if similarity_score > 0.00125:
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# speak_text(engine, refined_response, language)
# else:
# st.write("I'm sorry, I cannot answer this question based on the provided context.")
# st.write("Waiting for more info about your question... LLaMA-2 model is preparing to provide the information...")
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# speak_text(engine, refined_response, language)
# else:
# refined_response = get_llama_response(initial_response, no_words=256, blog_style="detailed")
# st.write("Refined Reply:", refined_response)
# speak_text(engine, refined_response, language)
# def speak_text(engine, text, language):
# voices = engine.getProperty('voices')
# # Select the appropriate voice based on the language
# for voice in voices:
# if language in voice.languages:
# engine.setProperty('voice', voice.id)
# break
# engine.say(text)
# engine.runAndWait()
# def stop_speech(engine):
# engine.stop()
# def main():
# st.set_page_config(page_title="Chat With AUTHOR", page_icon="πŸ“š", layout='centered')
# st.header("Enhance Understanding with Gemini and LLaMA-2 models πŸ€–")
# engine = pyttsx3.init()
# user_question = st.text_input("Ask a Question from the PDF Files uploaded")
# language = st.selectbox("Select Language", ["en", "es", "fr", "de"]) # Example languages
# with st.sidebar:
# st.title("Menu:")
# pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
# if st.button("Submit & Process"):
# with st.spinner("Processing..."):
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# st.success("Done")
# if user_question:
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# user_input(user_question, raw_text, engine, language)
# if __name__ == "__main__":
# main()
# import os
# import streamlit as st
# from PyPDF2 import PdfReader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
# import google.generativeai as genai
# from langchain_community.vectorstores import FAISS
# from langchain_community.llms import CTransformers
# from langchain.chains.question_answering import load_qa_chain
# from langchain.prompts import PromptTemplate
# from dotenv import load_dotenv
# import pyttsx3
# try:
# import speech_recognition as sr
# except ImportError:
# sr = None
# load_dotenv() # this will load env variables
# google_api_key = os.getenv("GOOGLE_API_KEY")
# if not google_api_key:
# raise ValueError("Google API key not found. Please check your environment variables.")
# genai.configure(api_key=google_api_key)
# def get_pdf_text(pdf_docs):
# text = ""
# for pdf in pdf_docs:
# pdf_reader = PdfReader(pdf)
# for page in pdf_reader.pages:
# text += page.extract_text() or ""
# return text
# # Function to split text into manageable chunks
# def get_text_chunks(text):
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
# return text_splitter.split_text(text)
# # Function to create a vector store for text chunks
# def get_vector_store(text_chunks):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
# vector_store.save_local("faiss_index")
# # Function to load the conversational chain
# def get_conversational_chain():
# prompt_template = """
# Please provide a detailed answer based on the provided context. If the necessary information to answer the question is not present in the context, respond with 'The answer is not available in the context'
# Context:
# {context}
# Question:
# {question}
# Answer:
# """
# model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
# prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
# return load_qa_chain(model, chain_type="stuff", prompt=prompt)
# def get_llama_response(input_text, no_words, blog_style):
# llm = CTransformers(
# model='models/llama-2-7b-chat.ggmlv3.q8_0.bin',
# model_type='llama',
# config={'max_new_tokens': 256, 'temperature': 0.01}
# )
# prompt_template = """
# Given some information of '{input_text}', provide a concise summary suitable for a {blog_style} blog post in approximately {no_words} words. Focus on key aspects and provide accurate information.
# """
# prompt = PromptTemplate(input_variables=["input_text", "no_words", "blog_style"], template=prompt_template)
# formatted_prompt = prompt.format(input_text=input_text, no_words=no_words, blog_style=blog_style)
# response = llm.generate([formatted_prompt])
# return response
# from sklearn.feature_extraction.text import TfidfVectorizer
# from sklearn.metrics.pairwise import cosine_similarity
# import PyPDF2
# import nltk
# from nltk.corpus import stopwords
# nltk.download('stopwords')
# stop_words = stopwords.words('english')
# custom_stopwords = ["what", "is", "how", "who", "explain", "about", "?", "please", "hey", "whatsup", "can u explain"]
# stop_words.extend(custom_stopwords)
# def calculate_cosine_similarity(text, user_question):
# vectorizer = TfidfVectorizer(stop_words=stop_words)
# tfidf_matrix = vectorizer.fit_transform([text, user_question])
# cos_similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
# return cos_similarity
# def user_input(user_question, raw_text, engine, language):
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
# docs = new_db.similarity_search(user_question)
# gemini_chain = get_conversational_chain()
# gemini_response = gemini_chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
# initial_response = gemini_response["output_text"]
# similarity_score = calculate_cosine_similarity(raw_text, user_question)
# st.write("Cosine similarity score: ", similarity_score)
# if "The answer is not available in the context" in initial_response or "The provided context does not contain any information" in initial_response:
# if similarity_score > 0.00125:
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# speak_text(engine, refined_response, language)
# else:
# st.write("I'm sorry, I cannot answer this question based on the provided context.")
# st.write("Waiting for more info about your question... LLaMA-2 model is preparing to provide the information...")
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# speak_text(engine, refined_response, language)
# else:
# refined_response = get_llama_response(initial_response, no_words=256, blog_style="detailed")
# st.write("Refined Reply:", refined_response)
# speak_text(engine, refined_response, language)
# def speak_text(engine, text, language):
# voices = engine.getProperty('voices')
# # Select the appropriate voice based on the language
# for voice in voices:
# if language in voice.languages:
# engine.setProperty('voice', voice.id)
# break
# engine.say(text)
# engine.runAndWait()
# def stop_speech(engine):
# engine.stop()
# def main():
# st.set_page_config(page_title="Chat With AUTHOR", page_icon="πŸ“š", layout='centered')
# st.header("Enhance Understanding with Gemini and LLaMA-2 models πŸ€–")
# engine = pyttsx3.init()
# user_question = st.text_input("Ask a Question from the PDF Files uploaded")
# language = st.selectbox("Select Language", ["en", "es", "fr", "de"]) # Example languages
# with st.sidebar:
# st.title("Menu:")
# pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
# if st.button("Submit & Process"):
# with st.spinner("Processing..."):
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# st.success("Done")
# if sr and st.button("Use Voice Input to Query"):
# recognizer = sr.Recognizer()
# with sr.Microphone() as source:
# # st.write("Listening...")
# audio = recognizer.listen(source)
# if(audio==true){
# st.write("listening")
# }else{
# st.write("")
# }
# try:
# user_question = recognizer.recognize_google(audio)
# st.write(f"You said: {user_question}")
# except sr.UnknownValueError:
# st.write("Sorry, I could not understand your speech.")
# except sr.RequestError:
# st.write("Could not request results; check your network connection.")
# elif not sr:
# st.write("Speech recognition module not available. Please install it to use voice input.")
# if user_question:
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# user_input(user_question, raw_text, engine, language)
# if __name__ == "__main__":
# main()
# import os
# import streamlit as st
# from PyPDF2 import PdfReader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
# import google.generativeai as genai
# from langchain_community.vectorstores import FAISS
# from langchain.chains.question_answering import load_qa_chain
# from langchain.prompts import PromptTemplate
# from dotenv import load_dotenv
# from gtts import gTTS
# import speech_recognition as sr
# import pyttsx3
# import tempfile
# from sklearn.feature_extraction.text import TfidfVectorizer
# from sklearn.metrics.pairwise import cosine_similarity
# import nltk
# from nltk.corpus import stopwords
# from langchain_community.llms import CTransformers
# # Load environment variables
# load_dotenv()
# google_api_key = os.getenv("GOOGLE_API_KEY")
# if not google_api_key:
# raise ValueError("Google API key not found. Please check your environment variables.")
# genai.configure(api_key=google_api_key)
# # Download stopwords
# nltk.download('stopwords')
# stop_words = stopwords.words('english')
# custom_stopwords = ["what", "is", "how", "who", "explain", "about", "?", "please", "hey", "whatsup", "can u explain"]
# stop_words.extend(custom_stopwords)
# def get_pdf_text(pdf_docs):
# text = ""
# for pdf in pdf_docs:
# pdf_reader = PdfReader(pdf)
# for page in pdf_reader.pages:
# text += page.extract_text() or ""
# return text
# def get_text_chunks(text):
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
# return text_splitter.split_text(text)
# def get_vector_store(text_chunks):
# try:
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
# vector_store.save_local("faiss_index")
# except Exception as e:
# st.error(f"Error during embedding: {e}")
# def get_conversational_chain():
# prompt_template = """
# Please provide a detailed answer based on the provided context. If the necessary information to answer the question is not present in the context, respond with 'The answer is not available in the context'
# Context:
# {context}
# Question:
# {question}
# Answer:
# """
# model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
# prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
# return load_qa_chain(model, chain_type="stuff", prompt=prompt)
# def get_llama_response(input_text, no_words, blog_style):
# llm = CTransformers(
# model='models/llama-2-7b-chat.ggmlv3.q8_0.bin',
# model_type='llama',
# config={'max_new_tokens': 256, 'temperature': 0.01}
# )
# prompt_template = """
# Given some information of '{input_text}', provide a concise summary suitable for a {blog_style} blog post in approximately {no_words} words explain me in telugu language i mean cob=nvert it to telugu. Focus on key aspects and provide accurate information.
# """
# prompt=PromptTemplate(input_variables=["input_text", "no_words", "blog_style"], template=prompt_template)
# formatted_prompt = prompt.format(input_text=input_text, no_words=no_words, blog_style=blog_style)
# response = llm.generate([formatted_prompt])
# return response
# def calculate_cosine_similarity(text, user_question):
# vectorizer = TfidfVectorizer(stop_words=list(stop_words))
# tfidf_matrix = vectorizer.fit_transform([text, user_question])
# cos_similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
# return cos_similarity
# def user_input(user_question, raw_text, engine, language):
# try:
# embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
# docs = new_db.similarity_search(user_question)
# gemini_chain = get_conversational_chain()
# gemini_response = gemini_chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
# initial_response = gemini_response["output_text"]
# except Exception as e:
# st.error(f"Error during question answering: {e}")
# initial_response = "The provided context does not contain any information"
# similarity_score = calculate_cosine_similarity(raw_text, user_question)
# st.write("Cosine similarity score: ", similarity_score)
# if "The answer is not available in the context" in initial_response or "The provided context does not contain any information" in initial_response:
# if similarity_score > 0.00125:
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# speak_text(engine, refined_response, language)
# else:
# st.write("I'm sorry, I cannot answer this question based on the provided context.")
# st.write("Waiting for more info about your question... LLaMA-2 model is preparing to provide the information...")
# refined_response = get_llama_response(user_question, no_words=256, blog_style="detailed")
# st.write("Generated Response from LLaMA-2:", refined_response)
# speak_text(engine, refined_response, language)
# else:
# refined_response = get_llama_response(initial_response, no_words=256, blog_style="detailed")
# st.write("Refined Reply:", refined_response)
# speak_text(engine, refined_response, language)
# def speak_text(engine, text, language):
# try:
# if language == 'en':
# # Use pyttsx3 for English
# engine.say(text)
# engine.runAndWait()
# else:
# # Use gTTS for other languages
# with tempfile.NamedTemporaryFile(delete=True) as fp:
# tts = gTTS(text=text, lang=language)
# tts.save(fp.name)
# os.system(f"start {fp.name}")
# except Exception as e:
# st.error(f"Error occurred during text-to-speech: {e}")
# def stop_speech(engine):
# engine.stop()
# def main():
# st.set_page_config(page_title="Chat With AUTHOR", page_icon="πŸ“š", layout='centered')
# st.header("Enhance Understanding with Gemini and LLaMA-2 models πŸ€–")
# engine = pyttsx3.init()
# user_question = st.text_input("Ask a Question from the PDF Files uploaded")
# language = st.selectbox("Select Language", ["en", "es", "fr", "de", "te"]) # Example languages, including Telugu (te)
# if st.button("Use Voice Input to Query"):
# recognizer = sr.Recognizer()
# with sr.Microphone() as source:
# st.write("Listening...")
# audio = recognizer.listen(source)
# st.write("Listening stopped")
# try:
# user_question = recognizer.recognize_google(audio)
# st.write(f"You said: {user_question}")
# except sr.UnknownValueError:
# st.write("Sorry, I could not understand your speech.")
# except sr.RequestError:
# st.write("Could not request results; check your network connection.")
# with st.sidebar:
# st.title("Menu:")
# pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
# if st.button("Submit & Process"):
# with st.spinner("Processing..."):
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# st.success("Done")
# if user_question:
# raw_text = get_pdf_text(pdf_docs)
# text_chunks = get_text_chunks(raw_text)
# get_vector_store(text_chunks)
# user_input(user_question, raw_text, engine, language)
# if __name__ == "__main__":
# main()
import os
import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
import google.generativeai as genai
from langchain_community.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
from gtts import gTTS
import speech_recognition as sr
import pyttsx3
import tempfile
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import nltk
from nltk.corpus import stopwords
from langchain_community.llms import CTransformers
from googletrans import Translator
# Load environment variables
load_dotenv()
google_api_key = os.getenv("GOOGLE_API_KEY")
if not google_api_key:
raise ValueError("Google API key not found. Please check your environment variables.")
genai.configure(api_key=google_api_key)
# Download stopwords
nltk.download('stopwords')
stop_words = stopwords.words('english')
custom_stopwords = ["what", "is", "how", "who", "explain", "about", "?", "please", "hey", "whatsup", "can u explain"]
stop_words.extend(custom_stopwords)
def get_pdf_text(pdf_docs):
text = ""
for pdf in pdf_docs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text() or ""
return text
def get_text_chunks(text):
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
return text_splitter.split_text(text)
def get_vector_store(text_chunks):
try:
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
vector_store.save_local("faiss_index")
except Exception as e:
st.error(f"Error during embedding: {e}")
def get_conversational_chain():
prompt_template = """
Please provide a detailed answer based on the provided context. If the necessary information to answer the question is not present in the context, respond with 'The answer is not available in the context'
Context:
{context}
Question:
{question}
Answer:
"""
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
return load_qa_chain(model, chain_type="stuff", prompt=prompt)
def get_llama_response(input_text, no_words, blog_style, response_language):
llm = CTransformers(
model='models/llama-2-7b-chat.ggmlv3.q8_0.bin',
model_type='llama',
config={'max_new_tokens': 500, 'temperature': 0.01}
)
template = """
Given some information of '{input_text}', provide a concise summary suitable for a {blog_style} blog post in approximately {no_words} words. The total response should be in {response_language} language. Focus on key aspects and provide accurate information.
"""
prompt = PromptTemplate(input_variables=["blog_style", "input_text", 'no_words', 'response_language'],
template=template)
response = llm(prompt.format(input_text=input_text, no_words=no_words, blog_style=blog_style, response_language=response_language))
return response
def calculate_cosine_similarity(text, user_question):
vectorizer = TfidfVectorizer(stop_words=list(stop_words))
tfidf_matrix = vectorizer.fit_transform([text, user_question])
cos_similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
return cos_similarity
def translate_text(text, dest_language):
translator = Translator()
translation = translator.translate(text, dest=dest_language)
return translation.text
def user_input(user_question, raw_text, response_language):
try:
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
docs = new_db.similarity_search(user_question)
gemini_chain = get_conversational_chain()
gemini_response = gemini_chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
initial_response = gemini_response["output_text"]
except Exception as e:
# st.error(f"Error during question answering: {e}")
initial_response = "The provided context does not contain any information"
similarity_score = calculate_cosine_similarity(raw_text, user_question)
st.write("Cosine similarity score: ", similarity_score)
if "The answer is not available in the context" in initial_response or "The provided context does not contain any information" in initial_response:
if similarity_score > 0.00125:
refined_response = get_llama_response(user_question, no_words=500, blog_style="detailed", response_language="english")
else:
refined_response = "I'm sorry, I cannot answer this question based on the provided context."
else:
refined_response = get_llama_response(initial_response, no_words=500, blog_style="detailed", response_language="english")
translated_response = translate_text(refined_response, response_language)
st.write("Generated Response:", translated_response)
st.session_state.refined_response = translated_response
# def speak_text(engine, text, language):
# try:
# if language == 'en':
# # Use pyttsx3 for English
# engine.say(text)
# engine.runAndWait()
# else:
# # Use gTTS for other languages
# with tempfile.NamedTemporaryFile(delete=True) as fp:
# tts = gTTS(text=text, lang=language)
# tts.save(fp.name)
# os.system(f"start {fp.name}")
# except Exception as e:
# st.error(f"Error occurred during text-to-speech: {e}")
# import os
# import tempfile
# import pyttsx3
# from gtts import gTTS
# from pydub import AudioSegment
# from pydub.playback import play
# def speak_text(engine, text, language):
# if language == 'en':
# # Use pyttsx3 for English
# engine.say(text)
# engine.runAndWait()
# else:
# # Use gTTS for other languages
# tts = gTTS(text=text, lang=language)
# with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
# tts.save(fp.name)
# # Use Pydub to play the audio
# audio = AudioSegment.from_file(fp.name)
# play(audio)
# os.remove(fp.name)
# Example usage
# engine = pyttsx3.init()
def stop_speech(engine):
engine.stop()
def main():
st.set_page_config(page_title="Chat With AUTHOR", page_icon="πŸ“š", layout='centered')
st.header("Enhance Understanding with Gemini and LLaMA-2 models πŸ€–")
engine = pyttsx3.init()
user_question = st.text_input("Ask a Question from the PDF Files uploaded")
if st.button("πŸŽ™"):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
st.write("Listening...")
audio = recognizer.listen(source)
st.write("Listening stopped")
try:
user_question = recognizer.recognize_google(audio)
st.write(f"You said: {user_question}")
except sr.UnknownValueError:
st.write("Sorry, I could not understand your speech.")
except sr.RequestError:
st.write("Could not request results; check your network connection.")
response_language = st.selectbox("Select Response Language", ["en", "es", "fr", "de", "te"]) # Example languages, including Telugu (te)
with st.sidebar:
st.title("Menu:")
pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
if st.button("Submit & Process"):
with st.spinner("Processing..."):
raw_text = get_pdf_text(pdf_docs)
text_chunks = get_text_chunks(raw_text)
get_vector_store(text_chunks)
st.success("Done")
if user_question:
raw_text = get_pdf_text(pdf_docs)
text_chunks = get_text_chunks(raw_text)
get_vector_store(text_chunks)
user_input(user_question, raw_text, response_language)
# if "refined_response" in st.session_state:
# if st.button("Speak"):
# speak_text(engine, st.session_state.translated_response, response_language)
if __name__ == "__main__":
main()