barghavani commited on
Commit
c8b10d8
·
verified ·
1 Parent(s): 8db7f1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -44
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
- # Environment and API setup
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
- # 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
 
102
- with st.sidebar:
103
- st.title("Menu:")
104
- pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
105
- if st.button("Submit & Process"):
106
- with st.spinner("Processing..."):
107
- raw_text = get_pdf_text(pdf_docs)
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()