alex buz commited on
Commit
3df36f0
1 Parent(s): 13dd234
.streamlit/secrets.toml ADDED
@@ -0,0 +1 @@
 
 
1
+
__pycache__/whisper_stt.cpython-311.pyc CHANGED
Binary files a/__pycache__/whisper_stt.cpython-311.pyc and b/__pycache__/whisper_stt.cpython-311.pyc differ
 
app copy.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from st_pages import Page, show_pages
3
+ from openai import OpenAI
4
+
5
+ from whisper_stt import whisper_stt
6
+
7
+
8
+
9
+
10
+ # Set page configuration
11
+ st.set_page_config(layout="wide")
12
+ show_pages([Page("app.py", "Home", "🏠")])
13
+
14
+ # Initialize session state variables
15
+ if 'paused' not in st.session_state:
16
+ st.session_state.paused = False
17
+ if 'question_text' not in st.session_state:
18
+ st.session_state.question_text = ""
19
+ if 'submitted' not in st.session_state:
20
+ st.session_state.submitted = False
21
+ if 'response_content' not in st.session_state:
22
+ st.session_state.response_content = ""
23
+ if 'stopped' not in st.session_state:
24
+ st.session_state.stopped = False
25
+ if 'function_call_count' not in st.session_state:
26
+ st.session_state.function_call_count = 0
27
+ if 'transcribed_text' not in st.session_state:
28
+ st.session_state.transcribed_text = ""
29
+ if 'last_processed_text' not in st.session_state:
30
+ st.session_state.last_processed_text = ""
31
+ if 'headers' not in st.session_state:
32
+ st.session_state.headers = []
33
+
34
+ def create_anchor_link(text):
35
+ if text is None:
36
+ return ""
37
+ return f"<a href='#{text.strip().lower().replace(' ', '-').replace(',', '').replace('.', '').replace(chr(39), '-')}'>{text}</a>"
38
+
39
+ def on_stop():
40
+ st.session_state.stopped = True
41
+
42
+ col0 = st.columns(1)[0]
43
+ placeholder = col0.empty() # Define the placeholder
44
+ def handle_enter(key):
45
+ if key == "ctrl+enter":
46
+ new_question = st.session_state.question_input
47
+ print(f"handle_enter called. new_question: '{new_question}'")
48
+ print(f"session state: {st.session_state}")
49
+ with st.sidebar:
50
+ api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
51
+
52
+ col1, col2 = st.columns(2)
53
+
54
+ with col1:
55
+ # Call whisper_stt without a callback
56
+ transcribed_text = whisper_stt(
57
+ openai_api_key=api_key,
58
+ language='en'
59
+ )
60
+ if transcribed_text:
61
+ st.session_state.question_text = transcribed_text
62
+
63
+ # Check if new transcription is available
64
+ if transcribed_text and transcribed_text != st.session_state.transcribed_text:
65
+ st.session_state.transcribed_text = transcribed_text
66
+ st.session_state.question_text = transcribed_text
67
+ st.session_state.submitted = True
68
+
69
+ if st.session_state.question_text:
70
+ st.markdown(create_anchor_link(st.session_state.question_text), unsafe_allow_html=True)
71
+
72
+ if 'question_input' in st.session_state and st.session_state.question_input:
73
+ st.markdown(create_anchor_link(st.session_state.question_input), unsafe_allow_html=True)
74
+ with col2:
75
+ st.button(label='Stop', on_click=on_stop)
76
+
77
+ # Create an input for the question and use new_question directly
78
+ new_question = st.text_area("Question",
79
+ value=st.session_state.question_text or "",
80
+ height=150,
81
+ key="question_input",
82
+ on_change=handle_enter,
83
+ args=("ctrl+enter",)
84
+ )
85
+ print(f"After text_area, new_question: '{new_question}'")
86
+ # Check if new_question has changed and is not empty
87
+ if new_question and new_question != st.session_state.question_text:
88
+ st.session_state.question_text = new_question
89
+ st.session_state.submitted = True
90
+
91
+ st.markdown("## Navigation")
92
+ for header in st.session_state.headers:
93
+ st.markdown(create_anchor_link(header), unsafe_allow_html=True)
94
+
95
+ if st.session_state.question_text and not api_key:
96
+ st.info("Please add your OpenAI API key to continue.")
97
+ st.stop()
98
+
99
+ if st.session_state.submitted and not st.session_state.stopped:
100
+ st.session_state.headers.append(st.session_state.question_text)
101
+
102
+ if st.session_state.function_call_count == 0:
103
+ header = f'## {st.session_state.question_text}'
104
+ anchor = create_anchor_link(st.session_state.question_text)
105
+ st.session_state.response_content += f'{header}\n\n{anchor}\n\n'
106
+ else:
107
+ header = f'\n\n---\n{st.session_state.question_text}\n---\n\n'
108
+ anchor = create_anchor_link(st.session_state.question_text)
109
+ st.session_state.response_content += f'{header}{anchor}\n\n'
110
+
111
+ st.session_state.function_call_count += 1
112
+ client = OpenAI(api_key=api_key)
113
+ st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
114
+
115
+ response = client.chat.completions.create(
116
+ model="gpt-4o",
117
+ messages=st.session_state.messages,
118
+ stream=True
119
+ )
120
+
121
+ for chunk in response:
122
+ if st.session_state.stopped:
123
+ st.session_state.stopped = False
124
+ st.session_state.submitted = False
125
+ break
126
+ else:
127
+ if chunk and chunk.choices[0].delta.content:
128
+ st.session_state.response_content += chunk.choices[0].delta.content
129
+ placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
130
+
131
+ st.session_state.submitted = False
132
+ st.session_state.stopped = False
133
+
134
+ placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
135
+ st.session_state.stopped = False
app.py CHANGED
@@ -1,68 +1,8 @@
1
  import streamlit as st
