Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
from PyPDF2 import PdfReader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
import os
|
@@ -9,140 +12,90 @@ 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 |
load_dotenv()
|
15 |
os.getenv("GOOGLE_API_KEY")
|
16 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
17 |
|
18 |
-
|
19 |
-
def transcribe_audio(audio_file):
|
20 |
-
model = whisper.load_model("small")
|
21 |
-
audio = whisper.load_audio(audio_file)
|
22 |
-
audio = whisper.pad_or_trim(audio)
|
23 |
-
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
24 |
-
prediction = model.transcribe(mel, language="en", fp16=False)
|
25 |
-
return prediction['text']
|
26 |
-
|
27 |
-
|
28 |
def get_pdf_text(pdf_docs):
|
29 |
-
text=""
|
30 |
for pdf in pdf_docs:
|
31 |
-
pdf_reader= PdfReader(pdf)
|
32 |
for page in pdf_reader.pages:
|
33 |
-
text+= page.extract_text()
|
34 |
-
return
|
35 |
-
|
36 |
-
|
37 |
|
38 |
def get_text_chunks(text):
|
39 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
40 |
chunks = text_splitter.split_text(text)
|
41 |
return chunks
|
42 |
|
43 |
-
|
44 |
def get_vector_store(text_chunks):
|
45 |
-
embeddings = GoogleGenerativeAIEmbeddings(model
|
46 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
47 |
vector_store.save_local("faiss_index")
|
48 |
|
49 |
-
|
50 |
def get_conversational_chain():
|
51 |
-
|
52 |
prompt_template = """
|
53 |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
54 |
-
provided context just say,
|
55 |
Context:\n {context}?\n
|
56 |
Question: \n{question}\n
|
57 |
Answer:
|
58 |
"""
|
59 |
-
|
60 |
-
|
61 |
-
temperature=0.3)
|
62 |
-
|
63 |
-
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
64 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
65 |
-
|
66 |
return chain
|
67 |
|
68 |
-
|
69 |
-
|
70 |
def user_input(user_question):
|
71 |
-
embeddings = GoogleGenerativeAIEmbeddings(model
|
72 |
-
|
73 |
new_db = FAISS.load_local("faiss_index", embeddings)
|
74 |
docs = new_db.similarity_search(user_question)
|
75 |
-
|
76 |
chain = get_conversational_chain()
|
77 |
-
|
78 |
-
|
79 |
-
response = chain(
|
80 |
-
{"input_documents":docs, "question": user_question}
|
81 |
-
, return_only_outputs=True)
|
82 |
-
|
83 |
-
print(response)
|
84 |
st.write("Reply: ", response["output_text"])
|
85 |
|
86 |
-
import streamlit.components.v1 as components
|
87 |
-
|
88 |
-
def voice_recorder(key=None):
|
89 |
-
component_html = """
|
90 |
-
<script src="https://cdn.jsdelivr.net/npm/@ffmpeg/ffmpeg"></script>
|
91 |
-
<button id="recordButton">Record</button>
|
92 |
-
<button id="stopButton" disabled>Stop</button>
|
93 |
-
<audio id="audioPlayback" controls></audio>
|
94 |
-
<script>
|
95 |
-
let recordButton = document.getElementById("recordButton");
|
96 |
-
let stopButton = document.getElementById("stopButton");
|
97 |
-
let audioPlayback = document.getElementById("audioPlayback");
|
98 |
-
|
99 |
-
let recorder;
|
100 |
-
let audioData;
|
101 |
-
|
102 |
-
recordButton.onclick = () => {
|
103 |
-
navigator.mediaDevices.getUserMedia({ audio: true })
|
104 |
-
.then(stream => {
|
105 |
-
const mediaRecorder = new MediaRecorder(stream);
|
106 |
-
mediaRecorder.start();
|
107 |
-
const audioChunks = [];
|
108 |
-
mediaRecorder.addEventListener("dataavailable", event => {
|
109 |
-
audioChunks.push(event.data);
|
110 |
-
});
|
111 |
-
recorder = mediaRecorder;
|
112 |
-
stopButton.disabled = false;
|
113 |
-
recordButton.disabled = true;
|
114 |
-
});
|
115 |
-
};
|
116 |
-
|
117 |
-
stopButton.onclick = () => {
|
118 |
-
recorder.stop();
|
119 |
-
recorder.addEventListener("stop", () => {
|
120 |
-
const audioBlob = new Blob(audioChunks);
|
121 |
-
const audioUrl = URL.createObjectURL(audioBlob);
|
122 |
-
audioPlayback.src = audioUrl;
|
123 |
-
const reader = new FileReader();
|
124 |
-
reader.readAsDataURL(audioBlob);
|
125 |
-
reader.onload = () => {
|
126 |
-
window.parent.postMessage({ audioData: reader.result }, '*');
|
127 |
-
};
|
128 |
-
});
|
129 |
-
stopButton.disabled = true;
|
130 |
-
recordButton.disabled = false;
|
131 |
-
};
|
132 |
-
</script>
|
133 |
-
"""
|
134 |
-
audio_base64 = components.html(component_html, height=150, key=key)
|
135 |
-
return audio_base64
|
136 |
-
|
137 |
def main():
|
138 |
st.set_page_config("Chat PDF")
|
139 |
st.header("Chat with PDF using Gemini💁")
|
140 |
|
141 |
-
#
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
st.write(f"Transcribed Question: {user_question}")
|
147 |
user_input(user_question)
|
148 |
|
@@ -157,4 +110,4 @@ def main():
|
|
157 |
st.success("Done")
|
158 |
|
159 |
if __name__ == "__main__":
|
160 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from bokeh.models.widgets import Button
|
3 |
+
from bokeh.models import CustomJS
|
4 |
+
from streamlit_bokeh_events import streamlit_bokeh_events
|
5 |
from PyPDF2 import PdfReader
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
import os
|
|
|
12 |
from langchain.chains.question_answering import load_qa_chain
|
13 |
from langchain.prompts import PromptTemplate
|
14 |
from dotenv import load_dotenv
|
|
|
15 |
|
16 |
+
# Environment and API setup
|
17 |
load_dotenv()
|
18 |
os.getenv("GOOGLE_API_KEY")
|
19 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def get_pdf_text(pdf_docs):
|
22 |
+
text = ""
|
23 |
for pdf in pdf_docs:
|
24 |
+
pdf_reader = PdfReader(pdf)
|
25 |
for page in pdf_reader.pages:
|
26 |
+
text += page.extract_text()
|
27 |
+
return text
|
|
|
|
|
28 |
|
29 |
def get_text_chunks(text):
|
30 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
31 |
chunks = text_splitter.split_text(text)
|
32 |
return chunks
|
33 |
|
|
|
34 |
def get_vector_store(text_chunks):
|
35 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
36 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
37 |
vector_store.save_local("faiss_index")
|
38 |
|
|
|
39 |
def get_conversational_chain():
|
|
|
40 |
prompt_template = """
|
41 |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
42 |
+
provided context just say, 'answer is not available in the context', don't provide the wrong answer\n\n
|
43 |
Context:\n {context}?\n
|
44 |
Question: \n{question}\n
|
45 |
Answer:
|
46 |
"""
|
47 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
48 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
|
|
|
|
|
|
49 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
|
|
50 |
return chain
|
51 |
|
|
|
|
|
52 |
def user_input(user_question):
|
53 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
|
|
54 |
new_db = FAISS.load_local("faiss_index", embeddings)
|
55 |
docs = new_db.similarity_search(user_question)
|
|
|
56 |
chain = get_conversational_chain()
|
57 |
+
response = chain({"input_documents":docs, "question": user_question}, return_only_outputs=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
st.write("Reply: ", response["output_text"])
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def main():
|
61 |
st.set_page_config("Chat PDF")
|
62 |
st.header("Chat with PDF using Gemini💁")
|
63 |
|
64 |
+
# Bokeh button to activate speech recognition
|
65 |
+
stt_button = Button(label="Speak", width=100)
|
66 |
+
stt_button.js_on_event("button_click", CustomJS(code="""
|
67 |
+
var recognition = new webkitSpeechRecognition();
|
68 |
+
recognition.continuous = true;
|
69 |
+
recognition.interimResults = true;
|
70 |
+
|
71 |
+
recognition.onresult = function (e) {
|
72 |
+
var value = "";
|
73 |
+
for (var i = e.resultIndex; i < e.results.length; ++i) {
|
74 |
+
if (e.results[i].isFinal) {
|
75 |
+
value += e.results[i][0].transcript;
|
76 |
+
}
|
77 |
+
}
|
78 |
+
if (value != "") {
|
79 |
+
document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
|
80 |
+
}
|
81 |
+
}
|
82 |
+
recognition.start();
|
83 |
+
"""))
|
84 |
+
|
85 |
+
# Streamlit Bokeh event for receiving transcribed text
|
86 |
+
result = streamlit_bokeh_events(
|
87 |
+
stt_button,
|
88 |
+
events="GET_TEXT",
|
89 |
+
key="listen",
|
90 |
+
refresh_on_update=False,
|
91 |
+
override_height=75,
|
92 |
+
debounce_time=0
|
93 |
+
)
|
94 |
+
|
95 |
+
# Process the transcribed text
|
96 |
+
if result:
|
97 |
+
if "GET_TEXT" in result:
|
98 |
+
user_question = result.get("GET_TEXT")
|
99 |
st.write(f"Transcribed Question: {user_question}")
|
100 |
user_input(user_question)
|
101 |
|
|
|
110 |
st.success("Done")
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
+
main()
|