Spaces:
Sleeping
Sleeping
barghavani
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ from langchain.chains.question_answering import load_qa_chain
|
|
13 |
from langchain.prompts import PromptTemplate
|
14 |
from dotenv import load_dotenv
|
15 |
|
16 |
-
#
|
17 |
load_dotenv()
|
18 |
os.getenv("GOOGLE_API_KEY")
|
19 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
@@ -61,53 +61,61 @@ def main():
|
|
61 |
st.set_page_config("Chat PDF")
|
62 |
st.header("Chat with PDF using Gemini💁")
|
63 |
|
64 |
-
#
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
}
|
77 |
}
|
78 |
-
|
79 |
-
|
80 |
}
|
81 |
-
|
82 |
-
|
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 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
text_chunks = get_text_chunks(raw_text)
|
109 |
-
get_vector_store(text_chunks)
|
110 |
-
st.success("Done")
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
-
main()
|
|
|
13 |
from langchain.prompts import PromptTemplate
|
14 |
from dotenv import load_dotenv
|
15 |
|
16 |
+
# Load environment variables and configure API
|
17 |
load_dotenv()
|
18 |
os.getenv("GOOGLE_API_KEY")
|
19 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
|
|
61 |
st.set_page_config("Chat PDF")
|
62 |
st.header("Chat with PDF using Gemini💁")
|
63 |
|
64 |
+
# Organizing layout to separate PDF upload and STT
|
65 |
+
col1, col2 = st.columns(2)
|
66 |
+
|
67 |
+
with col1:
|
68 |
+
st.subheader("Upload PDF")
|
69 |
+
pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
|
70 |
+
if st.button("Process PDF"):
|
71 |
+
with st.spinner("Processing PDF..."):
|
72 |
+
raw_text = get_pdf_text(pdf_docs)
|
73 |
+
text_chunks = get_text_chunks(raw_text)
|
74 |
+
get_vector_store(text_chunks)
|
75 |
+
st.success("PDF processing complete.")
|
76 |
+
|
77 |
+
with col2:
|
78 |
+
st.subheader("Voice Question")
|
79 |
+
# Bokeh button to activate speech recognition
|
80 |
+
stt_button = Button(label="Speak", width=100)
|
81 |
+
stt_button.js_on_event("button_click", CustomJS(code="""
|
82 |
+
var recognition = new webkitSpeechRecognition();
|
83 |
+
recognition.continuous = true;
|
84 |
+
recognition.interimResults = true;
|
85 |
+
|
86 |
+
recognition.onresult = function (e) {
|
87 |
+
var value = "";
|
88 |
+
for (var i = e.resultIndex; i < e.results.length; ++i) {
|
89 |
+
if (e.results[i].isFinal) {
|
90 |
+
value += e.results[i][0].transcript;
|
91 |
+
}
|
92 |
+
}
|
93 |
+
if (value != "") {
|
94 |
+
document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
|
95 |
}
|
96 |
}
|
97 |
+
recognition.onerror = function (event) {
|
98 |
+
console.error('Speech recognition error', event);
|
99 |
}
|
100 |
+
recognition.start();
|
101 |
+
"""))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
# Streamlit Bokeh event for receiving transcribed text
|
104 |
+
result = streamlit_bokeh_events(
|
105 |
+
stt_button,
|
106 |
+
events="GET_TEXT",
|
107 |
+
key="listen",
|
108 |
+
refresh_on_update=False,
|
109 |
+
override_height=75,
|
110 |
+
debounce_time=0
|
111 |
+
)
|
112 |
|
113 |
+
# Process the transcribed text
|
114 |
+
if result:
|
115 |
+
if "GET_TEXT" in result:
|
116 |
+
user_question = result.get("GET_TEXT")
|
117 |
+
st.write(f"Transcribed Question: {user_question}")
|
118 |
+
user_input(user_question)
|
|
|
|
|
|
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
+
main()
|