Upload app.py
Browse files
app.py
CHANGED
|
@@ -1624,6 +1624,29 @@ def ask_ai(problem: str, sympy_info: dict, history: list) -> str:
|
|
| 1624 |
)
|
| 1625 |
|
| 1626 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1627 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1628 |
# SIDEBAR
|
| 1629 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
@@ -1747,12 +1770,14 @@ if problem and problem != st.session_state.last_submitted:
|
|
| 1747 |
with st.chat_message("user", avatar="๐งโ๐"):
|
| 1748 |
st.markdown(problem)
|
| 1749 |
|
| 1750 |
-
# Show AI response
|
| 1751 |
with st.chat_message("assistant", avatar="๐"):
|
|
|
|
| 1752 |
with st.spinner("๐งฎ Computing..."):
|
| 1753 |
sympy_result = run_sympy(problem)
|
| 1754 |
-
|
| 1755 |
-
|
|
|
|
| 1756 |
|
| 1757 |
# Save to history
|
| 1758 |
st.session_state.messages.append({"role": "user", "content": problem})
|
|
|
|
| 1624 |
)
|
| 1625 |
|
| 1626 |
|
| 1627 |
+
|
| 1628 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1629 |
+
# STREAMING WRAPPER โ shows text word by word like ChatGPT
|
| 1630 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1631 |
+
def ask_ai_streaming(problem: str, sympy_info: dict, history: list) -> str:
|
| 1632 |
+
"""Get AI response and stream it word by word using st.write_stream."""
|
| 1633 |
+
import time
|
| 1634 |
+
|
| 1635 |
+
# Get full response first
|
| 1636 |
+
full_response = ask_ai(problem, sympy_info, history)
|
| 1637 |
+
|
| 1638 |
+
# Stream it word by word
|
| 1639 |
+
def word_generator():
|
| 1640 |
+
words = full_response.split(" ")
|
| 1641 |
+
for i, word in enumerate(words):
|
| 1642 |
+
yield word + (" " if i < len(words)-1 else "")
|
| 1643 |
+
time.sleep(0.015) # 15ms per word โ smooth streaming
|
| 1644 |
+
|
| 1645 |
+
# Use st.write_stream for streaming display
|
| 1646 |
+
streamed = st.write_stream(word_generator())
|
| 1647 |
+
return full_response # return full for history
|
| 1648 |
+
|
| 1649 |
+
|
| 1650 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1651 |
# SIDEBAR
|
| 1652 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 1770 |
with st.chat_message("user", avatar="๐งโ๐"):
|
| 1771 |
st.markdown(problem)
|
| 1772 |
|
| 1773 |
+
# Show AI response with streaming
|
| 1774 |
with st.chat_message("assistant", avatar="๐"):
|
| 1775 |
+
# Step 1: SymPy computation (instant)
|
| 1776 |
with st.spinner("๐งฎ Computing..."):
|
| 1777 |
sympy_result = run_sympy(problem)
|
| 1778 |
+
|
| 1779 |
+
# Step 2: Stream AI response word by word
|
| 1780 |
+
answer = ask_ai_streaming(problem, sympy_result, st.session_state.messages)
|
| 1781 |
|
| 1782 |
# Save to history
|
| 1783 |
st.session_state.messages.append({"role": "user", "content": problem})
|