Spaces:
Paused
Paused
| # streamlit_app.py | |
| import streamlit as st | |
| from langGraph import graph | |
| import pandas as pd | |
| st.set_page_config(page_title="🧠 Chatbot Terapeutyczny", page_icon="🧠") | |
| # --------- INICJALIZACJA STANU --------- | |
| if "state" not in st.session_state: | |
| st.session_state.state = { | |
| "messages": [], | |
| "awaitingUser": False, | |
| "first_stage_iterations": 0, | |
| "distortion": None, | |
| "situation": "", | |
| "think": "", | |
| "emotion": "", | |
| "messages_socratic": [], | |
| "distortion_def": "", | |
| "cel": "", | |
| "wniosek": "", | |
| "decision_explanation": "", | |
| "proposition": "" | |
| } | |
| st.session_state.inited = False # czy bot już się „przywitał” | |
| # --------- PIERWSZE ODPALENIE (powitanie bota) --------- | |
| if not st.session_state.inited: | |
| with st.spinner("Uruchamiam bota..."): | |
| st.session_state.state = graph.invoke(st.session_state.state) | |
| st.session_state.inited = True | |
| st.title("🧠 Chatbot Terapeutyczny") | |
| # --------- WYŚWIETLENIE HISTORII --------- | |
| for msg in st.session_state.state["messages"]: | |
| role = msg.get("role", "assistant") | |
| if role == "system": | |
| continue | |
| with st.chat_message("user" if role == "user" else "assistant"): | |
| st.markdown(msg.get("content", "")) | |
| # --------- WEJŚCIE UŻYTKOWNIKA --------- | |
| prompt = st.chat_input("Wpisz wiadomość...") | |
| if prompt: | |
| # 1) dopisz usera do stanu | |
| st.session_state.state["messages"].append({"role": "user", "content": prompt}) | |
| st.session_state.state["awaitingUser"] = False | |
| st.session_state.state["validated"] = False | |
| st.session_state.state["last_user_msg_content"] = prompt | |
| st.session_state.state["last_user_msg"] = True | |
| # 2) wywołaj graph (jedno „kółko”) | |
| with st.chat_message("assistant"): | |
| with st.spinner("Bot pisze..."): | |
| st.session_state.state = graph.invoke(st.session_state.state) | |
| # 3) odśwież widok (żeby zobaczyć nową odpowiedź) | |
| st.rerun() | |
| # --------- SIDEBAR: NARZĘDZIA --------- | |
| with st.sidebar: | |
| s = st.session_state.state | |
| stage = s.get("stage", "—") | |
| distortion = s.get("distortion") or "—" | |
| cue_hit = s.get("cue_hit") or "—" | |
| confidence = s.get("confidence") or "—" | |
| noValidated = s.get("noValidated") or "—" | |
| intention = s.get("current_intention") or "—" | |
| socratic = s.get("messages_socratic") or "—" | |
| situation = s.get("situation") or "—" | |
| think = s.get("think") or "—" | |
| emotion = s.get("emotion") or "—" | |
| explanation = s.get("explanation") or "—" | |
| decision_explanation = s.get("decision_explanation") or "-" | |
| proposition = s.get("proposition") or "-" | |
| classify_result = s.get("classify_result") or "—" | |
| st.header("📊 Status") | |
| st.markdown(f"Etap: {stage}") | |
| st.markdown(f"Sytuacja: {situation}") | |
| st.markdown(f"Myśl: {think}") | |
| st.markdown(f"Emocje: {emotion}") | |
| st.markdown(f"Zniekształcenie: {distortion}") | |
| st.markdown(f"Classify_result: {classify_result}") | |
| st.markdown(f"Cue: {cue_hit}") | |
| st.markdown(f"Confidence: {confidence}") | |
| st.markdown(f"NoValidated: {noValidated}") | |
| st.markdown(f"Explanation: {explanation}") | |
| st.markdown(f"Intention: {intention}") | |
| st.markdown(f"Socratic: {socratic}") | |
| st.markdown(f"Decision: {decision_explanation}") | |
| st.markdown(f"Proposition: {proposition}") | |
| st.header("⚙️ Narzędzia") | |
| rows = [] | |
| for m in st.session_state.state["messages"]: | |
| role = m.get("role", "assistant") | |
| if role == "system": | |
| continue | |
| content = (m.get("content") or "").replace("\r\n", "\n").strip() | |
| if role == "assistant": | |
| rows.append({"assistant": content, "user": ""}) | |
| elif role == "user": | |
| rows.append({"assistant": "", "user": content}) | |
| else: | |
| # inne role, jeśli kiedyś wystąpią – zapisz do osobnej kolumny lub pomiń | |
| rows.append({"assistant": "", "user": content}) | |
| df = pd.DataFrame(rows, columns=["assistant", "user"]) | |
| csv_data = df.to_csv(index=False, encoding="utf-8-sig") | |
| st.download_button( | |
| "📥 Pobierz CSV", | |
| data=csv_data, | |
| file_name="chat_history.csv", | |
| mime="text/csv" | |
| ) | |