Ubuntu
commited on
Commit
·
b8f95fe
1
Parent(s):
99870e3
UI fixes 6
Browse files
app.py
CHANGED
@@ -106,40 +106,41 @@ for message in st.session_state.messages:
|
|
106 |
else:
|
107 |
st.markdown(f'<div class="assistant-message">{message["content"]}</div>', unsafe_allow_html=True)
|
108 |
|
109 |
-
# Input form
|
110 |
-
|
111 |
-
user_input = st.text_input("Message:", key="user_input")
|
112 |
-
col1, col2 = st.columns([4, 1])
|
113 |
-
with col1:
|
114 |
-
submit_button = st.form_submit_button("Send")
|
115 |
-
with col2:
|
116 |
-
clear_button = st.form_submit_button("Clear Chat")
|
117 |
|
118 |
-
#
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
# Generate response
|
124 |
-
try:
|
125 |
-
full_response = ""
|
126 |
-
for token in generate_response(
|
127 |
-
st.session_state.model,
|
128 |
-
st.session_state.tokenizer,
|
129 |
-
user_input
|
130 |
-
):
|
131 |
-
full_response += token
|
132 |
-
|
133 |
-
# Add assistant response
|
134 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
135 |
-
except Exception as e:
|
136 |
-
st.session_state.messages.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
137 |
-
|
138 |
-
st.rerun()
|
139 |
|
140 |
-
# Handle
|
141 |
-
if
|
142 |
st.session_state.messages = []
|
143 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
else:
|
107 |
st.markdown(f'<div class="assistant-message">{message["content"]}</div>', unsafe_allow_html=True)
|
108 |
|
109 |
+
# Input box (outside form to keep state during generation)
|
110 |
+
user_input = st.text_input("Message:", key="user_input", placeholder="Type your message here...")
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
+
# Submit and Clear buttons
|
113 |
+
col1, col2 = st.columns([4, 1])
|
114 |
+
submit = col1.button("Send")
|
115 |
+
clear = col2.button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
+
# Handle Clear button
|
118 |
+
if clear:
|
119 |
st.session_state.messages = []
|
120 |
+
st.experimental_rerun()
|
121 |
+
|
122 |
+
# Handle user input + generate response
|
123 |
+
if submit and user_input:
|
124 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
125 |
+
with st.spinner("Assistant is typing..."):
|
126 |
+
response = ""
|
127 |
+
response_placeholder = st.empty()
|
128 |
|
129 |
+
try:
|
130 |
+
for token in generate_response(
|
131 |
+
st.session_state.model,
|
132 |
+
st.session_state.tokenizer,
|
133 |
+
user_input
|
134 |
+
):
|
135 |
+
response += token
|
136 |
+
response_placeholder.markdown(
|
137 |
+
f'<div class="assistant-message">{response}</div>',
|
138 |
+
unsafe_allow_html=True
|
139 |
+
)
|
140 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
141 |
+
except Exception as e:
|
142 |
+
error_msg = f"Error: {str(e)}"
|
143 |
+
st.session_state.messages.append({"role": "assistant", "content": error_msg})
|
144 |
+
st.error(error_msg)
|
145 |
+
|
146 |
+
st.experimental_rerun()
|