Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -334,20 +334,18 @@ def save_feedback_to_huggingface():
|
|
334 |
def main():
|
335 |
load_environment()
|
336 |
|
337 |
-
# Initialize session state
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
"feedback_log": []
|
345 |
-
}.items():
|
346 |
-
if key not in st.session_state:
|
347 |
-
st.session_state[key] = default
|
348 |
if "feedback_log" not in st.session_state:
|
349 |
st.session_state.feedback_log = []
|
350 |
-
|
|
|
|
|
351 |
st.header("Chat with MODTRAN Documents π")
|
352 |
|
353 |
if not st.session_state.chat_ready:
|
@@ -357,24 +355,53 @@ def main():
|
|
357 |
st.session_state.chat_ready = True
|
358 |
st.success("MODTRAN User Manual loaded successfully!")
|
359 |
|
360 |
-
#
|
361 |
-
|
362 |
-
st.
|
363 |
-
st.
|
364 |
-
|
365 |
-
#
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
378 |
with st.spinner("Generating answer..."):
|
379 |
try:
|
380 |
set_global_vectorstore(st.session_state.vectorstore)
|
@@ -382,43 +409,15 @@ def main():
|
|
382 |
except Exception as e:
|
383 |
response = f"β οΈ Something went wrong: {e}"
|
384 |
|
385 |
-
#
|
386 |
-
st.
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
st.session_state.last_question = user_question
|
391 |
-
|
392 |
-
st.markdown(f"π Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
|
393 |
-
|
394 |
-
with st.form(key=f"feedback_form_{len(st.session_state.chat_history)}"):
|
395 |
-
rating = st.radio(
|
396 |
-
"Rate this response:",
|
397 |
-
options=["1", "2", "3", "4", "5"],
|
398 |
-
key=f"rating_{len(st.session_state.chat_history)}",
|
399 |
-
horizontal=True
|
400 |
-
)
|
401 |
-
submitted = st.form_submit_button("Submit Rating")
|
402 |
-
if submitted:
|
403 |
-
feedback = {
|
404 |
-
"question": user_question,
|
405 |
-
"response": response,
|
406 |
-
"rating": rating,
|
407 |
-
"timestamp": datetime.datetime.now().isoformat()
|
408 |
-
}
|
409 |
-
st.session_state.feedback_log.append(feedback)
|
410 |
-
|
411 |
-
if len(st.session_state.feedback_log) >= 1:
|
412 |
-
print("π¦ Upload threshold reached β saving feedback to Hugging Face.")
|
413 |
-
save_feedback_to_huggingface()
|
414 |
-
|
415 |
-
st.session_state.feedback_submitted = True
|
416 |
-
st.rerun()
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
|
|
|
|
|
|
|
422 |
|
423 |
if __name__ == "__main__":
|
424 |
load_environment()
|
|
|
334 |
def main():
|
335 |
load_environment()
|
336 |
|
337 |
+
# Initialize session state safely
|
338 |
+
if "chat_ready" not in st.session_state:
|
339 |
+
st.session_state.chat_ready = False
|
340 |
+
if "chat_history" not in st.session_state:
|
341 |
+
st.session_state.chat_history = []
|
342 |
+
if "vectorstore" not in st.session_state:
|
343 |
+
st.session_state.vectorstore = None
|
|
|
|
|
|
|
|
|
344 |
if "feedback_log" not in st.session_state:
|
345 |
st.session_state.feedback_log = []
|
346 |
+
if "feedback_submitted" not in st.session_state:
|
347 |
+
st.session_state.feedback_submitted = False
|
348 |
+
|
349 |
st.header("Chat with MODTRAN Documents π")
|
350 |
|
351 |
if not st.session_state.chat_ready:
|
|
|
355 |
st.session_state.chat_ready = True
|
356 |
st.success("MODTRAN User Manual loaded successfully!")
|
357 |
|
358 |
+
# Show entire conversation history
|
359 |
+
for i, exchange in enumerate(st.session_state.chat_history):
|
360 |
+
st.markdown(f"**You:** {exchange['user']}")
|
361 |
+
st.markdown(f"**MODTRAN Bot:** {exchange['bot']}")
|
362 |
+
|
363 |
+
# If rated, show rating
|
364 |
+
if "rating" in exchange:
|
365 |
+
st.markdown(f"βοΈ You rated this: {exchange['rating']}/5")
|
366 |
+
|
367 |
+
# If last answer not yet rated, show rating form
|
368 |
+
elif i == len(st.session_state.chat_history) - 1:
|
369 |
+
with st.form(key=f"feedback_form_{i}"):
|
370 |
+
rating = st.radio(
|
371 |
+
"Rate this response:",
|
372 |
+
options=["1", "2", "3", "4", "5"],
|
373 |
+
key=f"rating_{i}",
|
374 |
+
horizontal=True
|
375 |
+
)
|
376 |
+
submitted = st.form_submit_button("Submit Rating")
|
377 |
+
if submitted:
|
378 |
+
# Save rating
|
379 |
+
st.session_state.chat_history[i]["rating"] = rating
|
380 |
+
st.session_state.feedback_log.append({
|
381 |
+
"question": exchange["user"],
|
382 |
+
"response": exchange["bot"],
|
383 |
+
"rating": rating,
|
384 |
+
"timestamp": datetime.now().isoformat()
|
385 |
+
})
|
386 |
+
|
387 |
+
print(f"π Feedbacks collected so far: {len(st.session_state.feedback_log)}")
|
388 |
+
|
389 |
+
# Upload feedbacks when 5 are collected
|
390 |
+
if len(st.session_state.feedback_log) >= 5:
|
391 |
+
print("π¦ Upload threshold reached β saving feedback to Hugging Face.")
|
392 |
+
save_feedback_to_huggingface()
|
393 |
+
|
394 |
+
st.success("β
Thank you for your feedback!")
|
395 |
+
st.session_state.feedback_submitted = True
|
396 |
+
st.rerun()
|
397 |
+
|
398 |
+
# Show how many feedbacks collected
|
399 |
+
st.markdown(f"π Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
|
400 |
+
|
401 |
+
# Show input box for next question
|
402 |
+
user_question = st.text_input("Ask your next question:", key="user_input")
|
403 |
+
|
404 |
+
if user_question:
|
405 |
with st.spinner("Generating answer..."):
|
406 |
try:
|
407 |
set_global_vectorstore(st.session_state.vectorstore)
|
|
|
409 |
except Exception as e:
|
410 |
response = f"β οΈ Something went wrong: {e}"
|
411 |
|
412 |
+
# Append new conversation
|
413 |
+
st.session_state.chat_history.append({
|
414 |
+
"user": user_question,
|
415 |
+
"bot": response
|
416 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
417 |
|
418 |
+
# Clear input and rerun for a clean view
|
419 |
+
st.session_state.user_input = ""
|
420 |
+
st.rerun()
|
421 |
|
422 |
if __name__ == "__main__":
|
423 |
load_environment()
|