parvezalmuqtadir commited on
Commit
c35538c
1 Parent(s): 11d6b2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -37
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import os
2
- import tempfile
3
  import streamlit as st
4
  from streamlit_chat import message
5
  from pdfquery import PDFQuery
6
  from dotenv import load_dotenv
7
  from gtts import gTTS
8
  import io
9
- import base64
10
 
11
  # Load environment variables from .env file
12
  load_dotenv()
@@ -26,40 +24,28 @@ def generate_tts_audio(text):
26
  audio_bytes.seek(0)
27
  return audio_bytes
28
 
29
- from datetime import datetime
30
-
31
  def play_audio(audio_bytes):
32
  audio_bytes.seek(0) # Ensure the pointer is at the beginning of the IO stream
33
  st.audio(audio_bytes, format='audio/mp3', start_time=0)
34
 
35
-
36
-
37
  def process_input():
38
  if st.session_state["user_input"] and len(st.session_state["user_input"].strip()) > 0:
39
  user_text = st.session_state["user_input"].strip()
40
  with st.session_state["thinking_spinner"], st.spinner("Thinking..."):
41
  query_text = st.session_state["pdfquery"].ask(user_text)
42
-
43
- # Generate and play audio for the response with autoplay
44
  audio_response = generate_tts_audio(query_text)
45
  play_audio(audio_response)
46
-
47
  st.session_state["messages"].append((user_text, True))
48
  st.session_state["messages"].append((query_text, False))
49
 
50
- def read_and_save_file():
51
- st.session_state["pdfquery"].forget() # to reset the knowledge base
52
- st.session_state["messages"] = []
53
- st.session_state["user_input"] = ""
54
-
55
- for file in st.session_state["file_uploader"]:
56
- with tempfile.NamedTemporaryFile(delete=False) as tf:
57
- tf.write(file.getbuffer())
58
- file_path = tf.name
59
-
60
- with st.session_state["ingestion_spinner"], st.spinner(f"Ingesting {file.name}"):
61
- st.session_state["pdfquery"].ingest(file_path)
62
- os.remove(file_path)
63
 
64
  def is_openai_api_key_set() -> bool:
65
  return len(st.session_state["OPENAI_API_KEY"]) > 0
@@ -70,27 +56,14 @@ def main():
70
  st.session_state["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
71
  if is_openai_api_key_set():
72
  st.session_state["pdfquery"] = PDFQuery(st.session_state["OPENAI_API_KEY"])
 
73
  else:
74
  st.session_state["pdfquery"] = None
75
 
76
  st.header("Wise Chat Labs - Your Ultimate Chatbot")
77
 
78
- st.subheader("Upload document")
79
- st.file_uploader(
80
- "Upload document",
81
- type=["pdf"],
82
- key="file_uploader",
83
- on_change=read_and_save_file,
84
- label_visibility="collapsed",
85
- accept_multiple_files=True,
86
- disabled=not is_openai_api_key_set(),
87
- )
88
-
89
- st.session_state["ingestion_spinner"] = st.empty()
90
-
91
  display_messages()
92
  st.text_input("Message", key="user_input", disabled=not is_openai_api_key_set(), on_change=process_input)
93
 
94
-
95
  if __name__ == "__main__":
96
- main()
 
1
  import os
 
2
  import streamlit as st
3
  from streamlit_chat import message
4
  from pdfquery import PDFQuery
5
  from dotenv import load_dotenv
6
  from gtts import gTTS
7
  import io
 
8
 
9
  # Load environment variables from .env file
10
  load_dotenv()
 
24
  audio_bytes.seek(0)
25
  return audio_bytes
26
 
 
 
27
  def play_audio(audio_bytes):
28
  audio_bytes.seek(0) # Ensure the pointer is at the beginning of the IO stream
29
  st.audio(audio_bytes, format='audio/mp3', start_time=0)
30
 
 
 
31
  def process_input():
32
  if st.session_state["user_input"] and len(st.session_state["user_input"].strip()) > 0:
33
  user_text = st.session_state["user_input"].strip()
34
  with st.session_state["thinking_spinner"], st.spinner("Thinking..."):
35
  query_text = st.session_state["pdfquery"].ask(user_text)
36
+
 
37
  audio_response = generate_tts_audio(query_text)
38
  play_audio(audio_response)
39
+
40
  st.session_state["messages"].append((user_text, True))
41
  st.session_state["messages"].append((query_text, False))
42
 
43
+ def load_and_ingest_document():
44
+ file_path = 'osha.pdf' # PDF file is assumed to be in the same directory as this script
45
+ if os.path.exists(file_path):
46
+ st.session_state["pdfquery"].ingest(file_path)
47
+ else:
48
+ st.error("PDF file not found. Please ensure 'data.pdf' is in the root directory.")
 
 
 
 
 
 
 
49
 
50
  def is_openai_api_key_set() -> bool:
51
  return len(st.session_state["OPENAI_API_KEY"]) > 0
 
56
  st.session_state["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
57
  if is_openai_api_key_set():
58
  st.session_state["pdfquery"] = PDFQuery(st.session_state["OPENAI_API_KEY"])
59
+ load_and_ingest_document()
60
  else:
61
  st.session_state["pdfquery"] = None
62
 
63
  st.header("Wise Chat Labs - Your Ultimate Chatbot")
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  display_messages()
66
  st.text_input("Message", key="user_input", disabled=not is_openai_api_key_set(), on_change=process_input)
67
 
 
68
  if __name__ == "__main__":
69
+ main()