Spaces:
Sleeping
Sleeping
Commit ·
09edc25
1
Parent(s): 6bc818d
Fix: Add st.rerun() to example buttons to prevent blank page 2
Browse files- src/app.py +15 -3
src/app.py
CHANGED
|
@@ -7,6 +7,12 @@ import sys
|
|
| 7 |
from pathlib import Path
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Add parent directory to path for imports
|
| 11 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 12 |
|
|
@@ -97,6 +103,12 @@ if not st.session_state.config.is_valid():
|
|
| 97 |
st.error("Please set OPENAI_API_KEY in your .env file or environment variables.")
|
| 98 |
st.stop()
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Example texts for quick testing
|
| 101 |
with st.expander("Try Example Texts"):
|
| 102 |
col1, col2, col3 = st.columns(3)
|
|
@@ -105,19 +117,19 @@ with st.expander("Try Example Texts"):
|
|
| 105 |
if st.button("Example 1: Product Review", key="ex1"):
|
| 106 |
st.session_state.example_text = """This product is absolutely amazing! I've been using it for three months now and couldn't be happier. The quality is outstanding, and the customer service is excellent. The design is beautiful and modern. However, the price is a bit high, but I think it's worth every penny. Highly recommended for anyone looking for a reliable solution. The features are innovative and user-friendly. I love how easy it is to use!"""
|
| 107 |
st.session_state.example_question = "What is the overall sentiment and what are the main topics discussed?"
|
| 108 |
-
|
| 109 |
|
| 110 |
with col2:
|
| 111 |
if st.button("Example 2: News Article", key="ex2"):
|
| 112 |
st.session_state.example_text = """The new technology breakthrough announced today promises to revolutionize the industry. Scientists have developed an innovative approach that could solve long-standing problems. The research team, led by experts from multiple universities, demonstrated significant improvements in efficiency. However, some critics argue that implementation challenges remain. The project received substantial funding from government agencies and private investors. Initial tests show promising results, though more research is needed."""
|
| 113 |
st.session_state.example_question = "Extract the key topics and analyze the sentiment"
|
| 114 |
-
|
| 115 |
|
| 116 |
with col3:
|
| 117 |
if st.button("Example 3: Short Story", key="ex3"):
|
| 118 |
st.session_state.example_text = """The old lighthouse stood tall against the stormy night. Waves crashed violently against the rocks below. Inside, the keeper tended the light, ensuring it never went out. For years, he had performed this duty faithfully. Tonight felt different somehow. The wind howled louder than ever before. Yet he remained calm, focused on his purpose. Ships depended on that light. Lives depended on his vigilance."""
|
| 119 |
st.session_state.example_question = "How many words are in this text and what is the mood?"
|
| 120 |
-
|
| 121 |
|
| 122 |
# Text input
|
| 123 |
st.markdown("#### Text to Analyze")
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
+
# Compatibility: handle both st.rerun() and st.experimental_rerun()
|
| 11 |
+
if hasattr(st, 'rerun'):
|
| 12 |
+
rerun = st.rerun
|
| 13 |
+
else:
|
| 14 |
+
rerun = st.experimental_rerun
|
| 15 |
+
|
| 16 |
# Add parent directory to path for imports
|
| 17 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 18 |
|
|
|
|
| 103 |
st.error("Please set OPENAI_API_KEY in your .env file or environment variables.")
|
| 104 |
st.stop()
|
| 105 |
|
| 106 |
+
# Initialize example state if not exists
|
| 107 |
+
if 'example_text' not in st.session_state:
|
| 108 |
+
st.session_state.example_text = ''
|
| 109 |
+
if 'example_question' not in st.session_state:
|
| 110 |
+
st.session_state.example_question = ''
|
| 111 |
+
|
| 112 |
# Example texts for quick testing
|
| 113 |
with st.expander("Try Example Texts"):
|
| 114 |
col1, col2, col3 = st.columns(3)
|
|
|
|
| 117 |
if st.button("Example 1: Product Review", key="ex1"):
|
| 118 |
st.session_state.example_text = """This product is absolutely amazing! I've been using it for three months now and couldn't be happier. The quality is outstanding, and the customer service is excellent. The design is beautiful and modern. However, the price is a bit high, but I think it's worth every penny. Highly recommended for anyone looking for a reliable solution. The features are innovative and user-friendly. I love how easy it is to use!"""
|
| 119 |
st.session_state.example_question = "What is the overall sentiment and what are the main topics discussed?"
|
| 120 |
+
rerun()
|
| 121 |
|
| 122 |
with col2:
|
| 123 |
if st.button("Example 2: News Article", key="ex2"):
|
| 124 |
st.session_state.example_text = """The new technology breakthrough announced today promises to revolutionize the industry. Scientists have developed an innovative approach that could solve long-standing problems. The research team, led by experts from multiple universities, demonstrated significant improvements in efficiency. However, some critics argue that implementation challenges remain. The project received substantial funding from government agencies and private investors. Initial tests show promising results, though more research is needed."""
|
| 125 |
st.session_state.example_question = "Extract the key topics and analyze the sentiment"
|
| 126 |
+
rerun()
|
| 127 |
|
| 128 |
with col3:
|
| 129 |
if st.button("Example 3: Short Story", key="ex3"):
|
| 130 |
st.session_state.example_text = """The old lighthouse stood tall against the stormy night. Waves crashed violently against the rocks below. Inside, the keeper tended the light, ensuring it never went out. For years, he had performed this duty faithfully. Tonight felt different somehow. The wind howled louder than ever before. Yet he remained calm, focused on his purpose. Ships depended on that light. Lives depended on his vigilance."""
|
| 131 |
st.session_state.example_question = "How many words are in this text and what is the mood?"
|
| 132 |
+
rerun()
|
| 133 |
|
| 134 |
# Text input
|
| 135 |
st.markdown("#### Text to Analyze")
|