Spaces:
Sleeping
Sleeping
alex buz commited on
Commit ·
3774cbd
1
Parent(s): f10d421
final
Browse files- .gitignore +1 -0
- app copy 2.py +133 -0
- app copy 3.py +135 -0
- app copy 4.py +135 -0
- app copy 5.py +137 -0
- app copy 6.py +112 -0
- app copy 7.py +119 -0
- app copy final.py +136 -0
- app.py +59 -80
- bottom.py +31 -0
- expand.py +10 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.streamlit
|
app copy 2.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
| 9 |
+
show_pages([Page("app.py", "Home", "🏠")])
|
| 10 |
+
|
| 11 |
+
# Initialize session state variables
|
| 12 |
+
if 'paused' not in st.session_state:
|
| 13 |
+
st.session_state.paused = False
|
| 14 |
+
if 'question_text' not in st.session_state:
|
| 15 |
+
st.session_state.question_text = ""
|
| 16 |
+
if 'submitted' not in st.session_state:
|
| 17 |
+
st.session_state.submitted = False
|
| 18 |
+
if 'response_content' not in st.session_state:
|
| 19 |
+
st.session_state.response_content = []
|
| 20 |
+
if 'stopped' not in st.session_state:
|
| 21 |
+
st.session_state.stopped = False
|
| 22 |
+
if 'function_call_count' not in st.session_state:
|
| 23 |
+
st.session_state.function_call_count = 0
|
| 24 |
+
if 'transcribed_text' not in st.session_state:
|
| 25 |
+
st.session_state.transcribed_text = ""
|
| 26 |
+
if 'last_processed_text' not in st.session_state:
|
| 27 |
+
st.session_state.last_processed_text = ""
|
| 28 |
+
if 'questions' not in st.session_state:
|
| 29 |
+
st.session_state.questions = []
|
| 30 |
+
|
| 31 |
+
def on_stop():
|
| 32 |
+
st.session_state.stopped = True
|
| 33 |
+
|
| 34 |
+
def handle_enter(key):
|
| 35 |
+
if key == "ctrl+enter":
|
| 36 |
+
new_question = st.session_state.question_input
|
| 37 |
+
print(f"handle_enter called. new_question: '{new_question}'")
|
| 38 |
+
print(f"session state: {st.session_state}")
|
| 39 |
+
|
| 40 |
+
with st.sidebar:
|
| 41 |
+
api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
|
| 42 |
+
|
| 43 |
+
col1, col2 = st.columns(2)
|
| 44 |
+
|
| 45 |
+
with col1:
|
| 46 |
+
# Call whisper_stt without a callback
|
| 47 |
+
transcribed_text = whisper_stt(
|
| 48 |
+
openai_api_key=api_key,
|
| 49 |
+
language='en'
|
| 50 |
+
)
|
| 51 |
+
if transcribed_text:
|
| 52 |
+
st.session_state.question_text = transcribed_text
|
| 53 |
+
|
| 54 |
+
# Check if new transcription is available
|
| 55 |
+
if transcribed_text and transcribed_text != st.session_state.transcribed_text:
|
| 56 |
+
st.session_state.transcribed_text = transcribed_text
|
| 57 |
+
st.session_state.question_text = transcribed_text
|
| 58 |
+
st.session_state.submitted = True
|
| 59 |
+
|
| 60 |
+
if st.session_state.question_text:
|
| 61 |
+
st.write("Current Question:", st.session_state.question_text)
|
| 62 |
+
|
| 63 |
+
if 'question_input' in st.session_state and st.session_state.question_input:
|
| 64 |
+
st.write("Current Question Input:", st.session_state.question_input)
|
| 65 |
+
|
| 66 |
+
with col2:
|
| 67 |
+
st.button(label='Stop', on_click=on_stop)
|
| 68 |
+
|
| 69 |
+
# Create an input for the question and use new_question directly
|
| 70 |
+
new_question = st.text_area("Question",
|
| 71 |
+
value=st.session_state.question_text or "",
|
| 72 |
+
height=150,
|
| 73 |
+
key="question_input",
|
| 74 |
+
on_change=handle_enter,
|
| 75 |
+
args=("ctrl+enter",)
|
| 76 |
+
)
|
| 77 |
+
print(f"After text_area, new_question: '{new_question}'")
|
| 78 |
+
# Check if new_question has changed and is not empty
|
| 79 |
+
if new_question and new_question != st.session_state.question_text:
|
| 80 |
+
st.session_state.question_text = new_question
|
| 81 |
+
st.session_state.submitted = True
|
| 82 |
+
|
| 83 |
+
st.markdown("## Navigation")
|
| 84 |
+
for i, q in enumerate(st.session_state.questions):
|
| 85 |
+
st.write(f"Q{i+1}: {q['question']}")
|
| 86 |
+
|
| 87 |
+
if st.session_state.question_text and not api_key:
|
| 88 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 89 |
+
st.stop()
|
| 90 |
+
|
| 91 |
+
if st.session_state.submitted and not st.session_state.stopped:
|
| 92 |
+
st.session_state.questions.append({'question': st.session_state.question_text, 'response': ''})
|
| 93 |
+
|
| 94 |
+
client = OpenAI(api_key=api_key)
|
| 95 |
+
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
| 96 |
+
|
| 97 |
+
response = client.chat.completions.create(
|
| 98 |
+
model="gpt-4o",
|
| 99 |
+
messages=st.session_state.messages,
|
| 100 |
+
stream=True
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Create a placeholder for the streamed response
|
| 104 |
+
response_placeholder = st.empty()
|
| 105 |
+
response_text = ""
|
| 106 |
+
|
| 107 |
+
for chunk in response:
|
| 108 |
+
if st.session_state.stopped:
|
| 109 |
+
st.session_state.stopped = False
|
| 110 |
+
st.session_state.submitted = False
|
| 111 |
+
break
|
| 112 |
+
else:
|
| 113 |
+
if chunk and chunk.choices[0].delta.content:
|
| 114 |
+
response_text += chunk.choices[0].delta.content
|
| 115 |
+
# Update the placeholder with the current response text
|
| 116 |
+
response_placeholder.markdown(f"**Answer:** {response_text}")
|
| 117 |
+
|
| 118 |
+
if response_text:
|
| 119 |
+
st.session_state.questions[-1]['response'] = response_text
|
| 120 |
+
|
| 121 |
+
st.session_state.submitted = False
|
| 122 |
+
st.session_state.stopped = False
|
| 123 |
+
|
| 124 |
+
# Display previous questions and answers
|
| 125 |
+
for i, q in enumerate(st.session_state.questions[:-1]): # Exclude the last question
|
| 126 |
+
st.markdown(f"### Question {i+1}: {q['question']}")
|
| 127 |
+
st.markdown(f"**Answer:** {q['response']}")
|
| 128 |
+
|
| 129 |
+
# Display the latest question separately
|
| 130 |
+
if st.session_state.questions:
|
| 131 |
+
latest_q = st.session_state.questions[-1]
|
| 132 |
+
st.markdown(f"### Question {len(st.session_state.questions)}: {latest_q['question']}")
|
| 133 |
+
# The answer for the latest question is already being streamed in the placeholder
|
app copy 3.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 copy 4.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 |
+
from whisper_stt import whisper_stt
|
| 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 |
+
expander = st.expander("Answer", expanded=True)
|
| 38 |
+
with expander:
|
| 39 |
+
col0 = st.columns(1)[0]
|
| 40 |
+
placeholder = col0.empty() # Define the placeholder
|
| 41 |
+
|
| 42 |
+
def handle_enter(key):
|
| 43 |
+
if key == "ctrl+enter":
|
| 44 |
+
new_question = st.session_state.question_input
|
| 45 |
+
print(f"handle_enter called. new_question: '{new_question}'")
|
| 46 |
+
print(f"session state: {st.session_state}")
|
| 47 |
+
|
| 48 |
+
with st.sidebar:
|
| 49 |
+
api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
|
| 50 |
+
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
|
| 53 |
+
with col1:
|
| 54 |
+
# Call whisper_stt without a callback
|
| 55 |
+
transcribed_text = whisper_stt(
|
| 56 |
+
openai_api_key=api_key,
|
| 57 |
+
language='en'
|
| 58 |
+
)
|
| 59 |
+
if transcribed_text:
|
| 60 |
+
st.session_state.question_text = transcribed_text
|
| 61 |
+
expander.header = 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 |
+
expander.header = "Completed Answer"
|
| 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 copy 5.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from st_pages import Page, show_pages
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from whisper_stt import whisper_stt
|
| 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 |
+
def handle_enter(key):
|
| 39 |
+
if key == "ctrl+enter":
|
| 40 |
+
new_question = st.session_state.question_input
|
| 41 |
+
print(f"handle_enter called. new_question: '{new_question}'")
|
| 42 |
+
print(f"session state: {st.session_state}")
|
| 43 |
+
|
| 44 |
+
with st.sidebar:
|
| 45 |
+
api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
|
| 46 |
+
|
| 47 |
+
col1, col2 = st.columns(2)
|
| 48 |
+
|
| 49 |
+
with col1:
|
| 50 |
+
# Call whisper_stt without a callback
|
| 51 |
+
transcribed_text = whisper_stt(
|
| 52 |
+
openai_api_key=api_key,
|
| 53 |
+
language='en'
|
| 54 |
+
)
|
| 55 |
+
if transcribed_text:
|
| 56 |
+
st.session_state.question_text = transcribed_text
|
| 57 |
+
|
| 58 |
+
# Check if new transcription is available
|
| 59 |
+
if transcribed_text and transcribed_text != st.session_state.transcribed_text:
|
| 60 |
+
st.session_state.transcribed_text = transcribed_text
|
| 61 |
+
st.session_state.question_text = transcribed_text
|
| 62 |
+
st.session_state.submitted = True
|
| 63 |
+
|
| 64 |
+
if st.session_state.question_text:
|
| 65 |
+
st.markdown(create_anchor_link(st.session_state.question_text), unsafe_allow_html=True)
|
| 66 |
+
|
| 67 |
+
if 'question_input' in st.session_state and st.session_state.question_input:
|
| 68 |
+
st.markdown(create_anchor_link(st.session_state.question_input), unsafe_allow_html=True)
|
| 69 |
+
with col2:
|
| 70 |
+
st.button(label='Stop', on_click=on_stop)
|
| 71 |
+
|
| 72 |
+
# Create an input for the question and use new_question directly
|
| 73 |
+
new_question = st.text_area("Question",
|
| 74 |
+
value=st.session_state.question_text or "",
|
| 75 |
+
height=150,
|
| 76 |
+
key="question_input",
|
| 77 |
+
on_change=handle_enter,
|
| 78 |
+
args=("ctrl+enter",)
|
| 79 |
+
)
|
| 80 |
+
print(f"After text_area, new_question: '{new_question}'")
|
| 81 |
+
# Check if new_question has changed and is not empty
|
| 82 |
+
if new_question and new_question != st.session_state.question_text:
|
| 83 |
+
st.session_state.question_text = new_question
|
| 84 |
+
st.session_state.submitted = True
|
| 85 |
+
|
| 86 |
+
st.markdown("## Navigation")
|
| 87 |
+
for header in st.session_state.headers:
|
| 88 |
+
st.markdown(create_anchor_link(header), unsafe_allow_html=True)
|
| 89 |
+
|
| 90 |
+
if st.session_state.question_text and not api_key:
|
| 91 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 92 |
+
st.stop()
|
| 93 |
+
|
| 94 |
+
# Define the placeholder outside the expander block to avoid NameError
|
| 95 |
+
#placeholder = st.empty()
|
| 96 |
+
|
| 97 |
+
if st.session_state.submitted and not st.session_state.stopped:
|
| 98 |
+
st.session_state.headers.append(st.session_state.question_text)
|
| 99 |
+
|
| 100 |
+
if st.session_state.function_call_count == 0:
|
| 101 |
+
header = f'## {st.session_state.question_text}'
|
| 102 |
+
anchor = create_anchor_link(st.session_state.question_text)
|
| 103 |
+
st.session_state.response_content += f'{header}\n\n{anchor}\n\n'
|
| 104 |
+
else:
|
| 105 |
+
header = f'\n\n---\n{st.session_state.question_text}\n---\n\n'
|
| 106 |
+
anchor = create_anchor_link(st.session_state.question_text)
|
| 107 |
+
st.session_state.response_content += f'{header}{anchor}\n\n'
|
| 108 |
+
|
| 109 |
+
st.session_state.function_call_count += 1
|
| 110 |
+
client = OpenAI(api_key=api_key)
|
| 111 |
+
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
| 112 |
+
|
| 113 |
+
response = client.chat.completions.create(
|
| 114 |
+
model="gpt-4",
|
| 115 |
+
messages=st.session_state.messages,
|
| 116 |
+
stream=True
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
expander = st.expander(st.session_state.question_text, expanded=True)
|
| 120 |
+
with expander:
|
| 121 |
+
# Use the placeholder defined outside the block
|
| 122 |
+
expander_placeholder = st.empty()
|
| 123 |
+
|
| 124 |
+
for chunk in response:
|
| 125 |
+
if st.session_state.stopped:
|
| 126 |
+
st.session_state.stopped = False
|
| 127 |
+
st.session_state.submitted = False
|
| 128 |
+
break
|
| 129 |
+
else:
|
| 130 |
+
if chunk and chunk.choices[0].delta.content:
|
| 131 |
+
st.session_state.response_content += chunk.choices[0].delta.content
|
| 132 |
+
expander_placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
|
| 133 |
+
st.session_state.submitted = False
|
| 134 |
+
st.session_state.stopped = False
|
| 135 |
+
|
| 136 |
+
#expander_placeholder.markdown(st.session_state.response_content, unsafe_allow_html=True)
|
| 137 |
+
st.session_state.stopped = False
|
app copy 6.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from st_pages import Page, show_pages
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from whisper_stt import whisper_stt
|
| 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 |
+
|
| 31 |
+
def on_stop():
|
| 32 |
+
st.session_state.stopped = True
|
| 33 |
+
|
| 34 |
+
def handle_enter(key):
|
| 35 |
+
if key == "ctrl+enter":
|
| 36 |
+
new_question = st.session_state.question_input
|
| 37 |
+
print(f"handle_enter called. new_question: '{new_question}'")
|
| 38 |
+
print(f"session state: {st.session_state}")
|
| 39 |
+
|
| 40 |
+
with st.sidebar:
|
| 41 |
+
api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
|
| 42 |
+
|
| 43 |
+
col1, col2 = st.columns(2)
|
| 44 |
+
|
| 45 |
+
with col1:
|
| 46 |
+
# Call whisper_stt without a callback
|
| 47 |
+
transcribed_text = whisper_stt(
|
| 48 |
+
openai_api_key=api_key,
|
| 49 |
+
language='en'
|
| 50 |
+
)
|
| 51 |
+
if transcribed_text:
|
| 52 |
+
st.session_state.question_text = transcribed_text
|
| 53 |
+
|
| 54 |
+
# Check if new transcription is available
|
| 55 |
+
if transcribed_text and transcribed_text != st.session_state.transcribed_text:
|
| 56 |
+
st.session_state.transcribed_text = transcribed_text
|
| 57 |
+
st.session_state.question_text = transcribed_text
|
| 58 |
+
st.session_state.submitted = True
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
with col2:
|
| 64 |
+
st.button(label='Stop', on_click=on_stop)
|
| 65 |
+
|
| 66 |
+
# Create an input for the question and use new_question directly
|
| 67 |
+
new_question = st.text_area("Question",
|
| 68 |
+
value=st.session_state.question_text or "",
|
| 69 |
+
height=150,
|
| 70 |
+
key="question_input",
|
| 71 |
+
on_change=handle_enter,
|
| 72 |
+
args=("ctrl+enter",)
|
| 73 |
+
)
|
| 74 |
+
print(f"After text_area, new_question: '{new_question}'")
|
| 75 |
+
# Check if new_question has changed and is not empty
|
| 76 |
+
if new_question and new_question != st.session_state.question_text:
|
| 77 |
+
st.session_state.question_text = new_question
|
| 78 |
+
st.session_state.submitted = True
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if st.session_state.question_text and not api_key:
|
| 83 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 84 |
+
st.stop()
|
| 85 |
+
|
| 86 |
+
if st.session_state.submitted and not st.session_state.stopped:
|
| 87 |
+
st.session_state.headers.append(st.session_state.question_text)
|
| 88 |
+
|
| 89 |
+
client = OpenAI(api_key=api_key)
|
| 90 |
+
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
| 91 |
+
|
| 92 |
+
response = client.chat.completions.create(
|
| 93 |
+
model="gpt-4o",
|
| 94 |
+
messages=st.session_state.messages,
|
| 95 |
+
stream=True
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
expander = st.expander(f"<h2><strong>{st.session_state.question_text}</strong></h2>", expanded=True)
|
| 99 |
+
response_placeholder = expander.empty()
|
| 100 |
+
complete_response = ""
|
| 101 |
+
|
| 102 |
+
for chunk in response:
|
| 103 |
+
if st.session_state.stopped:
|
| 104 |
+
st.session_state.stopped = False
|
| 105 |
+
st.session_state.submitted = False
|
| 106 |
+
break
|
| 107 |
+
else:
|
| 108 |
+
if chunk and chunk.choices[0].delta.content:
|
| 109 |
+
complete_response += chunk.choices[0].delta.content
|
| 110 |
+
response_placeholder.markdown(complete_response, unsafe_allow_html=True)
|
| 111 |
+
st.session_state.submitted = False
|
| 112 |
+
st.session_state.stopped = False
|
app copy 7.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from st_pages import Page, show_pages
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from whisper_stt import whisper_stt
|
| 5 |
+
|
| 6 |
+
# Set page configuration
|
| 7 |
+
st.set_page_config(layout="wide")
|
| 8 |
+
show_pages([Page("app.py", "Home", "🏠")])
|
| 9 |
+
|
| 10 |
+
# Custom CSS for expander header
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
.streamlit-expanderHeader {
|
| 14 |
+
font-size: 24px !important;
|
| 15 |
+
font-weight: bold !important;
|
| 16 |
+
color: #1E90FF !important;
|
| 17 |
+
}
|
| 18 |
+
</style>
|
| 19 |
+
""", unsafe_allow_html=True)
|
| 20 |
+
|
| 21 |
+
# Initialize session state variables
|
| 22 |
+
if 'paused' not in st.session_state:
|
| 23 |
+
st.session_state.paused = False
|
| 24 |
+
if 'question_text' not in st.session_state:
|
| 25 |
+
st.session_state.question_text = ""
|
| 26 |
+
if 'submitted' not in st.session_state:
|
| 27 |
+
st.session_state.submitted = False
|
| 28 |
+
if 'response_content' not in st.session_state:
|
| 29 |
+
st.session_state.response_content = ""
|
| 30 |
+
if 'stopped' not in st.session_state:
|
| 31 |
+
st.session_state.stopped = False
|
| 32 |
+
if 'function_call_count' not in st.session_state:
|
| 33 |
+
st.session_state.function_call_count = 0
|
| 34 |
+
if 'transcribed_text' not in st.session_state:
|
| 35 |
+
st.session_state.transcribed_text = ""
|
| 36 |
+
if 'last_processed_text' not in st.session_state:
|
| 37 |
+
st.session_state.last_processed_text = ""
|
| 38 |
+
if 'headers' not in st.session_state:
|
| 39 |
+
st.session_state.headers = []
|
| 40 |
+
|
| 41 |
+
def on_stop():
|
| 42 |
+
st.session_state.stopped = True
|
| 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 |
+
|
| 53 |
+
col1, col2 = st.columns(2)
|
| 54 |
+
|
| 55 |
+
with col1:
|
| 56 |
+
# Call whisper_stt without a callback
|
| 57 |
+
transcribed_text = whisper_stt(
|
| 58 |
+
openai_api_key=api_key,
|
| 59 |
+
language='en'
|
| 60 |
+
)
|
| 61 |
+
if transcribed_text:
|
| 62 |
+
st.session_state.question_text = transcribed_text
|
| 63 |
+
|
| 64 |
+
# Check if new transcription is available
|
| 65 |
+
if transcribed_text and transcribed_text != st.session_state.transcribed_text:
|
| 66 |
+
st.session_state.transcribed_text = transcribed_text
|
| 67 |
+
st.session_state.question_text = transcribed_text
|
| 68 |
+
st.session_state.submitted = True
|
| 69 |
+
|
| 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 |
+
if st.session_state.question_text and not api_key:
|
| 88 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 89 |
+
st.stop()
|
| 90 |
+
|
| 91 |
+
if st.session_state.submitted and not st.session_state.stopped:
|
| 92 |
+
st.session_state.headers.append(st.session_state.question_text)
|
| 93 |
+
|
| 94 |
+
client = OpenAI(api_key=api_key)
|
| 95 |
+
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
| 96 |
+
|
| 97 |
+
response = client.chat.completions.create(
|
| 98 |
+
model="gpt-4o",
|
| 99 |
+
messages=st.session_state.messages,
|
| 100 |
+
stream=True
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Use Streamlit's expander
|
| 104 |
+
with st.expander(st.session_state.question_text, expanded=True):
|
| 105 |
+
response_placeholder = st.empty()
|
| 106 |
+
complete_response = ""
|
| 107 |
+
|
| 108 |
+
for chunk in response:
|
| 109 |
+
if st.session_state.stopped:
|
| 110 |
+
st.session_state.stopped = False
|
| 111 |
+
st.session_state.submitted = False
|
| 112 |
+
break
|
| 113 |
+
else:
|
| 114 |
+
if chunk and chunk.choices[0].delta.content:
|
| 115 |
+
complete_response += chunk.choices[0].delta.content
|
| 116 |
+
response_placeholder.markdown(complete_response, unsafe_allow_html=True)
|
| 117 |
+
|
| 118 |
+
st.session_state.submitted = False
|
| 119 |
+
st.session_state.stopped = False
|
app copy final.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from st_pages import Page, show_pages
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from whisper_stt import whisper_stt
|
| 5 |
+
|
| 6 |
+
# Set page configuration
|
| 7 |
+
st.set_page_config(layout="wide")
|
| 8 |
+
show_pages([Page("app.py", "Home", "🏠")])
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# JavaScript to preserve scroll position
|
| 13 |
+
st.markdown("""
|
| 14 |
+
<script>
|
| 15 |
+
window.addEventListener("load", function() {
|
| 16 |
+
if (window.location.hash) {
|
| 17 |
+
var hash = window.location.hash.substring(1);
|
| 18 |
+
if (hash && document.getElementById(hash)) {
|
| 19 |
+
document.getElementById(hash).scrollIntoView();
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
document.querySelectorAll('a').forEach(anchor => {
|
| 25 |
+
anchor.addEventListener('click', function (e) {
|
| 26 |
+
e.preventDefault();
|
| 27 |
+
var targetId = this.getAttribute('href').substring(1);
|
| 28 |
+
var targetElement = document.getElementById(targetId);
|
| 29 |
+
if (targetElement) {
|
| 30 |
+
targetElement.scrollIntoView({ behavior: 'smooth' });
|
| 31 |
+
}
|
| 32 |
+
window.location.hash = targetId;
|
| 33 |
+
});
|
| 34 |
+
});
|
| 35 |
+
</script>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
# Initialize session state variables
|
| 39 |
+
if 'paused' not in st.session_state:
|
| 40 |
+
st.session_state.paused = False
|
| 41 |
+
if 'question_text' not in st.session_state:
|
| 42 |
+
st.session_state.question_text = ""
|
| 43 |
+
if 'submitted' not in st.session_state:
|
| 44 |
+
st.session_state.submitted = False
|
| 45 |
+
if 'response_content' not in st.session_state:
|
| 46 |
+
st.session_state.response_content = ""
|
| 47 |
+
if 'stopped' not in st.session_state:
|
| 48 |
+
st.session_state.stopped = False
|
| 49 |
+
if 'function_call_count' not in st.session_state:
|
| 50 |
+
st.session_state.function_call_count = 0
|
| 51 |
+
if 'transcribed_text' not in st.session_state:
|
| 52 |
+
st.session_state.transcribed_text = ""
|
| 53 |
+
if 'last_processed_text' not in st.session_state:
|
| 54 |
+
st.session_state.last_processed_text = ""
|
| 55 |
+
if 'headers' not in st.session_state:
|
| 56 |
+
st.session_state.headers = []
|
| 57 |
+
|
| 58 |
+
def on_stop():
|
| 59 |
+
st.session_state.stopped = True
|
| 60 |
+
|
| 61 |
+
def handle_enter(key):
|
| 62 |
+
if key == "ctrl+enter":
|
| 63 |
+
new_question = st.session_state.question_input
|
| 64 |
+
print(f"handle_enter called. new_question: '{new_question}'")
|
| 65 |
+
print(f"session state: {st.session_state}")
|
| 66 |
+
|
| 67 |
+
with st.sidebar:
|
| 68 |
+
api_key = st.text_input("API Key", key="chatbot_api_key", type="password")
|
| 69 |
+
|
| 70 |
+
col1, col2 = st.columns(2)
|
| 71 |
+
|
| 72 |
+
with col1:
|
| 73 |
+
# Call whisper_stt without a callback
|
| 74 |
+
transcribed_text = whisper_stt(
|
| 75 |
+
openai_api_key=api_key,
|
| 76 |
+
language='en'
|
| 77 |
+
)
|
| 78 |
+
if transcribed_text:
|
| 79 |
+
st.session_state.question_text = transcribed_text
|
| 80 |
+
|
| 81 |
+
# Check if new transcription is available
|
| 82 |
+
if transcribed_text and transcribed_text != st.session_state.transcribed_text:
|
| 83 |
+
st.session_state.transcribed_text = transcribed_text
|
| 84 |
+
st.session_state.question_text = transcribed_text
|
| 85 |
+
st.session_state.submitted = True
|
| 86 |
+
|
| 87 |
+
with col2:
|
| 88 |
+
st.button(label='Stop', on_click=on_stop)
|
| 89 |
+
|
| 90 |
+
# Create an input for the question and use new_question directly
|
| 91 |
+
new_question = st.text_area("Question",
|
| 92 |
+
value=st.session_state.question_text or "",
|
| 93 |
+
height=150,
|
| 94 |
+
key="question_input",
|
| 95 |
+
on_change=handle_enter,
|
| 96 |
+
args=("ctrl+enter",)
|
| 97 |
+
)
|
| 98 |
+
print(f"After text_area, new_question: '{new_question}'")
|
| 99 |
+
# Check if new_question has changed and is not empty
|
| 100 |
+
if new_question and new_question != st.session_state.question_text:
|
| 101 |
+
st.session_state.question_text = new_question
|
| 102 |
+
st.session_state.submitted = True
|
| 103 |
+
|
| 104 |
+
if st.session_state.question_text and not api_key:
|
| 105 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 106 |
+
st.stop()
|
| 107 |
+
|
| 108 |
+
if st.session_state.submitted and not st.session_state.stopped:
|
| 109 |
+
st.session_state.headers.append(st.session_state.question_text)
|
| 110 |
+
|
| 111 |
+
client = OpenAI(api_key=api_key)
|
| 112 |
+
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
| 113 |
+
|
| 114 |
+
response = client.chat.completions.create(
|
| 115 |
+
model="gpt-4o",
|
| 116 |
+
messages=st.session_state.messages,
|
| 117 |
+
stream=True
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Use Streamlit's expander
|
| 121 |
+
with st.expander(st.session_state.question_text, expanded=True):
|
| 122 |
+
response_placeholder = st.empty()
|
| 123 |
+
complete_response = ""
|
| 124 |
+
|
| 125 |
+
for chunk in response:
|
| 126 |
+
if st.session_state.stopped:
|
| 127 |
+
st.session_state.stopped = False
|
| 128 |
+
st.session_state.submitted = False
|
| 129 |
+
break
|
| 130 |
+
else:
|
| 131 |
+
if chunk and chunk.choices[0].delta.content:
|
| 132 |
+
complete_response += chunk.choices[0].delta.content
|
| 133 |
+
response_placeholder.markdown(complete_response, unsafe_allow_html=True)
|
| 134 |
+
|
| 135 |
+
st.session_state.submitted = False
|
| 136 |
+
st.session_state.stopped = False
|
app.py
CHANGED
|
@@ -1,13 +1,49 @@
|
|
| 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")
|
| 9 |
show_pages([Page("app.py", "Home", "🏠")])
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Initialize session state variables
|
| 12 |
if 'paused' not in st.session_state:
|
| 13 |
st.session_state.paused = False
|
|
@@ -27,20 +63,12 @@ if 'last_processed_text' not in st.session_state:
|
|
| 27 |
st.session_state.last_processed_text = ""
|
| 28 |
if 'headers' not in st.session_state:
|
| 29 |
st.session_state.headers = []
|
| 30 |
-
|
| 31 |
-
|
| 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}' onclick='navigateToSection(\"{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
|
|
@@ -67,16 +95,9 @@ with st.sidebar:
|
|
| 67 |
st.session_state.question_text = transcribed_text
|
| 68 |
st.session_state.submitted = True
|
| 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)
|
| 77 |
with col2:
|
| 78 |
st.button(label='Stop', on_click=on_stop)
|
| 79 |
-
|
| 80 |
# Create an input for the question and use new_question directly
|
| 81 |
new_question = st.text_area("Question",
|
| 82 |
value=st.session_state.question_text or "",
|
|
@@ -91,27 +112,13 @@ with st.sidebar:
|
|
| 91 |
st.session_state.question_text = new_question
|
| 92 |
st.session_state.submitted = True
|
| 93 |
|
| 94 |
-
st.markdown("## Navigation")
|
| 95 |
-
for header in st.session_state.headers:
|
| 96 |
-
st.markdown(create_anchor_link(header), unsafe_allow_html=True)
|
| 97 |
-
|
| 98 |
if st.session_state.question_text and not api_key:
|
| 99 |
st.info("Please add your OpenAI API key to continue.")
|
| 100 |
st.stop()
|
| 101 |
|
| 102 |
if st.session_state.submitted and not st.session_state.stopped:
|
| 103 |
st.session_state.headers.append(st.session_state.question_text)
|
| 104 |
-
|
| 105 |
-
if st.session_state.function_call_count == 0:
|
| 106 |
-
header = f'## {st.session_state.question_text}'
|
| 107 |
-
anchor = create_anchor_link(st.session_state.question_text)
|
| 108 |
-
st.session_state.response_content += f'{header}\n\n{anchor}\n\n'
|
| 109 |
-
else:
|
| 110 |
-
header = f'\n\n---\n{st.session_state.question_text}\n---\n\n'
|
| 111 |
-
anchor = create_anchor_link(st.session_state.question_text)
|
| 112 |
-
st.session_state.response_content += f'{header}{anchor}\n\n'
|
| 113 |
-
|
| 114 |
-
st.session_state.function_call_count += 1
|
| 115 |
client = OpenAI(api_key=api_key)
|
| 116 |
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
| 117 |
|
|
@@ -121,6 +128,10 @@ if st.session_state.submitted and not st.session_state.stopped:
|
|
| 121 |
stream=True
|
| 122 |
)
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
for chunk in response:
|
| 125 |
if st.session_state.stopped:
|
| 126 |
st.session_state.stopped = False
|
|
@@ -128,52 +139,20 @@ if st.session_state.submitted and not st.session_state.stopped:
|
|
| 128 |
break
|
| 129 |
else:
|
| 130 |
if chunk and chunk.choices[0].delta.content:
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
st.session_state.submitted = False
|
| 135 |
st.session_state.stopped = False
|
| 136 |
|
| 137 |
-
|
| 138 |
-
st.session_state.
|
| 139 |
-
|
| 140 |
-
#
|
| 141 |
-
|
| 142 |
-
|
| 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 navigateToSection(anchor) {
|
| 151 |
-
const element = document.getElementById(anchor);
|
| 152 |
-
if (element) {
|
| 153 |
-
element.scrollIntoView({behavior: 'smooth'});
|
| 154 |
-
} else {
|
| 155 |
-
console.error('Section not found:', anchor);
|
| 156 |
-
}
|
| 157 |
-
}
|
| 158 |
-
</script>
|
| 159 |
-
""",
|
| 160 |
-
unsafe_allow_html=True
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
# JavaScript for intercepting clicks
|
| 164 |
-
st.markdown(
|
| 165 |
-
"""
|
| 166 |
-
<script>
|
| 167 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 168 |
-
document.querySelectorAll('a').forEach(anchor => {
|
| 169 |
-
anchor.addEventListener('click', function(e) {
|
| 170 |
-
e.preventDefault();
|
| 171 |
-
const href = this.getAttribute('href').substring(1);
|
| 172 |
-
navigateToSection(href);
|
| 173 |
-
});
|
| 174 |
-
});
|
| 175 |
-
});
|
| 176 |
-
</script>
|
| 177 |
-
""",
|
| 178 |
-
unsafe_allow_html=True
|
| 179 |
-
)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from st_pages import Page, show_pages
|
| 3 |
from openai import OpenAI
|
|
|
|
| 4 |
from whisper_stt import whisper_stt
|
| 5 |
|
| 6 |
# Set page configuration
|
| 7 |
st.set_page_config(layout="wide")
|
| 8 |
show_pages([Page("app.py", "Home", "🏠")])
|
| 9 |
|
| 10 |
+
# Custom CSS for expander header
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
.streamlit-expanderHeader {
|
| 14 |
+
font-size: 24px !important;
|
| 15 |
+
font-weight: bold !important;
|
| 16 |
+
color: #1E90FF !important;
|
| 17 |
+
}
|
| 18 |
+
</style>
|
| 19 |
+
""", unsafe_allow_html=True)
|
| 20 |
+
|
| 21 |
+
# JavaScript to preserve scroll position
|
| 22 |
+
st.markdown("""
|
| 23 |
+
<script>
|
| 24 |
+
window.addEventListener("load", function() {
|
| 25 |
+
if (window.location.hash) {
|
| 26 |
+
var hash = window.location.hash.substring(1);
|
| 27 |
+
if (hash && document.getElementById(hash)) {
|
| 28 |
+
document.getElementById(hash).scrollIntoView();
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
document.querySelectorAll('a').forEach(anchor => {
|
| 34 |
+
anchor.addEventListener('click', function (e) {
|
| 35 |
+
e.preventDefault();
|
| 36 |
+
var targetId = this.getAttribute('href').substring(1);
|
| 37 |
+
var targetElement = document.getElementById(targetId);
|
| 38 |
+
if (targetElement) {
|
| 39 |
+
targetElement.scrollIntoView({ behavior: 'smooth' });
|
| 40 |
+
}
|
| 41 |
+
window.location.hash = targetId;
|
| 42 |
+
});
|
| 43 |
+
});
|
| 44 |
+
</script>
|
| 45 |
+
""", unsafe_allow_html=True)
|
| 46 |
+
|
| 47 |
# Initialize session state variables
|
| 48 |
if 'paused' not in st.session_state:
|
| 49 |
st.session_state.paused = False
|
|
|
|
| 63 |
st.session_state.last_processed_text = ""
|
| 64 |
if 'headers' not in st.session_state:
|
| 65 |
st.session_state.headers = []
|
| 66 |
+
if 'history' not in st.session_state:
|
| 67 |
+
st.session_state.history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def on_stop():
|
| 70 |
st.session_state.stopped = True
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
def handle_enter(key):
|
| 73 |
if key == "ctrl+enter":
|
| 74 |
new_question = st.session_state.question_input
|
|
|
|
| 95 |
st.session_state.question_text = transcribed_text
|
| 96 |
st.session_state.submitted = True
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
with col2:
|
| 99 |
st.button(label='Stop', on_click=on_stop)
|
| 100 |
+
|
| 101 |
# Create an input for the question and use new_question directly
|
| 102 |
new_question = st.text_area("Question",
|
| 103 |
value=st.session_state.question_text or "",
|
|
|
|
| 112 |
st.session_state.question_text = new_question
|
| 113 |
st.session_state.submitted = True
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
if st.session_state.question_text and not api_key:
|
| 116 |
st.info("Please add your OpenAI API key to continue.")
|
| 117 |
st.stop()
|
| 118 |
|
| 119 |
if st.session_state.submitted and not st.session_state.stopped:
|
| 120 |
st.session_state.headers.append(st.session_state.question_text)
|
| 121 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
client = OpenAI(api_key=api_key)
|
| 123 |
st.session_state.messages = [{"role": "user", "content": st.session_state.question_text}]
|
| 124 |
|
|
|
|
| 128 |
stream=True
|
| 129 |
)
|
| 130 |
|
| 131 |
+
complete_response = ""
|
| 132 |
+
current_expander = st.expander(st.session_state.question_text, expanded=True)
|
| 133 |
+
response_placeholder = current_expander.empty()
|
| 134 |
+
|
| 135 |
for chunk in response:
|
| 136 |
if st.session_state.stopped:
|
| 137 |
st.session_state.stopped = False
|
|
|
|
| 139 |
break
|
| 140 |
else:
|
| 141 |
if chunk and chunk.choices[0].delta.content:
|
| 142 |
+
complete_response += chunk.choices[0].delta.content
|
| 143 |
+
response_placeholder.markdown(complete_response, unsafe_allow_html=True)
|
| 144 |
+
|
| 145 |
+
st.session_state.response_content = complete_response
|
| 146 |
+
st.session_state.history.insert(0, {
|
| 147 |
+
'question': st.session_state.question_text,
|
| 148 |
+
'response': complete_response
|
| 149 |
+
})
|
| 150 |
st.session_state.submitted = False
|
| 151 |
st.session_state.stopped = False
|
| 152 |
|
| 153 |
+
# Display all questions and answers
|
| 154 |
+
for idx, entry in enumerate(st.session_state.history):
|
| 155 |
+
if idx == 0 and st.session_state.response_content:
|
| 156 |
+
continue # Skip the first item since it's already shown as the current expander
|
| 157 |
+
with st.expander(entry['question'], expanded=False):
|
| 158 |
+
st.markdown(entry['response'], unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bottom.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
if 'chat' not in st.session_state:
|
| 4 |
+
st.session_state.chat = "A: Hello"
|
| 5 |
+
|
| 6 |
+
def submit():
|
| 7 |
+
st.session_state.chat += f' \nB: {st.session_state.B}'
|
| 8 |
+
st.session_state.chat += ' \nA: Some response.'
|
| 9 |
+
# Clear the text input widget for convenience
|
| 10 |
+
st.session_state.B = ''
|
| 11 |
+
|
| 12 |
+
st.write(st.session_state.chat)
|
| 13 |
+
|
| 14 |
+
st.text_input('B\'s Response', key='B', on_change=submit)
|
| 15 |
+
|
| 16 |
+
# Define the scroll operation as a function and pass in something unique for each
|
| 17 |
+
# page load that it needs to re-evaluate where "bottom" is
|
| 18 |
+
js = f"""
|
| 19 |
+
<script>
|
| 20 |
+
function scroll(dummy_var_to_force_repeat_execution){{
|
| 21 |
+
var textAreas = parent.document.querySelectorAll('section.main');
|
| 22 |
+
for (let index = 0; index < textAreas.length; index++) {{
|
| 23 |
+
textAreas[index].style.color = 'red'
|
| 24 |
+
textAreas[index].scrollTop = textAreas[index].scrollHeight;
|
| 25 |
+
}}
|
| 26 |
+
}}
|
| 27 |
+
scroll({len(st.session_state.chat)})
|
| 28 |
+
</script>
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
st.components.v1.html(js)
|
expand.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
st.bar_chart({"data": [1, 5, 2, 6, 2, 1]})
|
| 4 |
+
|
| 5 |
+
with st.expander("See explanation"):
|
| 6 |
+
st.write('''
|
| 7 |
+
The chart above shows some numbers I picked for you.
|
| 8 |
+
I rolled actual dice for these, so they're *guaranteed* to
|
| 9 |
+
be random.
|
| 10 |
+
''')
|