2
  from st_pages import Page, show_pages
3
  from openai import OpenAI
4
- from streamlit_mic_recorder import mic_recorder
5
- import io
6
-
7
-
8
-
9
-
10
- def whisper_stt(openai_api_key, start_prompt="Start recording", stop_prompt="Stop recording", just_once=False,
11
- use_container_width=False, language=None, callback=None, args=(), kwargs=None, key=None):
12
-
13
-
14
-
15
- if not '_last_speech_to_text_transcript_id' in st.session_state:
16
- st.session_state._last_speech_to_text_transcript_id = 0
17
- if not '_last_speech_to_text_transcript' in st.session_state:
18
- st.session_state._last_speech_to_text_transcript = None
19
- if key and not key + '_output' in st.session_state:
20
- st.session_state[key + '_output'] = None
21
- audio = mic_recorder(start_prompt=start_prompt, stop_prompt=stop_prompt, just_once=just_once,
22
- use_container_width=use_container_width,format="webm", key=key)
23
- new_output = False
24
- if audio is None:
25
- output = None
26
- else:
27
- if openai_api_key:
28
- if not 'openai_client' in st.session_state:
29
- #assert openai_api_key, openai_api_key
30
- st.session_state.openai_client = OpenAI(api_key=openai_api_key)
31
-
32
- id = audio['id']
33
- new_output = (id > st.session_state._last_speech_to_text_transcript_id)
34
- if new_output:
35
- output = None
36
- st.session_state._last_speech_to_text_transcript_id = id
37
- audio_bio = io.BytesIO(audio['bytes'])
38
- audio_bio.name = 'audio.webm'
39
- success = False
40
- err = 0
41
- while not success and err < 3: # Retry up to 3 times in case of OpenAI server error.
42
- try:
43
- transcript = st.session_state.openai_client.audio.transcriptions.create(
44
- model="whisper-1",
45
- file=audio_bio,
46
- language=language
47
- )
48
- except Exception as e:
49
- print(str(e)) # log the exception in the terminal
50
- err += 1
51
- else:
52
- success = True
53
- output = transcript.text
54
- st.session_state._last_speech_to_text_transcript = output
55
- elif not just_once:
56
- output = st.session_state._last_speech_to_text_transcript
57
- else:
58
- output = None
59
- else:
60
- output = None
61
- if key:
62
- st.session_state[key + '_output'] = output
63
- if new_output and callback:
64
- callback(*args, **(kwargs or {}))
65
- return output
66
 
