File size: 4,252 Bytes
00cccb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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"
    )