goketech commited on
Commit
d3512a2
·
verified ·
1 Parent(s): 68d208e

Update pages/🧠_sickle_cell_chatbot.py

Browse files
Files changed (1) hide show
  1. pages/🧠_sickle_cell_chatbot.py +44 -1
pages/🧠_sickle_cell_chatbot.py CHANGED
@@ -18,6 +18,29 @@ st.set_page_config(page_title="Sickle cell chatbot", page_icon="🧠")
18
  st.header('Sickle cell chatbot')
19
  st.write('Enhancing Chatbot Interactions through sickle cell training')
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  class ContextChatbot:
22
 
23
  def __init__(self):
@@ -89,9 +112,29 @@ class ContextChatbot:
89
 
90
  @utils.enable_chat_history
91
  def main(self):
 
 
92
  chain = self.setup_chain()
93
  user_query = st.chat_input(placeholder="Ask me anything!")
94
- if user_query:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  utils.display_msg(user_query, 'user')
96
  with st.chat_message("assistant"):
97
  st_cb = StreamHandler(st.empty())
 
18
  st.header('Sickle cell chatbot')
19
  st.write('Enhancing Chatbot Interactions through sickle cell training')
20
 
21
+ from audio_recorder_streamlit import audio_recorder
22
+
23
+ # Subheading for audio input button
24
+ st.sidebar.header("Use Audio Input 🔊")
25
+ with st.sidebar:
26
+ audio_bytes = audio_recorder(
27
+ pause_threshold=3.0,
28
+ text="Start Talking 👉",
29
+ recording_color="#e8b62c",
30
+ neutral_color="#6aa36f",
31
+ icon_name="microphone",
32
+ icon_size="3x",)
33
+
34
+
35
+ def transcribe_audio(audio_bytes):
36
+ openai.api_key = os.getenv('OPENAI_API_KEY')
37
+ response = openai.audio.transcriptions.create(
38
+ model="whisper-1",
39
+ file=("temp." + "wav", audio_bytes, "audio/wav"),
40
+ )
41
+
42
+ return response.text
43
+
44
  class ContextChatbot:
45
 
46
  def __init__(self):
 
112
 
113
  @utils.enable_chat_history
114
  def main(self):
115
+
116
+
117
  chain = self.setup_chain()
118
  user_query = st.chat_input(placeholder="Ask me anything!")
119
+ if audio_bytes:
120
+ transcript = transcribe_audio(audio_bytes)
121
+ if transcript:
122
+ question = transcript
123
+ utils.display_msg(question, 'user')
124
+ with st.chat_message("assistant"):
125
+ st_cb = StreamHandler(st.empty())
126
+ result = chain.invoke(
127
+ {"question":question},
128
+ {"callbacks": [st_cb]}
129
+ )
130
+ #response = ""
131
+ #for source in result['source_documents']:
132
+ #response += source.metadata['source']
133
+ #response += "Content: " + source.page_content
134
+ response = result["answer"]
135
+ st.session_state.messages.append({"role": "assistant", "content": response})
136
+
137
+ elif user_query:
138
  utils.display_msg(user_query, 'user')
139
  with st.chat_message("assistant"):
140
  st_cb = StreamHandler(st.empty())