67
  # Set page configuration
68
  st.set_page_config(layout="wide")
@@ -91,18 +31,22 @@ if 'headers' not in st.session_state:
91
  def create_anchor_link(text):
92
  if text is None:
93
  return ""
94
- return f"<a href='#{text.strip().lower().replace(' ', '-').replace(',', '').replace('.', '').replace(chr(39), '-')}'>{text}</a>"
 
 
95
 
96
  def on_stop():
97
  st.session_state.stopped = True
98
 
99
  col0 = st.columns(1)[0]
100
  placeholder = col0.empty() # Define the placeholder
 
101
  def handle_enter(key):
102
  if key == "ctrl+enter":
103
  new_question = st.session_state.question_input
104
  print(f"handle_enter called. new_question: '{new_question}'")
105
  print(f"session state: {st.session_state}")
 
106
  with st.sidebar:
107
  api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
108
 
@@ -125,6 +69,8 @@ with st.sidebar:
125
 
126
  if st.session_state.question_text:
127
  st.markdown(create_anchor_link(st.session_state.question_text), unsafe_allow_html=True)
 
 
128
 
129
  if 'question_input' in st.session_state and st.session_state.question_input:
130
  st.markdown(create_anchor_link(st.session_state.question_input), unsafe_allow_html=True)
@@ -189,4 +135,27 @@ if st.session_state.submitted and not st.session_state.stopped:
189
  st.session_state.stopped = False
190
 
191
  placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
192
- st.session_state.stopped = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from st_pages import Page, show_pages
3
  from openai import OpenAI
4
+ import urllib.parse
5
+ from whisper_stt import whisper_stt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Set page configuration
8
  st.set_page_config(layout="wide")
 
31
  def create_anchor_link(text):
32
  if text is None:
33
  return ""
34
+ anchor = text.strip().lower().replace(' ', '-').replace(',', '').replace('.', '').replace(chr(39), '-')
35
+ anchor = urllib.parse.quote(anchor)
36
+ return f"<a href='#{anchor}'>{text}</a>"
37
 
38
  def on_stop():
39
  st.session_state.stopped = True
40
 
41
  col0 = st.columns(1)[0]
42
  placeholder = col0.empty() # Define the placeholder
43
+
44
  def handle_enter(key):
45
  if key == "ctrl+enter":
46
  new_question = st.session_state.question_input
47
  print(f"handle_enter called. new_question: '{new_question}'")
48
  print(f"session state: {st.session_state}")
49
+
50
  with st.sidebar:
51
  api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
52
 
 
69
 
70
  if st.session_state.question_text:
71
  st.markdown(create_anchor_link(st.session_state.question_text), unsafe_allow_html=True)
72
+ # Debug output
73
+ st.write("Debug: Generated anchor link:", create_anchor_link(st.session_state.question_text))
74
 
75
  if 'question_input' in st.session_state and st.session_state.question_input:
76
  st.markdown(create_anchor_link(st.session_state.question_input), unsafe_allow_html=True)
 
135
  st.session_state.stopped = False
136
 
137
  placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
138
+ st.session_state.stopped = False
139
+
140
+ # Add anchor points for each header
141
+ for header in st.session_state.headers:
142
+ anchor = header.strip().lower().replace(' ', '-').replace(',', '').replace('.', '').replace(chr(39), '-')
143
+ anchor = urllib.parse.quote(anchor)
144
+ st.markdown(f"<div id='{anchor}'></div>", unsafe_allow_html=True)
145
+
146
+ # Add JavaScript for smooth scrolling and anchor navigation
147
+ st.markdown(
148
+ """
149
+ <script>
150
+ function navigateToAnchor(anchor) {
151
+ const element = document.getElementById(anchor);
152
+ if (element) {
153
+ element.scrollIntoView({behavior: 'smooth'});
154
+ } else {
155
+ console.error('Anchor not found:', anchor);
156
+ }
157
+ }
158
+ </script>
159
+ """,
160
+ unsafe_allow_html=True
161
+ )
whisper_stt.py CHANGED
@@ -7,9 +7,9 @@ import os
7
 
