Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +74 -96
src/streamlit_app.py
CHANGED
|
@@ -139,7 +139,6 @@ st.subheader("Question-Answering", divider = "violet")
|
|
| 139 |
# Replaced two columns with a single text input
|
| 140 |
question_input = st.text_input("Ask wh-questions. **Wh-questions begin with what, when, where, who, whom, which, whose, why and how. We use them to ask for specific information.**")
|
| 141 |
|
| 142 |
-
|
| 143 |
if st.button("Add Question"):
|
| 144 |
if question_input:
|
| 145 |
if question_input not in st.session_state.user_labels:
|
|
@@ -149,110 +148,89 @@ if st.button("Add Question"):
|
|
| 149 |
st.warning("This question has already been added.")
|
| 150 |
else:
|
| 151 |
st.warning("Please enter a question.")
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
st.subheader("Record of Questions", divider
|
| 155 |
-
|
| 156 |
-
if st.session_state.user_labels:
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
# Remove the label at the specific index
|
| 166 |
-
st.session_state.user_labels.pop(i)
|
| 167 |
-
# Rerun to update the UI
|
| 168 |
-
st.rerun()
|
| 169 |
-
else:
|
| 170 |
-
st.info("No questions defined yet. Use the input above to add one.")
|
| 171 |
-
|
| 172 |
-
def get_stable_color(label):
|
| 173 |
-
"""Generates a consistent hexadecimal color from a given string."""
|
| 174 |
-
hash_object = hashlib.sha1(label.encode('utf-8'))
|
| 175 |
-
hex_dig = hash_object.hexdigest()
|
| 176 |
-
return '#' + hex_dig[:6]
|
| 177 |
-
|
| 178 |
-
st.divider()
|
| 179 |
-
|
| 180 |
-
# --- Main Processing Logic ---
|
| 181 |
-
if st.button("Extract Answers"):
|
| 182 |
-
if not user_text.strip():
|
| 183 |
-
st.warning("Please enter some text to analyze.")
|
| 184 |
-
elif not st.session_state.user_labels:
|
| 185 |
-
st.warning("Please define at least one question.")
|
| 186 |
else:
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
color_discrete_map=label_color_map
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
csv_data = df.to_csv(index=False).encode('utf-8')
|
| 228 |
-
with stylable_container(
|
| 229 |
-
key="download_button",
|
| 230 |
-
css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
|
| 231 |
-
):
|
| 232 |
st.download_button(
|
| 233 |
label="Download CSV",
|
| 234 |
data=csv_data,
|
| 235 |
-
file_name="
|
| 236 |
mime="text/csv",
|
| 237 |
)
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
if comet_initialized:
|
| 240 |
-
experiment.
|
| 241 |
-
experiment.log_table("predicted_entities", df)
|
| 242 |
-
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap")
|
| 243 |
-
|
| 244 |
experiment.end()
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
experiment.log_text(f"Error: {e}")
|
| 254 |
-
experiment.end()
|
| 255 |
-
|
| 256 |
|
| 257 |
|
| 258 |
|
|
|
|
| 139 |
# Replaced two columns with a single text input
|
| 140 |
question_input = st.text_input("Ask wh-questions. **Wh-questions begin with what, when, where, who, whom, which, whose, why and how. We use them to ask for specific information.**")
|
| 141 |
|
|
|
|
| 142 |
if st.button("Add Question"):
|
| 143 |
if question_input:
|
| 144 |
if question_input not in st.session_state.user_labels:
|
|
|
|
| 148 |
st.warning("This question has already been added.")
|
| 149 |
else:
|
| 150 |
st.warning("Please enter a question.")
|
| 151 |
+
|
| 152 |
+
st.markdown("---")
|
| 153 |
+
st.subheader("Record of Questions", divider="green")
|
| 154 |
+
|
| 155 |
+
if st.session_state.user_labels:
|
| 156 |
+
for i, label in enumerate(st.session_state.user_labels):
|
| 157 |
+
col_list, col_delete = st.columns([0.9, 0.1])
|
| 158 |
+
with col_list:
|
| 159 |
+
st.write(f"- {label}", key=f"label_{i}")
|
| 160 |
+
with col_delete:
|
| 161 |
+
if st.button("Delete", key=f"delete_{i}"):
|
| 162 |
+
st.session_state.user_labels.pop(i)
|
| 163 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
else:
|
| 165 |
+
st.info("No questions defined yet. Use the input above to add one.")
|
| 166 |
+
|
| 167 |
+
st.divider()
|
| 168 |
+
|
| 169 |
+
if st.button("Extract Answers"):
|
| 170 |
+
if not user_text.strip():
|
| 171 |
+
st.warning("Please enter some text to analyze.")
|
| 172 |
+
elif not st.session_state.user_labels:
|
| 173 |
+
st.warning("Please define at least one question.")
|
| 174 |
+
else:
|
| 175 |
+
if comet_initialized:
|
| 176 |
+
experiment = Experiment(api_key=COMET_API_KEY, workspace=COMET_WORKSPACE, project_name=COMET_PROJECT_NAME)
|
| 177 |
+
experiment.log_parameter("input_text_length", len(user_text))
|
| 178 |
+
experiment.log_parameter("defined_labels", st.session_state.user_labels)
|
| 179 |
+
|
| 180 |
+
start_time = time.time()
|
| 181 |
+
with st.spinner("Analyzing text...", show_time=True):
|
| 182 |
+
try:
|
| 183 |
+
entities = model_qa.predict_entities(user_text, st.session_state.user_labels)
|
| 184 |
+
end_time = time.time()
|
| 185 |
+
elapsed_time = end_time - start_time
|
| 186 |
+
st.info(f"Processing took **{elapsed_time:.2f} seconds**.")
|
| 187 |
+
|
| 188 |
+
if entities:
|
| 189 |
+
df1 = pd.DataFrame(entities)
|
| 190 |
+
df2 = df1[['label', 'text', 'score']]
|
| 191 |
+
df = df2.rename(columns={'label': 'question', 'text': 'answer'})
|
| 192 |
+
|
| 193 |
+
st.subheader("Extracted Answers", divider="green")
|
| 194 |
+
st.dataframe(df, use_container_width=True)
|
| 195 |
+
|
| 196 |
+
st.subheader("Tree map", divider="green")
|
| 197 |
+
all_labels = df['question'].unique()
|
| 198 |
+
label_color_map = {label: get_stable_color(label) for label in all_labels}
|
| 199 |
+
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'question', 'answer'], values='score', color='question', color_discrete_map=label_color_map)
|
| 200 |
+
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F3E5F5', plot_bgcolor='#F3E5F5')
|
| 201 |
+
st.plotly_chart(fig_treemap)
|
| 202 |
+
|
| 203 |
+
csv_data = df.to_csv(index=False).encode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
st.download_button(
|
| 205 |
label="Download CSV",
|
| 206 |
data=csv_data,
|
| 207 |
+
file_name="nlpblogs_questions_answers.csv",
|
| 208 |
mime="text/csv",
|
| 209 |
)
|
| 210 |
|
| 211 |
+
if comet_initialized:
|
| 212 |
+
experiment.log_metric("processing_time_seconds", elapsed_time)
|
| 213 |
+
experiment.log_table("predicted_entities", df)
|
| 214 |
+
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap")
|
| 215 |
+
experiment.end()
|
| 216 |
+
else:
|
| 217 |
+
st.info("No answers were found in the text with the defined questions.")
|
| 218 |
+
if comet_initialized:
|
| 219 |
+
experiment.end()
|
| 220 |
+
except Exception as e:
|
| 221 |
+
st.error(f"An error occurred during processing: {e}")
|
| 222 |
+
st.write(f"Error details: {e}")
|
| 223 |
if comet_initialized:
|
| 224 |
+
experiment.log_text(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
| 225 |
experiment.end()
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
|
| 236 |
|