Spaces:
Running
Running
alex buz
commited on
Commit
·
cbad54b
1
Parent(s):
73ad602
final
Browse files- __pycache__/whisper_stt.cpython-311.pyc +0 -0
- app.py +131 -0
- push.bat +3 -0
- requirements.txt +94 -0
- whisper_stt.py +56 -0
__pycache__/whisper_stt.cpython-311.pyc
ADDED
Binary file (2.71 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from whisper_stt import whisper_stt
|
3 |
+
from st_pages import Page, show_pages
|
4 |
+
from openai import OpenAI
|
5 |
+
|
6 |
+
# Set page configuration
|
7 |
+
st.set_page_config(layout="wide")
|
8 |
+
show_pages([Page("app.py", "Home", "🏠")])
|
9 |
+
|
10 |
+
# Initialize session state variables
|
11 |
+
if 'paused' not in st.session_state:
|
12 |
+
st.session_state.paused = False
|
13 |
+
if 'question_text' not in st.session_state:
|
14 |
+
st.session_state.question_text = ""
|
15 |
+
if 'submitted' not in st.session_state:
|
16 |
+
st.session_state.submitted = False
|
17 |
+
if 'response_content' not in st.session_state:
|
18 |
+
st.session_state.response_content = ""
|
19 |
+
if 'stopped' not in st.session_state:
|
20 |
+
st.session_state.stopped = False
|
21 |
+
if 'function_call_count' not in st.session_state:
|
22 |
+
st.session_state.function_call_count = 0
|
23 |
+
if 'transcribed_text' not in st.session_state:
|
24 |
+
st.session_state.transcribed_text = ""
|
25 |
+
if 'last_processed_text' not in st.session_state:
|
26 |
+
st.session_state.last_processed_text = ""
|
27 |
+
if 'headers' not in st.session_state:
|
28 |
+
st.session_state.headers = []
|
29 |
+
|
30 |
+
def create_anchor_link(text):
|
31 |
+
if text is None:
|
32 |
+
return ""
|
33 |
+
return f"<a href='#{text.strip().lower().replace(' ', '-').replace(',', '').replace('.', '').replace(chr(39), '-')}'>{text}</a>"
|
34 |
+
|
35 |
+
def on_stop():
|
36 |
+
st.session_state.stopped = True
|
37 |
+
|
38 |
+
col0 = st.columns(1)[0]
|
39 |
+
placeholder = col0.empty() # Define the placeholder
|
40 |
+
def handle_enter(key):
|
41 |
+
if key == "ctrl+enter":
|
42 |
+
new_question = st.session_state.question_input
|
43 |
+
print(f"handle_enter called. new_question: '{new_question}'")
|
44 |
+
print(f"session state: {st.session_state}")
|
45 |
+
with st.sidebar:
|
46 |
+
api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
|
47 |
+
|
48 |
+
col1, col2 = st.columns(2)
|
49 |
+
|
50 |
+
with col1:
|
51 |
+
# Call whisper_stt without a callback
|
52 |
+
transcribed_text = whisper_stt(
|
53 |
+
openai_api_key=api_key,
|
54 |
+
language='en'
|
55 |
+
)
|
56 |
+
if transcribed_text:
|
57 |
+
st.session_state.question_text = transcribed_text
|
58 |
+
|
59 |
+
# Check if new transcription is available
|
60 |
+
if transcribed_text and transcribed_text != st.session_state.transcribed_text:
|
61 |
+
st.session_state.transcribed_text = transcribed_text
|
62 |
+
st.session_state.question_text = transcribed_text
|
63 |
+
st.session_state.submitted = True
|
64 |
+
|
65 |
+
if st.session_state.question_text:
|
66 |
+
st.markdown(create_anchor_link(st.session_state.question_text), unsafe_allow_html=True)
|
67 |
+
|
68 |
+
if 'question_input' in st.session_state and st.session_state.question_input:
|
69 |
+
st.markdown(create_anchor_link(st.session_state.question_input), unsafe_allow_html=True)
|
70 |
+
with col2:
|
71 |
+
st.button(label='Stop', on_click=on_stop)
|
72 |
+
|
73 |
+
# Create an input for the question and use new_question directly
|
74 |
+
new_question = st.text_area("Question",
|
75 |
+
value=st.session_state.question_text or "",
|
76 |
+
height=150,
|
77 |
+
key="question_input",
|
78 |
+
on_change=handle_enter,
|
79 |
+
args=("ctrl+enter",)
|
80 |
+
)
|
81 |
+
print(f"After text_area, new_question: '{new_question}'")
|
82 |
+
# Check if new_question has changed and is not empty
|
83 |
+
if new_question and new_question != st.session_state.question_text:
|
84 |
+
st.session_state.question_text = new_question
|
85 |
+
st.session_state.submitted = True
|
86 |
+
|
87 |
+
st.markdown("## Navigation")
|
88 |
+
for header in st.session_state.headers:
|
89 |
+
st.markdown(create_anchor_link(header), unsafe_allow_html=True)
|
90 |
+
|
91 |
+
if st.session_state.question_text and not api_key:
|
92 |
+
st.info("Please add your OpenAI API key to continue.")
|
93 |
+
st.stop()
|
94 |
+
|
95 |
+
if st.session_state.submitted and not st.session_state.stopped:
|
96 |
+
st.session_state.headers.append(st.session_state.question_text)
|
97 |
+
|
98 |
+
if st.session_state.function_call_count == 0:
|
99 |
+
header = f'## {st.session_state.question_text}'
|
100 |
+
anchor = create_anchor_link(st.session_state.question_text)
|
101 |
+
st.session_state.response_content += f'{header}\n\n{anchor}\n\n'
|
102 |
+
else:
|
103 |
+
header = f'\n\n---\n{st.session_state.question_text}\n---\n\n'
|
104 |
+
anchor = create_anchor_link(st.session_state.question_text)
|
105 |
+
st.session_state.response_content += f'{header}{anchor}\n\n'
|
106 |
+
|
107 |
+
st.session_state.function_call_count += 1
|
108 |
+
client = OpenAI(api_key=api_key)
|
109 |
+
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
110 |
+
|
111 |
+
response = client.chat.completions.create(
|
112 |
+
model="gpt-4o",
|
113 |
+
messages=st.session_state.messages,
|
114 |
+
stream=True
|
115 |
+
)
|
116 |
+
|
117 |
+
for chunk in response:
|
118 |
+
if st.session_state.stopped:
|
119 |
+
st.session_state.stopped = False
|
120 |
+
st.session_state.submitted = False
|
121 |
+
break
|
122 |
+
else:
|
123 |
+
if chunk and chunk.choices[0].delta.content:
|
124 |
+
st.session_state.response_content += chunk.choices[0].delta.content
|
125 |
+
placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
|
126 |
+
|
127 |
+
st.session_state.submitted = False
|
128 |
+
st.session_state.stopped = False
|
129 |
+
|
130 |
+
placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
|
131 |
+
st.session_state.stopped = False
|
push.bat
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
git add .
|
2 |
+
git commit -m "%1"
|
3 |
+
git push -u origin
|
requirements.txt
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiohttp==3.9.5
|
2 |
+
aiosignal==1.3.1
|
3 |
+
altair==5.3.0
|
4 |
+
annotated-types==0.7.0
|
5 |
+
anthropic==0.31.2
|
6 |
+
anyio==4.4.0
|
7 |
+
attrs==23.2.0
|
8 |
+
blinker==1.8.2
|
9 |
+
bokeh==3.5.0
|
10 |
+
cachetools==5.4.0
|
11 |
+
certifi==2024.7.4
|
12 |
+
charset-normalizer==3.3.2
|
13 |
+
click==8.1.7
|
14 |
+
colorama==0.4.6
|
15 |
+
contourpy==1.2.1
|
16 |
+
dataclasses-json==0.6.7
|
17 |
+
distro==1.9.0
|
18 |
+
duckduckgo_search==6.2.1
|
19 |
+
filelock==3.15.4
|
20 |
+
frozenlist==1.4.1
|
21 |
+
fsspec==2024.6.1
|
22 |
+
gitdb==4.0.11
|
23 |
+
GitPython==3.1.43
|
24 |
+
greenlet==3.0.3
|
25 |
+
h11==0.14.0
|
26 |
+
httpcore==1.0.5
|
27 |
+
httpx==0.27.0
|
28 |
+
huggingface-hub==0.23.5
|
29 |
+
idna==3.7
|
30 |
+
Jinja2==3.1.4
|
31 |
+
jiter==0.5.0
|
32 |
+
jsonpatch==1.33
|
33 |
+
jsonpointer==3.0.0
|
34 |
+
jsonschema==4.23.0
|
35 |
+
jsonschema-specifications==2023.12.1
|
36 |
+
langchain==0.2.9
|
37 |
+
langchain-community==0.2.7
|
38 |
+
langchain-core==0.2.21
|
39 |
+
langchain-text-splitters==0.2.2
|
40 |
+
langsmith==0.1.90
|
41 |
+
loguru==0.7.2
|
42 |
+
markdown-it-py==3.0.0
|
43 |
+
MarkupSafe==2.1.5
|
44 |
+
marshmallow==3.21.3
|
45 |
+
mdurl==0.1.2
|
46 |
+
multidict==6.0.5
|
47 |
+
mypy-extensions==1.0.0
|
48 |
+
numpy==1.26.4
|
49 |
+
openai==1.35.14
|
50 |
+
orjson==3.10.6
|
51 |
+
packaging==24.1
|
52 |
+
pandas==2.2.2
|
53 |
+
pillow==10.4.0
|
54 |
+
protobuf==5.27.2
|
55 |
+
pyarrow==17.0.0
|
56 |
+
pydantic==2.8.2
|
57 |
+
pydantic_core==2.20.1
|
58 |
+
pydeck==0.9.1
|
59 |
+
Pygments==2.18.0
|
60 |
+
pyreqwest_impersonate==0.5.0
|
61 |
+
python-dateutil==2.9.0.post0
|
62 |
+
pytz==2024.1
|
63 |
+
PyYAML==6.0.1
|
64 |
+
referencing==0.35.1
|
65 |
+
requests==2.32.3
|
66 |
+
rich==13.7.1
|
67 |
+
rpds-py==0.19.0
|
68 |
+
shellingham==1.5.4
|
69 |
+
six==1.16.0
|
70 |
+
smmap==5.0.1
|
71 |
+
sniffio==1.3.1
|
72 |
+
SpeechRecognition==3.10.4
|
73 |
+
SQLAlchemy==2.0.31
|
74 |
+
st-pages==0.5.0
|
75 |
+
streamlit==1.36.0
|
76 |
+
streamlit-bokeh-events==0.1.2
|
77 |
+
streamlit-feedback==0.1.3
|
78 |
+
streamlit_mic_recorder==0.0.8
|
79 |
+
tenacity==8.5.0
|
80 |
+
tokenizers==0.19.1
|
81 |
+
toml==0.10.2
|
82 |
+
toolz==0.12.1
|
83 |
+
tornado==6.4.1
|
84 |
+
tqdm==4.66.4
|
85 |
+
trubrics==1.6.2
|
86 |
+
typer==0.12.3
|
87 |
+
typing-inspect==0.9.0
|
88 |
+
typing_extensions==4.12.2
|
89 |
+
tzdata==2024.1
|
90 |
+
urllib3==2.2.2
|
91 |
+
watchdog==4.0.1
|
92 |
+
win32-setctime==1.1.0
|
93 |
+
xyzservices==2024.6.0
|
94 |
+
yarl==1.9.4
|
whisper_stt.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from streamlit_mic_recorder import mic_recorder
|
2 |
+
import streamlit as st
|
3 |
+
import io
|
4 |
+
from openai import OpenAI
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
def whisper_stt(openai_api_key=None, 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 |
+
st.session_state.openai_client = OpenAI(api_key=openai_api_key or os.getenv('OPENAI_API_KEY'))
|
12 |
+
if not '_last_speech_to_text_transcript_id' in st.session_state:
|
13 |
+
st.session_state._last_speech_to_text_transcript_id = 0
|
14 |
+
if not '_last_speech_to_text_transcript' in st.session_state:
|
15 |
+
st.session_state._last_speech_to_text_transcript = None
|
16 |
+
if key and not key + '_output' in st.session_state:
|
17 |
+
st.session_state[key + '_output'] = None
|
18 |
+
audio = mic_recorder(start_prompt=start_prompt, stop_prompt=stop_prompt, just_once=just_once,
|
19 |
+
use_container_width=use_container_width,format="webm", key=key)
|
20 |
+
new_output = False
|
21 |
+
if audio is None:
|
22 |
+
output = None
|
23 |
+
else:
|
24 |
+
id = audio['id']
|
25 |
+
new_output = (id > st.session_state._last_speech_to_text_transcript_id)
|
26 |
+
if new_output:
|
27 |
+
output = None
|
28 |
+
st.session_state._last_speech_to_text_transcript_id = id
|
29 |
+
audio_bio = io.BytesIO(audio['bytes'])
|
30 |
+
audio_bio.name = 'audio.webm'
|
31 |
+
success = False
|
32 |
+
err = 0
|
33 |
+
while not success and err < 3: # Retry up to 3 times in case of OpenAI server error.
|
34 |
+
try:
|
35 |
+
transcript = st.session_state.openai_client.audio.transcriptions.create(
|
36 |
+
model="whisper-1",
|
37 |
+
file=audio_bio,
|
38 |
+
language=language
|
39 |
+
)
|
40 |
+
except Exception as e:
|
41 |
+
print(str(e)) # log the exception in the terminal
|
42 |
+
err += 1
|
43 |
+
else:
|
44 |
+
success = True
|
45 |
+
output = transcript.text
|
46 |
+
st.session_state._last_speech_to_text_transcript = output
|
47 |
+
elif not just_once:
|
48 |
+
output = st.session_state._last_speech_to_text_transcript
|
49 |
+
else:
|
50 |
+
output = None
|
51 |
+
|
52 |
+
if key:
|
53 |
+
st.session_state[key + '_output'] = output
|
54 |
+
if new_output and callback:
|
55 |
+
callback(*args, **(kwargs or {}))
|
56 |
+
return output
|