8
  def whisper_stt(openai_api_key, start_prompt="Start recording", stop_prompt="Stop recording", just_once=False,
9
  use_container_width=False, language=None, callback=None, args=(), kwargs=None, key=None):
10
- if not 'openai_client' in st.session_state:
11
- assert openai_api_key, openai_api_key
12
- st.session_state.openai_client = OpenAI(api_key=openai_api_key)
13
  if not '_last_speech_to_text_transcript_id' in st.session_state:
14
  st.session_state._last_speech_to_text_transcript_id = 0
15
  if not '_last_speech_to_text_transcript' in st.session_state:
@@ -22,34 +22,40 @@ def whisper_stt(openai_api_key, start_prompt="Start recording", stop_prompt="Sto
22
  if audio is None:
23
  output = None
24
  else:
25
- id = audio['id']
26
- new_output = (id > st.session_state._last_speech_to_text_transcript_id)
27
- if new_output:
28
- output = None
29
- st.session_state._last_speech_to_text_transcript_id = id
30
- audio_bio = io.BytesIO(audio['bytes'])
31
- audio_bio.name = 'audio.webm'
32
- success = False
33
- err = 0
34
- while not success and err < 3: # Retry up to 3 times in case of OpenAI server error.
35
- try:
36
- transcript = st.session_state.openai_client.audio.transcriptions.create(
37
- model="whisper-1",
38
- file=audio_bio,
39
- language=language
40
- )
41
- except Exception as e:
42
- print(str(e)) # log the exception in the terminal
43
- err += 1
44
- else:
45
- success = True
46
- output = transcript.text
47
- st.session_state._last_speech_to_text_transcript = output
48
- elif not just_once:
49
- output = st.session_state._last_speech_to_text_transcript
 
 
 
 
 
 
 
50
  else:
51
  output = None
52
-
53
  if key:
54
  st.session_state[key + '_output'] = output
55
  if new_output and callback:
 
7
 
8
  def whisper_stt(openai_api_key, start_prompt="Start recording", stop_prompt="Stop recording", just_once=False,
9
  use_container_width=False, language=None, callback=None, args=(), kwargs=None, key=None):
10
+
11
+
12
+
13
  if not '_last_speech_to_text_transcript_id' in st.session_state:
14
  st.session_state._last_speech_to_text_transcript_id = 0
15
  if not '_last_speech_to_text_transcript' in st.session_state:
 
22
  if audio is None:
23
  output = None
24
  else:
25
+ if openai_api_key:
26
+ if not 'openai_client' in st.session_state:
27
+ #assert openai_api_key, openai_api_key
28
+ st.session_state.openai_client = OpenAI(api_key=openai_api_key)
29
+
30
+ id = audio['id']
31
+ new_output = (id > st.session_state._last_speech_to_text_transcript_id)
32
+ if new_output:
33
+ output = None
34
+ st.session_state._last_speech_to_text_transcript_id = id
35
+ audio_bio = io.BytesIO(audio['bytes'])
36
+ audio_bio.name = 'audio.webm'
37
+ success = False
38
+ err = 0
39
+ while not success and err < 3: # Retry up to 3 times in case of OpenAI server error.
40
+ try:
41
+ transcript = st.session_state.openai_client.audio.transcriptions.create(
42
+ model="whisper-1",
43
+ file=audio_bio,
44
+ language=language
45
+ )
46
+ except Exception as e:
47
+ print(str(e)) # log the exception in the terminal
48
+ err += 1
49
+ else:
50
+ success = True
51
+ output = transcript.text
52
+ st.session_state._last_speech_to_text_transcript = output
53
+ elif not just_once:
54
+ output = st.session_state._last_speech_to_text_transcript
55
+ else:
56
+ output = None
57
  else:
58
  output = None
 
59
  if key:
60
  st.session_state[key + '_output'] = output
61
  if new_output and callback: