Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,25 +9,10 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
9 |
from langchain.chains.question_answering import load_qa_chain
|
10 |
from langchain.prompts import PromptTemplate
|
11 |
from dotenv import load_dotenv
|
12 |
-
import whisper
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
load_dotenv()
|
17 |
-
os.getenv("GOOGLE_API_KEY")
|
18 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
19 |
|
20 |
|
21 |
-
# Load the Whisper model
|
22 |
-
model_1 = whisper.load_model("large")
|
23 |
-
|
24 |
-
def speech_to_text(audio_path):
|
25 |
-
# Load and decode the audio file
|
26 |
-
result = model_1.transcribe(audio_path, language="en",fp16=False)
|
27 |
-
return result['text']
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
def get_pdf_text(pdf_docs):
|
32 |
text=""
|
33 |
for pdf in pdf_docs:
|
@@ -61,7 +46,7 @@ def get_conversational_chain():
|
|
61 |
"""
|
62 |
|
63 |
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
64 |
-
temperature=0.
|
65 |
|
66 |
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
67 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
@@ -73,7 +58,7 @@ def get_conversational_chain():
|
|
73 |
def user_input(user_question):
|
74 |
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
75 |
|
76 |
-
new_db = FAISS.load_local("faiss_index", embeddings)
|
77 |
docs = new_db.similarity_search(user_question)
|
78 |
|
79 |
chain = get_conversational_chain()
|
@@ -87,36 +72,28 @@ def user_input(user_question):
|
|
87 |
st.write("Reply: ", response["output_text"])
|
88 |
|
89 |
|
90 |
-
# Constants
|
91 |
-
DURATION = 5 # seconds
|
92 |
-
SAMPLERATE = 44100 # Hz
|
93 |
|
94 |
|
95 |
def main():
|
96 |
st.set_page_config("Chat PDF")
|
97 |
st.header("QnA with Multiple PDF files💁")
|
98 |
|
|
|
|
|
|
|
|
|
|
|
99 |
with st.sidebar:
|
100 |
st.title("Menu:")
|
101 |
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
102 |
-
audio_file = st.file_uploader("Upload your voice query", type=['wav', 'mp3', 'ogg'])
|
103 |
if st.button("Submit & Process"):
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
# Handle audio processing
|
112 |
-
audio_path = audio_file.name
|
113 |
-
with open(audio_path, "wb") as f:
|
114 |
-
f.write(audio_file.getbuffer())
|
115 |
-
user_question = speech_to_text(audio_path)
|
116 |
-
st.write(f"Your question: {user_question}")
|
117 |
-
user_input(user_question)
|
118 |
-
|
119 |
-
st.success("Done")
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
-
main()
|
|
|
9 |
from langchain.chains.question_answering import load_qa_chain
|
10 |
from langchain.prompts import PromptTemplate
|
11 |
from dotenv import load_dotenv
|
|
|
12 |
|
|
|
|
|
|
|
|
|
13 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def get_pdf_text(pdf_docs):
|
17 |
text=""
|
18 |
for pdf in pdf_docs:
|
|
|
46 |
"""
|
47 |
|
48 |
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
49 |
+
temperature=0.1)
|
50 |
|
51 |
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
52 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
|
|
58 |
def user_input(user_question):
|
59 |
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
60 |
|
61 |
+
new_db = FAISS.load_local("faiss_index", embeddings,allow_dangerous_deserialization= True)
|
62 |
docs = new_db.similarity_search(user_question)
|
63 |
|
64 |
chain = get_conversational_chain()
|
|
|
72 |
st.write("Reply: ", response["output_text"])
|
73 |
|
74 |
|
|
|
|
|
|
|
75 |
|
76 |
|
77 |
def main():
|
78 |
st.set_page_config("Chat PDF")
|
79 |
st.header("QnA with Multiple PDF files💁")
|
80 |
|
81 |
+
user_question = st.text_input("Ask a Question from the PDF Files")
|
82 |
+
|
83 |
+
if user_question:
|
84 |
+
user_input(user_question)
|
85 |
+
|
86 |
with st.sidebar:
|
87 |
st.title("Menu:")
|
88 |
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
|
|
89 |
if st.button("Submit & Process"):
|
90 |
+
with st.spinner("Processing..."):
|
91 |
+
raw_text = get_pdf_text(pdf_docs)
|
92 |
+
text_chunks = get_text_chunks(raw_text)
|
93 |
+
get_vector_store(text_chunks)
|
94 |
+
st.success("Done")
|
95 |
+
|
96 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
+
main()
|